Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: I usually debug via...

by ff (Hermit)
on Feb 15, 2005 at 13:47 UTC ( #431167=note: print w/ replies, xml ) Need Help??


in reply to Re: I usually debug via...
in thread I usually debug via...

Here here, warn belongs on the list!

My app is a Perl Tk app and has grown rather large. Since I sometimes will have to rerun little bits of debugging steps in the future, I leave my warn statements in the code, just comment them out when I don't need them. The trick is to start all such debug-flavor warn messages in column 0. This makes them easy to distinguish from real warn statements which are indented further into the code. After using the warning for the first time, comment away the warning with an initial # sign.

When I want to reuse a given warning, I uncomment the line, and when I'm ready to turn them off again, finding them is easy in vim because I only have to look for warn at the beginning of lines, i.e. I search for ^warn. When I find an uncommented warn, I do a I# to comment it away again, do a n to find the next uncommented warning and do a dot command to repeat the "comment away" command.


Comment on Re^2: I usually debug via...
Select or Download Code
Re^3: I usually debug via...
by bunnyman (Hermit) on Feb 15, 2005 at 16:00 UTC

    comment them out when I don't need them

    Have you considered trying something like

    warn "The code is running in debug" if $::debugging;

    This way you can just say $debugging = 1; instead of changing many lines of code. vim is a great tool, but it isn't a debugger. For debugging, you need emacs. :-)

    Update: You could even have $::debugging set by command-line switches, or an %ENV variable or a CGI parameter. Then you would not need to change any of the code to get some debugging info. This is great if something prevents you from touching the code, for example if it is running on a production web server.

      Generally a good approach, except that my programs are large and that could turn on hundreds of warnings. For me, I generally just need to turn on a few warnings at any given time and it's no big deal to hop around to those four or five that I've unmasked to comment them back up.

        My code is usually littered with stuff like print "Foo: ".Dumper(\$foo) if $debug>7;

        Often I write those in not knowing if I'll ever want that level of verbosity; usually I never have to crank $debug higher than 2 or 3. (1 just prints progress info usually, depending on what the app is; 2 gives more detail, and so on. 0 only prints actual errors, and undef suppresses non-fatal errors. Anything past 5 is dumping lots of big data structures.) Occasionally I do end up cranking it way up, though, and adding in extra print statements like the above, that I didn't originally anticipate needing.

      When I write CGI scripts, I like to have a &printError(); routine. Then all my debug statments call &printError("Message", $debug_level);. Based either on a config file, or and environment variable, &printError(); can decide whether or not $debug_level is high enough to send the message out to STDERR.

      May the Force be with you
Re^3: I usually debug via...
by ff (Hermit) on Feb 17, 2005 at 05:20 UTC
    Also along these lines, I like to use lines that start with if 0 and if 1 as additional debugging tools. I particularly use this approach with a little subroutine that invokes Dumper (I could probably do this directly, but anyway). Again, when I want the program to dump the object, it's easy to flip the '0' to '1', and when I'm done with the need to see such output, I can easily find all patterns of ^if 1 and flip the '1' back to '0'.

    For example, my "main" script might contain the following:

    use strict; require 'gen_subs.pl'; my $foo = { 'abc', 123, 'def', 456 }; gs::do_dumper( 'foo423:', $foo ) # 423 a silly, easy-to-find label if 0;

    and a helper file/libary named 'gen_subs.pl' might contain:

    package gs; use strict; use Data::Dumper; sub do_dumper { my ( $label, $variable ) = @_; my $msg = Dumper( $variable ); warn "$label:\n$msg\n"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-11-27 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (180 votes), past polls