in reply to Re^2: Eval doesn't see lexicals
in thread Eval doesn't see lexicals

package EvilEval; use Class::Std; sub _serial() { '1234567890' }; my %serial_of :ATTR; sub BUILD { my ( $this, $ident, $arg_ref ) = @_; $serial_of{$ident} = _serial(); } sub evil { #use Data::Dumper; print Dumper \%serial_of; my $string = 'use Data::Dumper; print Dumper \%serial_of'; eval $string; } 1;
Meanwhile, in a nearby perl script ...
use EvilEval; my $evil = EvilEval->new(); $evil->evil();
This demonstrates the problem, at least on my setup. If all the code is in the same file there is no problem - even if it is declared in different packages. When the caller is in a different file, the problem occurs.

Replies are listed 'Best First'.
Re^4: Eval doesn't see lexicals
by tilly (Archbishop) on Nov 02, 2005 at 02:26 UTC
    This is a version of the problem that I described in Re: Eval doesn't see lexicals. (End of file is a block exit and Perl does cleanup.) The simplest solution is to insert the following in EvilEval.pm.
    # Fool Perl's reference counting into keeping %serial_of alive sub do_not_cleanup_serial_of { %serial_of; }