[Phoenix-pm] Eval Namespace Question

Douglas E. Miles perlguy at earthlink.net
Mon Aug 7 13:59:24 PDT 2006


Hey all!

I'm having fun hacking Perl for a change, and I just realized I'd been 
using a feature that I don't completely understand.  man perlfunc says 
this about eval:

In the first form, the return value of EXPR is parsed and exe-
cuted as if it were a little Perl program.  The value of the
expression (which is itself determined within scalar context)
is first parsed, and if there weren't any errors, executed in
the lexical context of the current Perl program, so that any
variable settings or subroutine and format definitions remain
afterwards.

This means that in my code below, that $bar is in scope within the 
eval.  What blows me away is that $foo::foo is also in scope because of 
the "package foo" statement in $eval_string.  Does anyone know how perl 
accesses both namespaces simultaneously without specifying the namespace 
in each variable name.  Feel free to laugh at me if I'm missing 
something basic. :)  The cool thing is that this behavior is exactly 
what I need.  I'd just like to understand how it works (and hope that it 
isn't a misfeature that might be removed in the future).  Thanks!


#!/usr/bin/perl

package foo;

$foo = "FOO!";

package bar;

my $bar = "BAR!";

my $perl_code = 'print "$bar $foo\n"';
print "$perl_code\n";

my $eval_string = "package foo;\n$perl_code";
eval $eval_string;



More information about the Phoenix-pm mailing list