Error message line number in subs

Discussion in 'Perl Misc' started by laszlo, Aug 29, 2003.

  1. laszlo

    laszlo Guest

    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)
     
    laszlo, Aug 29, 2003
    #1
    1. Advertising

  2. laszlo

    Anno Siegel Guest

    laszlo <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, Aug 29, 2003
    #2
    1. Advertising

  3. '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;

    --

    Matt

    "laszlo" <> wrote in message
    news:...
    > 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)
     
    Matt Churchyard, Aug 29, 2003
    #3
  4. laszlo wrote:
    >
    > 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
     
    Michael P. Broida, Aug 29, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    577
    Ben Holness
    Oct 8, 2003
  2. Bolin

    Subs and lists

    Bolin, Nov 2, 2003, in forum: Perl
    Replies:
    6
    Views:
    572
  3. Christina N
    Replies:
    0
    Views:
    417
    Christina N
    Jun 17, 2004
  4. Tom Kaminski [MVP]

    Line number on error message?

    Tom Kaminski [MVP], Jul 21, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    474
    Shan Plourde
    Jul 23, 2004
  5. Hugo
    Replies:
    10
    Views:
    1,343
    Matt Humphrey
    Oct 18, 2004
Loading...

Share This Page