Hack 51. Find Compilation Errors Fast


Trace problem code as quickly as possible.

As helpful as Perl is, sometimes a missing semicolon, parenthesis, or closing quotation mark send it into a morass of confusion. Error messages clutter your logs or your console window and, try as hard as you might, you can't see what's not there or what's just a little bit wrong.

When trouble strikes, try a simple technique to zoom in on the error as quickly as possible.

The Hack

When your program really goes kablooey, the best thing to do is not to let Perl try to run it, even through your test programs. If things are going that badly wrong, take a tip from the Haskell world and convince yourself that if it at least compiles, it has to be fairly okay.[1] Just make it compile already!

[1] I kid because I like.

Go to the command line and tell Perl to compile the program with warnings and then stop. If your program is what_went_wrong.pl, use:

$ perl -wc what_went_wrong.pl             

If there's no error, Perl will report a happy okay message. Great! Go on to making your tests pass. Otherwise, grab the first error message and figure out how to solve it.

Binary searching for bugs

What if that error message makes no sense? Perl does its best to figure out the offending line, but because of the flexible syntax that allows you to span multiple lines and use postfix conditional expressions, sometimes the best it can say is "something's wrong". In that case, narrow down your search time considerably with a binary search.

Pick a place somewhere in the middle of the file, preferably between subroutines or methods. Add the __END__ token at the start of a line. Effectively this turns the rest of the code into data, so Perl will ignore it. Run perl -wc again. If the error message occurs, the error is in the first half of the file. If the error disappears, the error is in the second half of the file. Move the __END__ token appropriately halfway between whichever end of the file has the error and your current position and try again.

Sometimes you can't move the token without breaking up a block, which definitely causes compilation errors. In that case, use a pair of =cut POD directives to comment out the offending code. Within a handful of iterations, you'll zero in on the problem and you should have an easier time deciding how to fix it.

Hacking the Hack

This technique also works decently for figuring out where an error occurs, especially if you don't have logging or tracing statements in the code. Instead of commenting out code selectively, dump appropriate data with YAML or another serialization module at appropriate places to narrow down the error.

This approach often works, but it can fail when you do odd things, such as using a source filter. Beware.



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net