Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday August 14, 2006
05:19 AM

Poor man's "strace" for Perl with vim

[ #30621 ]

With the help of "smylers", I managed to create a poor man's version of strace for Perl. Basically, it's a touch of vim work with prints out almost every Perl line after that line is executed.

First, I add the following to the beginning of the program (though I put it all on one line):

use IO::Handle 'autoflush';
my $log = "db_upgrade.log";
open DEBUG_OUT, '>', $log or die $!;
autoflush DEBUG_OUT 1;

Then I type ,s, which is mapped to the following hideous line in my .vimrc:

noremap ,s :g/;$/
            \ s/.*/&\t\t\tprint DEBUG_OUT 'Line ' . __LINE__ . q(: &).\"\\n\"; # XXX debug/
            \ \| %s/^\(\s*);\)\s*print DEBUG_OUT.*/\1/

I then run !perl -c % a few times (mapped to ,c) to clean up some of the offending lines which remain. After that, I run the program while doing tail -f db_upgrade.log in a separate window and I can watch what's happening while I run it.

It's a nasty hack and I really need to look into other options.

Note that you need to do less cleanup if you use a unicode character for the q'' delimeter in the mapping, but my vim was choking on that. If I take the trouble to turn this into a proper function, that would make things easier to fix (I can escape things which give me grief). Also, if you have multi-line strings which end with a semicolon (such as SQL often does), then this will likely break.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • If all you want is a trace of lines executed, Devel::Trace should do the trick for you.
    • Devel::Trace is really close to what I want. However, it doesn't allow me to control the filehandle I want to send the trace output to, so error messages I might need are comingled with the trace output. However, the reality is, I didn't find that module when I was searching (I was searching for strace :), so this is only an "after the fact" gripe.

      • You could have just patched it. It'd have been easier. Also, your perl code transformation would have been easier to follow if the regex hadn't been obfuscated in that vim syntax.
        • Well, I just sent an email to Dominus asking if he's interested in patches for it. Amongst other things, it would be nice to pass in callbacks so I can have fine-grained control over what's being dumped out, including access to the debugger API. It would be nice if I don't start tracing until $x >= 3 or simply don't trace except when a particular variable changes.

    • Oh, and thanks for pointing that module out.