Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: eval('__PACKAGE__') is always main?!!!

by ikegami (Pope)
on Dec 23, 2004 at 21:09 UTC ( #417210=note: print w/replies, xml ) Need Help??

in reply to eval('__PACKAGE__') is always main?!!!

The docs say "In the first form," eval EXPR, "the return value of EXPR is parsed and executed as if it were a little Perl program."

Perl programs are put into main until a package statement is encountered, so the documentation is consistent with the behaviour (although an explicit statement about this behaviour would be favourable).

Which means the following is wrong?

sub moo { print("moooooain\n"); } package cow; sub moo { print("moooOoooOOoooo\n"); } # <--- prints this eval 'moo();';

There's definitely a bug, since __PACKAGE__ does not agree with the function getting called.

do EXPR is consistent with eval EXPR, as stated in do's docs:

> echo "print(__PACKAGE__, $/);" > > perl -e "package foo; do '';" main

Of course, eval EXPR is inconsistent with eval BLOCK which "parsed--and executed within the context of the current Perl program".

Replies are listed 'Best First'.
Re^2: eval('__PACKAGE__') is always main?!!!
by gaal (Parson) on Dec 24, 2004 at 07:01 UTC
    (Following up on a brief CB /msg exchange, that may be interesting to other monks.)

    Speaking of documentation only here: I thought "executed within the context of the current Perl program" ought to have trumped eval EXPR's move to main, but as ikegami notes, the statement about context applies only to eval BLOCK.

    But eval BLOCK of course does share some (a lot of) state with the main program; there's no new perl interpreter constructed, and the following does change the value of $x in the main program:

    perl -le '$x = 42; eval q($x = 6*9); print $x'    # prints "54"

    The first thing that needs fixing in this case appears to be the docs.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://417210]
[LanX]: I have my doubts, IMHO its a workaround if the glyph is missing
[LanX]: (they are pronounced differently)
[choroba]: perl -Mutf8 -Mfeature=fc -wE 'say fc "straße" eq fc "strasse"'
[choroba]: returns 1 for me
[choroba]: are they?
[LanX]: strict rules : the a is long in "straße" and short in "strasse"
[choroba]: So the "a" is different, not the "s" ;-)
[LanX]: people knowing that it's a workaround will pronounce it right
[LanX]: well
[LanX]: Maße and Masse are two very different words.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2018-03-21 15:47 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (268 votes). Check out past polls.