develooper Front page | perl.perl5.porters | Postings from September 2001

Re: [ID 20010930.005] interpolated BEGIN oddities

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
September 30, 2001 13:00
Subject:
Re: [ID 20010930.005] interpolated BEGIN oddities
Message ID:
20010930160006.D11413@blackrider
On Sun, Sep 30, 2001 at 01:56:35PM +0100, Hugo van der Sanden wrote:
> Under perls at least from 5.005_03 to blead@12163, I get this oddity:
> 
>   crypt% ./perl  -wle 'print qq;@{[ BEGIN { print "BEGIN" } ]};'
>   BEGIN
>   Can't call method "BEGIN" without a package or object reference at -e line 1.

Putting aside the utility of trying to define a BEGIN block in a
string for a moment...

You've basically confused the hell out of Perl.

It thinks you're doing this:

    print "@{[do { print 'BEGIN' }->BEGIN];}";

However, extending this a little further, I tried to see if I could
actually get that to call a method:

$ perl -wle 'print qq;@{[ foo { bless {} } ]};'
Can't locate object method "foo" via package "main" (perhaps you forgot to load "main"?) at -e line 1.

which deparses as

    print "@{[do { bless {} }->foo];}";

If we define a foo() function:

$ perl -wle 'sub foo { 42 }  print qq;@{[ foo { bless {} } ]};'
Odd number of elements in hash assignment at -e line 1.
42

it changes the whole characteristic of the parsing:

    sub foo { 42; }
    print "@{[ foo({ bless({}) }) ];}";

BUT if we do it the other way around, no warning:

$ perl -wle 'print qq;@{[ foo { bless {} } ]};;  sub foo { 42 }'
42

    print "@{[do { bless {} }->foo];}";

    sub foo { 42; }

and it parses like it did originally.

Now that's a problem.


-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
Any sufficiently encapsulated hack is no longer a hack.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About