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

Re: [ID 20010930.005] interpolated BEGIN oddities

Thread Previous | Thread Next
Michael G Schwern
September 30, 2001 13:00
Re: [ID 20010930.005] interpolated BEGIN oddities
Message ID:
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" } ]};'
>   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.

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 }'

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

    sub foo { 42; }

and it parses like it did originally.

Now that's a problem.


Michael G. Schwern   <>
Perl6 Quality Assurance     <>	     Kwalitee Is Job One
Any sufficiently encapsulated hack is no longer a hack.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About