Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Tracking down the line in error or warning

by PoorLuzer (Beadle)
on Mar 24, 2009 at 12:48 UTC ( #752851=perlquestion: print w/replies, xml ) Need Help??
PoorLuzer has asked for the wisdom of the Perl Monks concerning the following question:

A junior wrote some code like this:
use strict; use warnings; our $a = "something"; if($a eq "taste") { print "\nNot equals"; } elsif($a eq m/ERROR/) { print "\nNot matches regex"; }

I wasted 15 mins trying to figure out what was wrong at line 6, because PERL told me to look there:

Use of uninitialized value $_ in pattern match (m//) at line 6.

Yes, I read that you should look around the block where PERL issues a warning message and not usually at the line number itself, but do understand that in real life the code which resulted in this error message was not so tiny as I have posted, neither were the number of conditions and/or branches... so visual inspection is not always very reliable neither productive..

Any suggestions on how to locate the snippet in error next time I face a similar error ?

For those still wondering, an 'eq' should be '=~'.. oh well..

Replies are listed 'Best First'.
Re: Tracking down the line in error or warning
by ikegami (Pope) on Mar 24, 2009 at 13:10 UTC

    When we get this question at PerlMonks, it's always an "if" with an "elsif" clause. Sometimes, the petitioner doesn't mention the presence of an elsif clause, but we tell them the error is there anyway.

    For run-time errors/warnings, Perl tells you which statement generated the error. You have to look at the whole statement when it spans multiple lines (as is the case here).

    I thought they fixed this for if/elsif. Maybe the fix is in upcoming 5.10.1?

    Note the error message in 5.10.0 is better than the one you presented:

    Use of uninitialized value $_ in pattern match (m//) at line 6. ^^ ||

    ActiveState also had their own patch in 5.8.8 (but not 5.8.9 and 5.10.0):

    Use of uninitialized value in pattern match (m//) at line 10. ^^ ||
Re: Tracking down the line in error or warning
by VinsWorldcom (Parson) on Mar 24, 2009 at 13:12 UTC

    When I run that code, I get:

    {C} > Use of uninitialized value in pattern match (m//) at C:\tmp\ lin +e 10.

    Line 10 is the pattern match in question. In any case, if it had spewed line 6, I would have looked at the IF block starting there and inserted some trusty "print "HERE\n" debug helpers to see where I was getting dumped out.

    For more complicated logic trees and loops, I use PerlIDE - a simple freeware development tool that can do a step into/over debug which is really helpful if I'm stuck.

      Yeah, I was going to throw that in, too. Some well placed print statements can really make debugging easier.

      Also, and this may be obvious, but a text editor that shows the line numbers and highlights code can be a big help, too. Scanning through huge scripts looking for all the opening and closing brackets on functions is a lot easier when code is all pretty. It's embarrassing to me how many times my errors have just been one too many, or too few, brackets, parenthesis, or some similar typo.
      Wow.. this version of PERL is nice.

      The original code that I "debugged" used PERL 5.8 for HPUX and the error message I posted was from ActivePerl 5.10.. both gave the same error message (5.8 did not show the $_ however)

      No other way than PerlIDE (which I believe runs only on Windows)?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://752851]
Approved by Corion
shmem puts a big cauldron of "silly con charme" on the table in the refectorium
[shmem]: mahlzeitTM

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-04-27 11:16 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (503 votes). Check out past polls.