Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Using the Perl Debugger (-d)

by gaal (Parson)
on Jan 25, 2007 at 13:35 UTC ( [id://596471]=note: print w/replies, xml ) Need Help??


in reply to Using the Perl Debugger (-d)

Debug prints such as yours entail a runtime hit, which is almost always negligible; but they also clutter the code, which often isn't.

I'm not saying never put in debug prints and prefer the debugger instead--I prefer prints myself--but it's useful to distinguish prints you put in for fixing a particular problem at hand, which are usually temporary and removed after the problem is resolved, and logging messages, which are usually permanent even if easily turned off.

For the latter, there are many elaborate logging packages on CPAN, such as Log::Dispatch and Log::Log4perl to name two. For the former, where both informativeness and easy removal are important I usually use these subs which I stick in some common module:

sub ::Y { require YAML::Syck; YAML::Syck::Dump(@_) } sub ::YY { require Carp; Carp::confess(::Y(@_)) }
(It used to be ::D and ::DD using Data::Dumper instead of YAML::Syck, but I often end up installing the non-core Y::S on systems I use anyway, and I like its output better.)

Replies are listed 'Best First'.
Re^2: Using the Perl Debugger (-d)
by sgifford (Prior) on Jan 25, 2007 at 17:23 UTC
    Debug prints such as yours entail a runtime hit, which is almost always negligible; but they also clutter the code, which often isn't.
    If you use something like this:
    use constant DEBUG => 1; print "Some useful information" if (DEBUG);
    the compiler should optimize it away, which will remove even the small performance hit (or at least relegate it to the one-time compilation of the code).
      I have used your same approach sometimes. The problem I hit is that if your program has been migrated to a production environment where the script is read-only, you cannot turn on/off the switch for debugging purposes.
      That is why I prefer to add a command-line switch to turn on debugging:
      #!/usr/bin/perl -s use warings use strict;
      use vars $d;
      use vars qw/$d/; print "Debugging message..." if $d;
      Update
      To run the script you would use perl myscript.pl -d
        I'm sure it must be feasable to set the value for the constant based on a command line switch. I'm not sure if one can just plug in use a module like Getopt::Std or Getopt::Long for this — they probably work at runtime, and might interfere with your own use of command line switches. Hence the handcrafted code, in the example:
        { my $debug; BEGIN { if(grep { $_ eq '-d' } @ARGV) { # look for switch $debug = 1; @ARGV = grep { $_ ne '-d' } @ARGV; # remove switch } } use constant DEBUG => $debug; } print "Debug is on" if DEBUG;

        Does anybody else get a "Useless use of a constant in void context" warning for the print line if DEBUG is off, or is it my old Perl version (5.6.1)? -MO=Deparse tells me it got replaced by the statement '???';, which might explain it. It's still silly.

Re^2: Using the Perl Debugger (-d)
by sgt (Deacon) on Jan 25, 2007 at 18:32 UTC

    Smart::Comments don't really clutter the code...

    cheers --stephan
      You posted that when I was over to CPAN to get the link. :-)
        Next time say "[cpan://Smart::Comments]" :-)

      I love Smart::Comments! Basically, it requires you to write debug comments like this:

      sub foo { ### entering foo my $x = shift; ### x: $x return $x + 1; } ### starting... my $y = foo(42); ### y: $y ### finish exit;

      As you can see, they're just simple comments, until you use Smart::Comments, when they'll magically print the following output to STDERR:

      ### starting... ### entering foo ### x: 42 ### y: 43 ### finish

      The best part is that, when you disable Smart::Comments, they will sit quietly there -- just like comments they are -- documenting your code, but not being executed.

      Simple, useful... smart!!! :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://596471]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found