Recent additions to CPAN

Healthy in Old Age

Author(s):

Many people have declared the granddaddy of scripting, Perl, to be dead. A look at new items in the CPAN software repository, however, shows that the community is still quite active.

To paraphrase Friedrich Nietzsche, and may I be forgiven for doing so, "When Zarathustra was alone, however, he spoke thus to his heart: Is it possible? This old saint has not yet heard in his forest that Perl is dead!"

No question: Perl as a programming language has lost some of its significance. Few youngsters today have enough patience to grapple with the admittedly whimsical peculiarities of what used to be the leading scripting language. For years, many users have been moving off in various directions. This does not seem to affect the will of diehard Perl hackers, however. Hardly a day goes by without 50 new module versions appearing on CPAN [1] (Figure 1).

Figure 1: Every day about 50 new module versions land on the CPAN Recent page – striking proof of an active community.

This edition of my column accesses some of the most useful and entertaining modules I stumbled upon while reviewing what the Perl community has released in the past two weeks.

The King's Speech

Converting written text into spoken words requires a speech synthesizer – algorithmically, this is clearly top-level computer science that takes a lot of brain and computing power. Instead of struggling with this itself, the CPAN Speech::Google::TTS module [2] hooks into the multibillion dollar infrastructure of the Google Group by misusing the http://translate.google.com site, where Google provides word-for-word translations of typed text phrases.

But, there's more: At the bottom of the text box is a loudspeaker icon; when clicked, a computer voice dictates the pasted text (Figure 2). The module used in Listing 1 [3] intercepts the sound output and saves it as a .wav file in the /tmp directory. The as_filename() method shows the associated file name, and a call to the MPlayer binary in line 9 plays the spoken text.

Listing 1

text2speech

 

Figure 2: The CPAN Speech::Google::TTS module uses the sophisticated speech synthesizers from the translate.google.com page.

The language is set in line 7 to en for US English. If you have always wanted to end a tough build job acoustically with a crisp "Finish up your coffee break and get back to work!" command, the script now gives you the opportunity to do so. The module also requires the MPlayer package (or a different audio player like mpg123), which Ubuntu installs via apt-get install.

Analyzing Makefiles

You may have experienced that sense of despair when faced with a complicated makefile. The make syntax is not easily understandable, and if a target happens to depend on three others and each of those depends on …, then it can be very difficult to figure out where a potential bug is hiding. The CPAN GraphViz::Makefile module [4] sets out to visualize the dependencies between make targets with the Graphviz package.

In fairness, it must be said that currently GraphViz::Makefile only correctly visualizes toy makefiles: Once the syntax exceeds a certain complexity, the graphs it generates look like a haystack hit by a tornado. However, the makefile (Listing 2) used in the make tutorial [5] for training purposes was neatly processed by GraphViz::Makefile, as the graph generated in Listing 3 shows (Figure 3).

Listing 2

Makefile

 

Listing 3

make2viz

 

Figure 3: The CPAN module GraphViz::Makefile has visualized the makefile from Listing 2.

The Path to PNG

To do this, the constructor in GraphViz::Makefile in line 6 of Listing 3 first accepts an optional GraphViz object. If the parameter is set to undef, GraphViz::Makefile generates its own Graphviz object. The second parameter is set to Makefile, that is, it finds the Makefile in the current directory.

To generate the graph for a target, the generate() method in line 9 accepts the name of the target (in Listing 2, this is all). From here on, the graph is plotted. At the happy ending, the GraphViz->as_png() method saves the graph as makefile.png (Figure 3). The module may be still in its infancy, but its basic concept is quite compelling.

Invisible Servants

Perl programmers who upload a module or script to CPAN, often forget to specify the dependencies on other CPAN modules. Consequently, the installation fails for all users who do not already have the dependent modules on their systems by lucky coincidence. The CPAN Module::ScanDeps module [6] contains the scandeps.pl script, which expects a script or module as an argument and outputs the dependencies for it.

Applied to the script presented in Listing 3, the output of:

scandeps.pl make2viz

looks like Figure 4. Who would have thought that deep in the belly of GraphViz::Makefile the IPC::Run::Win32Helper module has an important role as part of the digestive system? Linux systems are undoubtedly ill-advised to integrate this; if it were missing on the installation platform, however, Perl would abort the script in the compile phase because it can't find the seemingly required module.

Figure 4: The make2viz script draws on some hidden modules, as Module::ScanDeps quickly reveals.

Multiple-Course Meal

In many command-line utilities, the programmer expects some input by selection from a list of items by the user. For Perl scripts, CPAN offers several text terminal menu simulations. In the list of new module versions that I reviewed, my attention was grabbed by Term::Menus [7]; it offers a convenient programming interface for cases in which the user needs to select an entry from a potentially large list.

Listing 4 shows an example of a script in which the user has to select a file from his home directory. Failure to clean up for many years has led to no fewer than 73 files on my Linux box at home. They are unlikely to fit in a terminal window. As Figure 5 shows, Text::Menus displays 10 items at a time and allows the user to press D to scroll down and U to scroll up through the list.

Listing 4

menu

 

Figure 5: Navigating in a very long list, here in the home directory of Perlmeister Mike Schilli, gets easier by using a convenient CPAN module.

By entering a number, the user finally selects an entry. In the menu script in Listing 4, this is then returned as result of the pick() function in the $selection variable. Term::Menus offers more features on top of that, such as multiple choices or submenus. These are all very handy when you consider that script programmers only need to invest in a few lines of code for this.

Finding Nuggets

A cursory review of the CPAN Recent list shows that it really is worthwhile panning for gold there. In addition to routine maintenance releases of popular modules, you will repeatedly find good ideas for more programming efficiency or entertaining information from the world of a lively Perl community glistening on the bottom of your pan.

"Zarathustra seeks creative collaborators; Zarathustra seeks fellow harvesters and celebrators: What has he to do with herds and herdsmen and corpses!"

The Author

Mike Schilli works as a software engineer with Yahoo! in Sunnyvale, California. He can be contacted at mailto:mschilli@perlmeister.com. Mike's homepage can be found at http://perlmeister.com.