Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

(Debugging, Tracing, Trace, Instrumenting) Automatic tracing of my Perl code?

by princepawn (Parson)
on Sep 22, 2000 at 17:48 UTC ( [id://33630] : perlquestion . print w/replies, xml ) Need Help??

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

The above title is designed to allow people to search this question on a variety of likely keywords.

I want my Perl program to automatically say on what line it is at all times up to the point it dies. But, I dont want to litter my code with a bunch of print statements.

Is this possible?

  • Comment on (Debugging, Tracing, Trace, Instrumenting) Automatic tracing of my Perl code?

Replies are listed 'Best First'.
(Ovid) RE: (Debugging, Tracing, Trace, Instrumenting)
by Ovid (Cardinal) on Sep 22, 2000 at 20:46 UTC
    Why do you want to do this? If you're looking to trace the program flow, check out the built-in debugger. You can step through your program, set breakpoints on lines, subroutines, and conditionals. You can also print stack backtraces and print complex data structures on the fly to verify that they're correct.

    I suspect that a lot of people overlook the usefulness of the debugger as many questions that are answered here would quickly be resolved with a quick debugger session.

    Check it out. It will quickly become your best friend. And yes, if you step through your program, it will automatically say what line the program is on at all times :) In fact, everything that you describe in your title is contained in the debugger.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just go the the link and check out our stats.

      I have to agree with Ovid. As with everything else, why reinvent the wheel? Not only does the build-in debbuger work very well, its already there.

      I'm on Win32, so I decided to give ActiveState's debugger a shot, and I have found it not only to be a solid app, but a very very useful tool while trying to figure where things went sour in my apps.

      The idea of printing line by line does sound like a cool challenge though, but only as one of those I-wonder-if-this-can-be-done? things...

      #!/home/bbq/bin/perl
      # Trust no1!
constant line number reporting
by japhy (Canon) on Sep 22, 2000 at 20:28 UTC
    Ok, try this. It's not guaranteed to work, but it should be better than trying to brave the multi-line problem from above.
    package LineDebug; $LOADED = 0; $insert = qq<print "\\tDEBUG: " . __LINE__ . "\\n";\n>; sub import { return if $LOADED++; my $freq = (@_ == 3 && pop) || 1; my ($file) = (caller)[1]; my $code; open PROG, $file; while (<PROG>) { $code .= $_; if ($freq == 1) { next unless /\S/; $code =~ s/;\s*\n\z/; $insert/; } elsif ($. % $freq == 0) { if (!/\S/) { $code =~ s/\n\z/; $insert/ } else { $code =~ s/:;\s*\n\z/; $insert/ } } } close PROG; # print $code; eval $code; exit; } 1;
    It's used like so:
    use LineDebug; # code...
    or
    use LineDebug freq => 10; # code...
    It will print the line number of the file on every line ending with a semicolon (THIS IS NOT 100% ACCURATE). Specifically, it will not do this on blank lines. If you give it a frequency argument, it will print every X lines, if that line ends in a semicolon or is blank.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: (Debugging, Tracing, Trace, Instrumenting) Automatic tracing of my Perl code?
by t0mas (Priest) on Sep 25, 2000 at 12:06 UTC
    I usually set PERLDB_OPTS to:NonStop frame=2 AutoTrace LineInfo=debug.out
    I then run my program with perl -d and look through the the debug.out file. Will that do for you?

    Another option is to add:
    { package DB; sub DB { my ($package,$filename,$line)=caller; print "Line: $line\n"; } }
    at the end of your code and set PERL5DB to DB::DB {}
    That will mix program output and lines on STDOUT (or whatever you have selected)...

    /brother t0mas
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: (Debugging
by Fastolfe (Vicar) on Sep 22, 2000 at 19:46 UTC
    You may be able to wrap your script in another script that inserts the print statements for you:
    my $script; while (<DATA>) { $script .= $_; $script .= qq{print "line $.\\n";\n}; } eval $script; __END__ # your script goes here
    You may be able to take this on step further and place that in a package, say, 'FollowLines', and then do something like this:
    use FollowLines; __DATA__ # your script goes here
    Be advised, though, that multi-line constructs will break this. Consider this code:
    if ($some_var == 1 || $other_var == 2 || $third_var) { sprintf("%s %s %d %s\n", $some_var, $other_var, @rest); }
    A print statement every line would be a Bad Thing. I'm afraid there's not much you can do to get this functionality in a reliable fashion.