Perhaps copying and restoring the symbol table does what you want?
use Data::Dumper;
package Foo;
$foo="bar";
sub quux { print "\$foo is '$foo'\n" }
package main;
my %Foo_save = %Foo::;
print "-1-\n",Dumper \%Foo::;
eval "\$Foo::bar = q{quux}";
print "-2-\n",Dumper \%Foo::;
print "-3-\n",Dumper \%Foo_save;
%Foo:: = %Foo_save;
print "-4-\n",Dumper \%Foo::;
Foo::quux();
eval "print \"but Foo::bar is '\$Foo::bar'\\n\"";
__END__
-1-
$VAR1 = {
'quux' => *Foo::quux,
'foo' => *Foo::foo
};
-2-
$VAR1 = {
'bar' => *Foo::bar,
'quux' => *Foo::quux,
'foo' => *Foo::foo
};
-3-
$VAR1 = {
'quux' => *Foo::quux,
'foo' => *Foo::foo
};
-4-
$VAR1 = {
'quux' => *Foo::quux,
'foo' => *Foo::foo
};
$foo is 'bar'
but Foo::bar is ''
That fails if eval'ed code populates a scalar slot of an already present typeglob - if, for instance, the subroutine in Foo was named 'bar', then $Foo::bar would retain the value set in the string eval. Hmm.. maybe Clone would help?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|