An Overview of Getopt::Euclid, Smart::Comments, and Log4Perl

Todd introduced the modules Smart::Comments, Getopt::Euclid, and Log::Log4perl. The first two modules were recommended by Perl Best Practices by Damian Conway. (They were also written by Conway.) The final module is a port of the Log4j package from Java to Perl.

Todd walked us through various uses of the Smart::Comments module. Smart comments work by filtering the text of the Perl program and converting specially formatted comments into print statements. The most basic uses of Smart::Comments are debugging related. Using a smart comment of the form:

      ### var: $var

prints the value of the variable with a label. You can also print just a marker by using a comment without any embedded variables.

      ### Before print routine...

A much more interesting feature of Smart::Comments is the automatic generation of progress indicators for loops. The general form looks like:

      for my $elem (@all_elements) { ### Working...            Done
          # do work on each element

which animates the three dots after the work Working to show current progress. Variations in the comment can provide an indication of percent done and time remaining. See the documentation for the module for the details.

The final feature of Smart::Comments is the ability to provide assertions. These are statements of conditions at a particular point in the code that must be true. If an assertion is violated, the program terminates with an exception. If you have experience with asserts in other languages, you may find these quite useful.

Todd went on to describe the easy mode of Log::Log4perl. This provides an easy introduction to this powerful logging framework. The main purpose of the module is to provide flexible and powerful logging for your Perl programs without building yet another framework. Todd showed how using the easy mode allows you to replace random print statements in your code with more explicit logging statements that can be written to a logfile. Given a reasonable logging format, this generates messages that are easier to search and filter when you are troubleshooting a problem.

The Getopt::Euclid module serves the same purpose as most of the other Getopt:: modules on CPAN. The idea is to provide a simple way of parsing command line options that makes passing parameters to the program easy. Most modules provide some special configuration information to define the parameters that the program will accept.

Getopt::Euclid takes the unusual approach of using the POD documentation as the configuration. This has the advantage of keeping your argument processing, POD documentation, and usage message all in sync. The POD documentation is pretty standard with a few minor changes needed if you want the module to check parameter arguments for you.