Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: if-elsif weirdness

by japhy (Canon)
on May 21, 2003 at 15:36 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-08-02 18:44 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (28 votes). Check out past polls.