Bewildering perl parser bug

K

kj

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
 
K

kj

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
 
X

xhoster

kj said:
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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top