3 Little Perl Debugging Tips

I’ll do a little walk-through of the Perl debugger in a separate post, but here are a couple hard-earned morsels of knowledge that I thought I could share:

If your code is NOT compiling, the most common hard-to-find error is forgetting a semi-colon or a paren or brace in the lines above. The error message usually clocks in on the line AFTER the goof. Look above the line listed in the error message and see if you didn’t forget a semi-colon somewhere.

If your code IS compiling, but you’re still getting weird results, here are a couple tips to help you along:

1. Make sure your code is written in coherent, bite-sized chunks. If you can’t see an entire function/action on the screen, then your building blocks are too big. Writing code in smaller chunks lets you test each chunk individually. Alas, this doesn’t always happen, so…

2. If you are using a lot of hash references ( stuff like $my_hash_ref->{$some_id}->{'some_value'} ), then you are bound to run into the pitfalls of using hashes: namely mispellings. Oops. Misspellings. (Did you catch that?) TRIPLE CHECK YOUR SPELLINGS. Highlight your variable name/hash-key do a search for it — if it doesn’t show up somewhere else, then you know you have a problem. Perl happily will do this:
$my_hash_ref->{$some_id}->{'some_valeu'} = 1;

Note the mispelling — you just stashed a value in the wrong place in the hash. Perl auto-vivified without any warnings because that’s what it’s supposed to do.

3. Make sure you haven’t botched your reference arrows. E.g.
$my_hash_ref->{$some_id}-{'some_value'}

Oops. Notice I forgot a ‘>’ there between the id and value. This actually compiles! But it’s not what you want! Do yourself a favor and search your code for any instances of ‘-{‘ — you may be surprised by the horrors you find.