Smart::Comments and Devel::Cover

[ Perl tips index ]
[ Subscribe to Perl tips ]

Comment your code with Smart::Comments

Normally we comment our code so that humans (including ourselves) can understand it later on. However with Smart::Comments it becomes possible to have comments which are not only useful in comprehending our code, but also in debugging our code.

With Smart::Comments enabled, when a special comment sequence is seen (### by default) debugging output is produced:

    use Smart::Comments;
    ### [<now>] Running backups...
    my $backup_dir = get_backup_host();
    ### Backing up files to: $backup_dir
    foreach my $file (@manifest) { ### Copying files...  % done
        backup($file, $backup_dir);
    }
    ### [<now>] Backup complete...

With Smart::Comments enabled, output similar to the following would be seen:

    ### [Fri Feb 25 12:14:34 2011] Running backups...
    ### Backing up files to: '/mnt/backup'
    Copying files........      64% done
    ### [Fri Feb 25 12:17:57 2011] Done...

One of great advantages of Smart::Comments is that the use Smart::Comments line can simply be removed (or commented away) when done, and all our smart comments are then simply ignored by Perl. There's no requirement that Smart::Comments be installed on the production systems, and no risk that that it may interfere with your code.

You can read more about Smart::Comments on the CPAN at http://search.cpan.org/perldoc .

Devel::Cover

Tests are essential in the production of high quality software, but even the best written test suite can miss testing certain lines of code.

Devel::Cover allows a developer to run their test suite, and gain detailed information on exactly what was and wasn't tested. Devel::Cover does a lot more than merely listing which statements are tested, it also provides feedback on which branches were covered, whether boolean expressions have had all options exercised, and which subroutines weren't run at all.

As an example, consider the following statement:

    if ($verbose or $debug) {
        say "Considering $data";
    }

If we've only tested our condition when both $verbose and $debug are false, or when $verbose is true, then we've missed the remaining case where $verbose is false and $debug is true. Devel::Cover is smart enough to spot these gaps in conditional coverage and report them. This is useful in both spotting additional tests that need to be written, and can sometimes find sections of code that can never be executed, facilitating their removal.

The simpliest way to use Devel::Cover is to invoke it with:

    cover -test

which will run your test suite, and produce an HTML report. Other ways of invoking Devel::Cover can be found in its documentation at http://search.cpan.org/perldoc .

[ Perl tips index ]
[ Subscribe to Perl tips ]


This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.

If you have any questions please don't hesitate to contact us:

Email: contact@perltraining.com.au
Phone: 03 9354 6001 (Australia)
International: +61 3 9354 6001

Valid XHTML 1.0 Valid CSS