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

Re^2: Dynamically Changing Packages w/out Eval

by Ovid (Cardinal)
on Jun 15, 2009 at 06:34 UTC ( [id://771557]=note: print w/replies, xml ) Need Help??


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

Umm, what is it you think that the package statement does in this code?

Because I'm hard pressed to think of anything that it might affect in the code you have posted. The vars are all lexical. So they arent modified at all by the package declaration.

I know that. I'm not that much of a Perl newbie :)

The exact problem I'm trying to solve is the same thing Test::Aggregate does: run arbitrary bits of code and ensure that we don't have namespace pollution. If one chunk of code defines a package variable or creates a subroutine in its namespace, I don't want that clashing with other chunks of code. Hence, the package declaration.

Replies are listed 'Best First'.
Re^3: Dynamically Changing Packages w/out Eval
by demerphq (Chancellor) on Jun 15, 2009 at 12:45 UTC

    Sorry ovid, but the code you posted as an example did not make that very clear*. You basically cannot get rid of namespace pollution in perl. It is impossible. Any piece of code can declare objects in any namespace at any time.

    As far as /useful/ advice, :-), take a look at the top of Benchmark.pm and see how Jarkko did it.

    * Update: i mean it didnt make your question clear. I know you arent a newbie. Remember we drank a fair amount of whiskey together in vienna? ;-)

    ---
    $world=~s/war/peace/g

Re^3: Dynamically Changing Packages w/out Eval
by shmem (Chancellor) on Jun 22, 2009 at 12:05 UTC

    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://771557]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found