Error message line number in subs

L

laszlo

I have an interactive perl cgi programlet, which allows some graphics
for interactively typed and executed perl programs:

http://lzkiss.netfirms.com/cgi-bin/igperl/igp.pl?name=test

Until I used perl 5.6 the programlet below didn't gave any error
message for the "while (true) {" statement. In my localhost I updated
to ActiveState 5.8.0 version, and the bareword true causes an error in
while. However the associated line number is not the real line number,
but the last line of the sub, so line 9 instead of the actual line
number of the error. If I modify the program to e.g. "while (true ==
1) " there is an error with the proper line number. Apparently the
program precompiles the condition in the sub and reports the error as
it would be at the last line of the sub.

Is there anything I can do to get the correct error number (I list $@
after calling eval for the programlet; the head with use statements is
called outside the eval function)

use warnings;
use strict "subs";
use strict "refs";
$a = 1;
sub alma {
while (true) {
$a = 2;
}
}

true;

Bareword "true" not allowed while "strict subs" in use in test.func
line 9


(You don't see this error on the above webpage because the public
server is using 5.6)
 
A

Anno Siegel

laszlo said:
I have an interactive perl cgi programlet, which allows some graphics
for interactively typed and executed perl programs:

http://lzkiss.netfirms.com/cgi-bin/igperl/igp.pl?name=test

Until I used perl 5.6 the programlet below didn't gave any error
message for the "while (true) {" statement. In my localhost I updated
to ActiveState 5.8.0 version, and the bareword true causes an error in
while. However the associated line number is not the real line number,
but the last line of the sub, so line 9 instead of the actual line
number of the error. If I modify the program to e.g. "while (true ==
1) " there is an error with the proper line number. Apparently the
program precompiles the condition in the sub and reports the error as
it would be at the last line of the sub.

Line numbers in Perl error messages have never been 100 % accurate. It
was worse in Perl 5.5 than in 5.6 and got still better in 5.8, but
occasionally the line number is off. Usually this happens with multi-
line statements, where the reported line number is the first line of the
statement while the error happened a few lines down in the same statement.

Looking at how complicated the lexing/parsing process in Perl is, it's
amazing it even has an idea where the stuff it's compiling comes from.
Is there anything I can do to get the correct error number (I list $@
after calling eval for the programlet; the head with use statements is
called outside the eval function)

Short of patching the Perl kernel, there is little you can do. Learn to
live with Perl's quirks.
use warnings;
use strict "subs";
use strict "refs";
$a = 1;
sub alma {
while (true) {
$a = 2;
}
}

true;

Bareword "true" not allowed while "strict subs" in use in test.func
line 9

Are you sure the first line of your code is what Perl sees as line 1?
I have never seen Perl claim the closing bracket of a sub as an error.

What's more interesting is that apparently some Perls accept a bareword
as a while-condition. 5.8.0 does it too, apparently 5.6 doesn't. This
is a minor bug that has better expectations of being fixed.

FWIW, 5.8.0 reports all errors with the correct line number. As mentioned,
it doesn't report "while ( true ) {".

Anno
 
M

Matt Churchyard

'true' is not a keyword in perl and is being interpreted as a bareword

IMHO it is a bad idea to use barewords anywhere (the program wont run at all
under 'use strict)',
and I would replace both instances of 'true' with 1 or define true as a
constant - ie

use constant true => 1;
 
M

Michael P. Broida

laszlo said:
use warnings;
use strict "subs";
use strict "refs";
$a = 1;
sub alma {
while (true) {
$a = 2;
}
}

true;

Bareword "true" not allowed while "strict subs" in use in test.func
line 9

Seeing the error as being on the END line of the "while"
statement makes SOME sense to me, based on my experience
with other languages.

In all the C or C++ compilers I've seen the test for the
"while" statement is actually done at the END of the loop.
Yes, the test has to be PERFORMED before the first pass, so a
jump to the end of the loop is placed just before the loop,
thus causing the test to be done before the code inside the
loop is executed. (A "do-while" would omit the initial
"jump".)

Sounds confusing, so here's a (hopefully helpful) diagram
of what I'm describing: (sorry if it displays oddly for you)

C/C++ code compiled implementation
--------------- -----------------------
<none> jump to ZZZ
while (CCC) AAA:
{
<dosomething> <dosomething>
} ZZZ: if CCC, go to AAA

That's the way I've always seen it in the output of the C or
C++ compiler (and other languages with similar loop structures).
Not pretty, but the compiler writers must figure it gains
them something to do it that way.

So, when an error occurs in the evaluation of "CCC", the
error is reported as being in the line at the END of the
loop because that's where the code really resides.

I don't know if Perl code compiles the same way, but it
could be an explanation for what you're seeing.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top