Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Looking For The Obvious - Debugging Perl

by slife (Scribe)
on Jun 22, 2004 at 08:15 UTC ( #368640=note: print w/replies, xml ) Need Help??

in reply to Looking For The Obvious - Debugging Perl

Learned behaviour can sometimes be a hindrance, even when the behaviour in question is good practice.

One of the habits I acquired in my earliest days as a Perl programmer was to protect myself from the common 'gotcha' of regex capture variables being left at the value of the previous match if the current match fails, e.g. instead of this dangerous code:

# Bad code. Don't copy while(<>) { /^(\w+?)/; my($val)=$1; # do something with $val. }

I'd instead do this:

while(<>) { my($val); if(/^(\w+?)/) { ($val) = $1; } else { # Handle failed regex. } # Do something with $val. }

and this served me well over the years.

However, on one occasion, I was helping a colleague debug a legacy program which was producing bizarre behaviour: it was supposed to read one line at a time from a data file, extracted some values and inserted them into a database. Recently, it had started to intermittently insert apparently random nonsense.

Well, my colleague and I spent hours debugging this thing, trying increasingly obscure and desparate 'fixes', until, finally, it dawned on me that the program was doing example one above, and the format in the data file had changed so that that regex sometimes failed. Problem solved.

Why did this take so long to spot? My colleague and I were so used to programming defensively against this behaviour that we simply didn't recognise it when we saw it. We assumed that because we did the right thing, everyone else did, too.

The lesson here is to always look at what the program is actually doing, not what you think it is, or even should be, doing. Sometimes, this means 'unlearning' good habits.

NB - I would probably have spotted this in minutes had I used the Perl debugger: lesson 2 here is not to neglect your toolset because "it's only an easy bug".

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://368640]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-03-18 22:33 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.