[ABE.pm] printing status messages

Walt Mankowski waltman at pobox.com
Sat Apr 28 05:01:40 PDT 2007


On Thu, Apr 26, 2007 at 09:57:49PM -0400, Ricardo SIGNES wrote:
> * "Faber J. Fedor" <faber at linuxnj.com> [2007-04-26T21:15:24]
> > Using GetOpts, I usually have a command line options like --debug or
> > --verbose which causes my program to spit out a lot of status
> > messages/debug statements.
> 
> I use Getopt::Long::Descriptive and its bigger brother App::Cmd.  At work, we
> use a superset of that which integrates with Log::Speak to do this sort of
> thing seamlessly.  Still, you can just do this:
> 
>   sub debug {
>     return unless $DEBUGGING;
>     print join '', @_, "\n";
>   }
> 
>   debug "Your message here.";
> 
> Or you could have:
> 
>   sub debug {
>     return unless $DEBUG_LEVEL >= 5;
>     ...
>   }
> 
> ...and have a "alert" and "info" sub for other debug levels.
> 
> See also Log::Dispatch, etc.

I second the idea of writing a simple debug function like Ric
suggests.  It really cleans up your code.  You might also considering
writing a version that uses printf, for those times where you want
more control over the formatting.

Here are the routines I use.  They live in Util.pm, so they have to
reference the $main package.

  #
  # dprint() and dprintf() are just like print() and printf(), except
  # they print to STDERR, and they only print if $main::DEBUG is true
  # (typically set with the --debug command line option)
  #
  sub dprint  { print  STDERR @_ if $main::DEBUG; }
  sub dprintf { printf STDERR @_ if $main::DEBUG; }

Here are some examples of it in action.  (It was a graphics program I
was working on btw.)

  dprintf "(%f, %f)\n", $self->{X}, $self->{Y};
  dprintf "(%d, %d) %04b\n", $x, $y, $outcode;
  dprintf "%d/%d faces rejected by back-face culling\n",
          $old_faces - scalar @faces, $old_faces;

Walt


More information about the ABE-pm mailing list