Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: if-elsif weirdness

by japhy (Canon)
on May 21, 2003 at 15:36 UTC ( [id://259750]=note: print w/replies, xml ) Need Help??


in reply to if-elsif weirdness

Internally speaking, the problem is because only "control ops" (Cop's) have a line number recorded with them. To Perl, an if-elsif-else sequence is one big logical operator, and that statement starts on a specific line. If there's a problem evaluating something in the operator itself, the line number the statement starts on is used. That's why
if ($x) { print 1; } elsif ($x != 2) { print $y; }
will warn that there's an uninitialized value at line 1 (even though it's at line 4), but it will properly tell you about the uninitialized value on line 5 (because each new statement in Perl is a new Cop).

A similar problem is found in this case:

print 1, 2, 3, undef, 4;
Even though the code spans five lines, error messages will refer to the line the statement started on.

It's not going to be easy to fix this bug, methinks.

Update: but a good kludgy work-around is to use a do { ; ... } block!

if ($x) { ... } elsif (do {; $x == 2}) { ... }
That properly reports the warning for line 2. It's super-kludge, though.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found