Skip navigation.

Harold's Home

XML version of this site

PHP Scripts

CLI fun
Mail on 404
HB-NS (NewsScript)


APOD to Desktop
Dreamweaver Extensions


Other stuff
Central Grinder

OOOk Default:

VJ stuff
VJ Tools
Sample Movies


Debugging switches in Perl
As I wrote earlier this week I've been busy with Perl. One of the things I wished to do whilst developing the script is printing debugging statements.

Ideally I'd like to use together with -v, -vv and -vvv switches when calling the script to print various levels of debugging statements.

With the magic of the Getopt::Long module included in a standard Perl install this is not only entirely possible but also pretty easy.

Herewith some sample code:
#!/usr/bin/perl -w
use Getopt::Long;
my $v = "";
my $vv = "";
my $vvv = "";
GetOptions ('v' => $v, 'vv' => $vv, 'vvv' => $vvv);
if ($vvv eq 1)
    $v = 1;
    $vv = 1;
if ($vv eq 1)
    $v = 1;
if ($vvv eq 1)
    print "Debugging $0 level 1, 2 and 3\n";
elsif ($vv eq 1)
    print "Debugging $0 level 1 and 2\n";
elsif ($v eq 1)
    print "Debugging $0 level 1\n";

Save this script somewhere as, make it executable (chmod 755 and call it using a various amount of v's.

Some more info on GetOpt::Long can be found at the A.P. Lawrence site.
This may be pretty basic stuff but I think it's pretty cool.

Just a small update: I have updated to samplecode to reflect the fact that multiple v's print more and more info.
For instance -vv prints level 1 and 2 info, which could be very verbose, but doesn't include the most verbose feedback possible (which needs -vvv).

Show all items | Read all items

About, copyright, privacy and accessibility | Mail