Tool for Perl Scripting - Devel::Comments

One of the advantages to Perl is that as a scripting language, a small script can be coded to perform a simple task. For example, I enjoy sharing photos of family over the internet, but all the images are at least 2.4Mb in disk size! Using a mix of Perl and Unix commands, I have a script no bigger than 1 page to resize entire folders for me.

I like to use an iterative method of programming. Code some lines, execute and test. Rinse and repeat. But I found my method of debugging annoying. $\ = "\n"; and print() statements to sanity check my variables, only to delete or comment those lines later.

Enter a great Module: Devel::Comments A module to help with debugging in the development stage by using the comment lines in your scripts.

Make the comments in your code work for you. Now to view the value of a variable: write:

### $mainDirectory

and STDERR will print out:

### $mainDirectory: /home/tyler/perl

You can set different output channels when you use the Module, or a combination of them (STDOUT and a filehandle). Best of all, when you are done the development work, you can simply remove the 'use Devel::Comments' line or comment it out, and all debug statements are disabled at once. The number of # characters you use determine the debug level, and different levels can be enabled or disabled for use.

Two final things: Assertions and checks are available too, that will act as a warn and die if the boolean checks fail.
### insist: $divisor != 0 (the assert statement)
### check: $current_year > 1900

This Module has proven to be much more helpful than the print statements that I once used when creating small scripts for my personal use. I use much more formal debugging and logging methods for applications I release, such as Log::Log4Perl.


Or you can enableDevel::Comments when running:

perl -MDevel::Comments <your script>

Leave a comment

About Tyler Slijboom

user-pic I blog about Perl, managing a web hosts, Apache and client work space.