pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Besseching blessed bretheren and sistren.

This is one of those, "how does everyone handle 'blah'" questions.

When writing scripts I like to make them respond to various levels of verbosity, so:

./foo.pl -v

Might output...

Starting... Working... Finishing... Done...

and

./foo -v -v

Might output

Starting... found config... config looks good... Working... ate 57 apples... ate 23 bananas.. Finishing... threw out apple cores... threw out banana peels... Done...

I'm totally good as far as getting and processing the args (Getopt::Long).

What I'm wondering is, how does everyone handle the bits inside code that produce the output dependant on the verbosity level? Is there a really good module for this? Does everyone just roll their own?

Thanks!
-Pileofrogs

Replies are listed 'Best First'.
Re: controlling script verbosity
by jdporter (Paladin) on May 04, 2007 at 22:53 UTC

    Assuming you're going to code this yourself, and not use a module... ;-)

    First, you have a verbosity level setting, say $verbosity, whose value is set by the number of -v switches (or whatever).

    You also define a function for producing the output which takes, in addition to the message content, a number which is the "severity" of the message. If this number is less that the verbosity level, the message is printed, otherwise, not. (Note that higher numbers indicate lower severity.) Example:

    my $verbosity = 0; GetOptions( 'verbose+' => \$verbosity, ); sub report { my $severity = shift; print @_ if $severity <= $verbosity; } report( 2, "reached ".__FILE__.":".__LINE__."\n" );

    To make it really useful, you'd want to have versions that warn and die as well.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: controlling script verbosity
by merlyn (Sage) on May 04, 2007 at 22:14 UTC
Re: controlling script verbosity
by Cap'n Steve (Friar) on May 05, 2007 at 10:31 UTC
    I don't know how complicated you plan on making this, but for my simple debug messages I just use

    $debug > 2 && print 'This is a very verbose message.';

    I'm sure that's not the most flexible or optimized solution, but it works for most things.