Poor man's "strace" for Perl with vim

Ovid on 2006-08-14T10:19:34

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.


What's up with Devel::Trace?

Simon on 2006-08-14T10:50:36

If all you want is a trace of lines executed, Devel::Trace should do the trick for you.

Re:What's up with Devel::Trace?

Ovid on 2006-08-14T11:01:40

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.

Re:What's up with Devel::Trace?

jjore on 2006-08-14T15:24:36

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.

Re:What's up with Devel::Trace?

Ovid on 2006-08-14T15:31:04

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.

Re:What's up with Devel::Trace?

Ovid on 2006-08-14T11:14:50

Oh, and thanks for pointing that module out.