Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
XP is just a number

Module for Printing Line Numbers in Debug?

by QM (Vicar)
on Jan 21, 2013 at 12:04 UTC ( #1014430=perlquestion: print w/ replies, xml ) Need Help??
QM has asked for the wisdom of the Perl Monks concerning the following question:

I have a library of modules I'm trying to debug, and I'd like to instrument some or all of them to output line numbers to STDERR, to help trace execution through some non-linear code, filled with lots of callbacks and such, where the normal code emitting quite a lot to STDOUT and STDERR.

Currently, I do a manual find/replace in the editor of big chunks of code, equivalent to this:

s/;\s*$/; $DEBUG and printf STDERR "***LINE*** [%s] %s: %s (%d)\n", ti +me_stamp(), __FILE__, __PACKAGE__, __LINE__;/

This of course makes the code hard to read, and doesn't work everywhere (I think use and package might have problems with this, maybe others.) Colleagues often get distracted with these, instead of seeing the important bits.

Really I'd just like a source filter or module I can invoke on the command line to safely do this between statements, and not muddy up the source code.

Any suggestions?

Update: I use a special script to run the script on a remote cluster of hardware that is not available locally. All files needed are copied over and untarred into an appropriate temp dir, the script run, and all STDOUT and STDERR reported back to the originating terminal.

Interactive debug would be my first choice, but the nature of the system makes it much easier to do s/// and print lines into the output logs.

Quantum Mechanics: The dreams stuff is made of

Comment on Module for Printing Line Numbers in Debug?
Select or Download Code
Re: Module for Printing Line Numbers in Debug?
by LanX (Abbot) on Jan 21, 2013 at 12:36 UTC
    Did you consider using the debugger?

    It has some nifty trace options!


    Also have a look into Carp.

    Cheers Rolf

      perldebug might work, but I can't figure out how to run a script non-interactively to do a trace with line numbers.

      I forgot to mention that this is running on a remote system, using a custom script to tar up everything needed, copy it over, run it, and pipe the output back to the originating terminal. It's a PITA to do this manually.

      It's also a requirement to run on the remote system, as custom devices and other hardware are there (these are formal test scripts).

      I'm quite familiar with the interactive use of the debugger, and would prefer to do that, but it's much easier to s/// the code and print line numbers. (OP updated).

      Quantum Mechanics: The dreams stuff is made of

        I googled trace perl and found perl option to trace execution.

        Devel::Trace seems to be what you are looking for.

        EDIT: It's not clear for me if you wanna trace all lines or just some. In the latter case why not defining a function log() which carps what you want only if DEBUG is set?

        Cheers Rolf

Re: Module for Printing Line Numbers in Debug?
by clueless newbie (Friar) on Jan 21, 2013 at 12:48 UTC

    Perhaps you could use Hook::LexWrap to wrap the subs in the modules that you're interested in. There was this thread Fun with Hook::LexWrap and code instrumentation that might be of interest.

    Update - Huh? I've got a module that automagically wraps all of the subs of subsequently loaded modules and then during execution generates a SQLite database of call chains together with what was passed and what was returned. All one would have to insert/remove one use statement. Is that what you're looking for?

    Dang - something I wrote might actually be useful!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014430]
Approved by vinoth.ree
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-19 12:40 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (480 votes), past polls