Bewildering perl parser bug

Discussion in 'Perl Misc' started by kj, Dec 14, 2007.

  1. kj

    kj Guest

    The following demo code causes the warning "Useless use of a constant
    in a void context":

    use strict;
    use warnings;

    { ; package ABC; 1; }

    sub foo {}

    foo ABC->xyz, 2;

    __END__

    ....because perl parses the last line as

    ABC->foo->xyz, 2;

    That's annoying enough, but what sends me for a loop is that if I
    change the block

    { ; package ABC; 1 }

    to

    { ; package PQR; 1 }

    now the last line gets parsed as foo( ABC->xyz, 2 ), as any
    reasonable, red-blooded parser would.

    Now, why on earth would that "package ABC" statement confuse the
    parser???

    Of course, in this case I can clue in the parser by changing the
    last line to

    foo( ABC->xyz, 2 );

    but this fix makes me uneasy, because it seems fixing the symptom
    and not the cause, leaving me wondering about what else in the file
    the parser will mess up (in fact this parsing error would have not
    have been visible until runtime if the second argument to foo had
    been a 1 or a 0 instead of a 2, because perl does not protest when
    it finds either of those two values in a void context).

    If there's a way to clue-in the parser that is closer to the source
    of the error, please let me know.

    TIA!

    kj




    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Dec 14, 2007
    #1
    1. Advertising

  2. kj

    kj Guest

    Sorry, I neglected to specify the version of perl I'm using:

    % perl -v

    This is is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi

    ...


    kj


    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    kj, Dec 14, 2007
    #2
    1. Advertising

  3. kj

    Guest

    kj <> wrote:
    > The following demo code causes the warning "Useless use of a constant
    > in a void context":
    >
    > use strict;
    > use warnings;
    >
    > { ; package ABC; 1; }
    >
    > sub foo {}
    >
    > foo ABC->xyz, 2;
    >
    > __END__
    >
    > ...because perl parses the last line as
    >
    > ABC->foo->xyz, 2;
    >
    > That's annoying enough, but what sends me for a loop is that if I
    > change the block
    >
    > { ; package ABC; 1 }
    >
    > to
    >
    > { ; package PQR; 1 }
    >
    > now the last line gets parsed as foo( ABC->xyz, 2 ), as any
    > reasonable, red-blooded parser would.
    >
    > Now, why on earth would that "package ABC" statement confuse the
    > parser???


    See the discussion of indirect object syntax in perldoc perlobj.

    The previously defined subroutine foo primes perl to recognize subroutine
    calls without parentheses. The previously defined package ABC primes perl
    to recognize indirect object syntax calls for ABC. Apparently, the latter
    takes precedence over the former.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Dec 14, 2007
    #3
    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. Bernd Oninger
    Replies:
    0
    Views:
    756
    Bernd Oninger
    Jun 9, 2004
  2. ZOCOR

    XML Parser VS HTML Parser

    ZOCOR, Oct 3, 2004, in forum: Java
    Replies:
    11
    Views:
    810
    Paul King
    Oct 5, 2004
  3. Bernd Oninger
    Replies:
    0
    Views:
    810
    Bernd Oninger
    Jun 9, 2004
  4. Joel Hedlund
    Replies:
    2
    Views:
    506
    Joel Hedlund
    Nov 11, 2006
  5. Guest
    Replies:
    1
    Views:
    679
    Ben Morrow
    Oct 12, 2004
Loading...

Share This Page