Trick or Treat #3

#0 | #1 | #2 | #3 | #4

Perl Modules - diagnostics

If you think Perl is pretty cool and powerful after reading over these pages, you ain't seen nothin' yet.

Every Perl distribution comes with a standard set of 'libraries'.

A very special type of library is a module. It is a single file of Perl subroutines that perform a set of tasks. It typically has the file extension .pm for 'perl module'.

Modules are available for almost every task you can imagine doing - there are over 200 that come installed with Perl.

Besides the standard ones that come with every Perl, there is a huge collection of them, usually written by experienced Perl programmers. They are available free for download from CPAN - the 'Comprehensive Perl Archive Network'.

To use modules, we have to say use [MODULE]; in our script. We've already seen this in our scripts: use strict; and use warnings;

These 2 modules are pragmas - special Perl modules that tweak some internal settings.

I'm not going to go into detail about modules in general. There are other (better) resources for that.

But I would like to show you one that may be useful. The one I chose to 'use' is diagnostics. It may help in debugging your own Perl scripts.

So start a new Perl file and add the following:

#!/usr/bin/perl
use strict;
use warnings;
use diagnostics; # <----- the new one

Now we need to add some Perl code that doesn't work - just so you can see what happens:

print $fubar;
print "This code doesn't work. I wonder why?\n

After running that script you should get a screenful of text, but not what your script says to print.

As you can see it is quite, ummm, verbose.

Global symbol "$fubar" requires explicit package name at ./oddends2.pl line 11.
Can't find string terminator '"' anywhere before EOF at ./oddends2.pl line 12 (#1)
    (F) You've said "use strict" or "use strict vars", which indicates 
    that all variables must either be lexically scoped (using "my" or "state"), 
    declared beforehand using "our", or explicitly qualified to say 
    which package the global variable is in (using "::").
    
Uncaught exception from user code:
	Global symbol "$fubar" requires explicit package name at ./oddends2.pl line 11.
	Can't find string terminator '"' anywhere before EOF at ./oddends2.pl line 12.

Two things that you should watch for:

Hopefully in there you will also find a clue as to what went wrong with your script.

Perhaps you will find that just using strict and warnings is sufficient - great. But if you need more help debugging it's available.

The official diagnostics page at CPAN.