Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Error messages point to wrong line number in if-elsif construct

by Sandy (Curate)
on Dec 21, 2004 at 22:02 UTC ( #416633=perlquestion: print w/replies, xml ) Need Help??
Sandy has asked for the wisdom of the Perl Monks concerning the following question:

This really isn't a question, and I'm not sure if it is the correct place to post, but nonetheless...

While looking at node Resetting variables I was mislead by the line number of the error message.

It appears that when there is an if(){...}elsif(){...} construct that contains an error with the elsif part, the line number points to the line of the if statement.

Examples are better than words:

use strict; use warnings; # demonstration of error message my $test = ""; my $abc = "abc"; if ($abc =~ /xyz/) { # <- LINE 7 print "nothing\n"; } #elsif ($abc =~ /[$test]/) { if ($abc =~ /[$test]/) { # <- LINE 11 print "passed 1st condition\n"; }
Result ... error is at line 11 ...
Unmatched [ before HERE mark in regex m/[ << HERE ]/ at D:\try3.pl lin +e 11.

But

use strict; use warnings; # demonstration of error message my $test = ""; my $abc = "abc"; if ($abc =~ /xyz/) { # <- LINE 7 print "nothing\n"; } elsif ($abc =~ /[$test]/) { #if ($abc =~ /[$test]/) { # <- LINE 11 print "passed 1st condition\n"; }
Result ... error is at line 7 ...
Unmatched [ before HERE mark in regex m/[ << HERE ]/ at D:\try3.pl lin +e 7.

Very confusing. I haven't seen any documentation on this behaviour.

Sandy

Replies are listed 'Best First'.
Re: Error messages point to wrong line number in if-elsif construct
by cchampion (Curate) on Dec 21, 2004 at 22:29 UTC

    Have a look at if-elsif weirdness (and the explanation at Re: if-elsif weirdness) where the same issue was addressed some time ago.

    It looks like it's either a well known bug or a behavior which most of the people are happy living with. :)

Re: Error messages point to wrong line number in if-elsif construct
by gaal (Parson) on Dec 21, 2004 at 22:32 UTC
Re: Error messages point to wrong line number in if-elsif construct
by ikegami (Pope) on Dec 21, 2004 at 22:31 UTC
    elsif doesn't start a statement. It's part of an if statement. In this case, it's telling you that the error is in the statement that started at line 7. I don't know why it's not more precise, but I'm sure changing it would involve extreme changes to perl's guts.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://416633]
Approved by Old_Gray_Bear
Front-paged by dbwiz
help
Chatterbox?
[holli]: Corion: 25$ deposit bottle from Lidl :)
[holli]: bullshit. cents not $
[ambrus]: holli: wait, plastic or glass bottle?
holli switches OS
[holli]: Lidl only sells plastic
[Corion]: holli: Oooh, a twister top - so yes, that's a worse state of mind than it sounded ;)
[holli]: and since I outed myself anyway as a horrible person, to top it off, the bottle contains beer. alcohol free beer
[Discipulus]: nowaday such beers have also a good taste

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (14)
As of 2017-09-25 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (280 votes). Check out past polls.

    Notices?