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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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".


In reply to Re: Looking For The Obvious - Debugging Perl by slife
in thread Looking For The Obvious - Debugging Perl by greenFox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (3)
    As of 2014-10-26 07:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (152 votes), past polls