Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Using the Perl Debugger (-d)

by MaxKlokan (Monk)
on Jan 26, 2007 at 10:33 UTC ( [id://596688]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Using the Perl Debugger (-d)
in thread Using the Perl Debugger (-d)

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

Replies are listed 'Best First'.
Re^4: Using the Perl Debugger (-d)
by bart (Canon) on Jan 26, 2007 at 11:59 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://596688]
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: (2)
As of 2025-07-12 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.