Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Confusing warning with if/elsif/else

by gumpu (Friar)
on Dec 08, 2005 at 10:02 UTC ( [id://515186]=perlmeditation: print w/replies, xml ) Need Help??

I'am an avid user of 'strict' however yesterday it cost me a lot of time. Was debugging a program, that distilled to the essentials, looks like:

#!/usr/bin/perl -w use strict; my $a = "foo"; my $b = "bar"; my $c = undef; if ( $a =~ m/aa/ ) { } elsif ( $a =~ m/bb/ ) { } elsif ( $b =~ m/aa/ ) { } elsif ( $c =~ m/aa/ ) { } else { }

Run this and Perl will issue the warning:

Use of uninitialized value in pattern match (m//) at ff.pl line 8
that is, it claims there is something wrong with
if ( $a =~ m/aa/ ) {

Of course in the actual code this was a _much_ more complex expressions with several variables. So I spent a lot of time studying the expression and find out what was wrong.

It turned out there is nothing wrong with the expression! The actual mistake is in:

elsif ( $c =~ m/aa/ ) {

but Perl reports it at the first if.

Just thought I share this so others can avoid doing the same.

Have fun.

2005-12-08 Retitled by g0n, as per Monastery guidelines
Original title: 'Confusing warning'

Replies are listed 'Best First'.
Re: Confusing warning with if/elsif/else
by fergal (Chaplain) on Dec 08, 2005 at 10:40 UTC
    Why are you blaming strict? Delete use strict and the problem still appears.

    The problem is perl's parser/interpretter which doesn't properly record the line number of the various bits of an if. Consider:

    #! /usr/bin/perl my $a = "foo"; if ( $a =~ m/aa/ ) { print "hello"; } elsif ( nonexistant() ) { print "hello"; } else { }

    Which says

    Undefined subroutine &main::nonexistant called at - line 4.

    Basically everything in the conditions of an if statement appears to be on the same line as the if.

    It sucks but it's a known issue and not likely to be fixed any time soon. If I recall correctly, the way perl currently works, to fix this would mean that each block in the if would require it's own stack frame, which is too much overhead.

Re: Confusing warning with if/elsif/else
by demerphq (Chancellor) on Dec 08, 2005 at 10:27 UTC

    This is a known issue that I beleive is being or has been or will be fixed for the next major release. But the rule the thumb for warnings and errors is to look at the line and then the general area. Perl often emits a confusing mishmash of warnings and errors which will all resolve once you find the correct one and fix it.

    ---
    $world=~s/war/peace/g

      Yes. This is a very old and popular bug, which gets reported all the time.

      Although it has been talked about on p5p, it hasn't been fixed. It’s actually a fundamental problem with the way that perl keeps track of the line number, and the proposed fix is to store the line number in every single op.

Re: Confusing warning with if/elsif/else
by Perl Mouse (Chaplain) on Dec 08, 2005 at 11:30 UTC
    Yes, because the problem is with the 'if' statement (the elsif is part of the if statement). Perl only records where a statement (or rather, a corresponding op-code) starts - and that's what's being used in the message.

    Note that the warning has nothing to do with 'strict'. In fact, if it's a warning, it cannot be caused by the strict pragma. The strict pragma throws fatal errors.

    Perl --((8:>*
      >>Yes, because the problem is with the 'if' statement (the elsif is part of the if statement).
      >>
      You are correct. Perl considers if..elsif..else as a complete if box. Hence it is pointing towards line number of 'if'.
      I Didn't know this, this is a good tip for debuggers indeed
Re: Confusing warning with if/elsif/else
by radiantmatrix (Parson) on Dec 08, 2005 at 15:22 UTC

    I find it fascinating that you're focusing on the somewhat inaccurate reporting of line numbers that "cost you time", but seem to be glossing over the fact that strict did help you find a mistake.

    Yes, the behavior of the warning is sometimes strange, and that should (and will) be addressed. But ultimately, strict promises to help highlight mistakes, and it did just that. I'd guess that, overall, you still saved time by avoiding having to hunt down a logic bug later.

    Update: the downvotes tell me that my tone might be mistaken. I'm not trying to be critical or insulting, I really do find it interesting that a problem that appears to cost in the short run seems to take the focus away from the fact that, despite the annoyance, strict is still a good practice. I posted this to remind novices that the benefits of strictures still outweigh their shortcomings.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law

      Afraid I have not expressed myself clearly enough. I am all for 'strict'. Use it all the time. However this warning (and it was not even a strict warning as was pointed out) was just a bit confusing in this particular instance. Just wanted to share this information so others can avoid falling in the same trap.

      Have Fun

Re: Confusing warning with if/elsif/else
by grinder (Bishop) on Dec 08, 2005 at 14:47 UTC

    It's a funny old world. I just commented on this exact problem over at Hash option/menu loop wierd or usefull?. Perhaps you could recast your if/elsif chain as a dispatch table.

    I know of at least one old thread that discusses this issue: Weird Warning (hey, a palindromic node id!). And there are others, but I can't coax them out with a search right now.

    • another intruder with the mooring in the heart of the Perl

Re: Confusing warning with if/elsif/else
by spiritway (Vicar) on Dec 09, 2005 at 05:55 UTC

    Your warning came a couple of days too late for me... Still, I'm glad you wrote up this article, because that problem was really driving me crazy. I'm glad to know that this particular time, it really wasn't all my fault...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://515186]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found