Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Dynamically Changing Packages w/out Eval

by shmem (Chancellor)
on Jun 22, 2009 at 12:05 UTC ( [id://773589]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Dynamically Changing Packages w/out Eval
in thread Dynamically Changing Packages w/out Eval

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://773589]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2025-06-23 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.