[Phoenix-pm] Eval Namespace Question
Douglas E. Miles
perlguy at earthlink.net
Mon Aug 7 19:37:20 PDT 2006
Bill Nash wrote:
>
> If you're excited about perl forwarding that namespace into your eval,
> just remember that's a double edged sword. When you're to a point
> where you're thinking about efficiency, or perl starts to core dump
> with frightening regularity, revisit your use of eval.
>
> - billn
>
Well, actually I've been using the code for several years now. No core
dumps. I just had reason to look at it again, realized what was
happening, and wondered how it worked.
> On Mon, 7 Aug 2006, Douglas E. Miles wrote:
>
>> 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;
>>
>> _______________________________________________
>> Phoenix-pm mailing list
>> Phoenix-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/phoenix-pm
>>
More information about the Phoenix-pm
mailing list