in reply to limiting scope of 'eval'??

I just wanted to point out that I couldn't get any of the posted answers to work. Perhaps its a flaw in the way I implemented them so below is the code i tried and its output.

use strict; use warnings; my $test = q[print $a;]; my $a = "doesn't stop me\n"; sub eval_it { eval shift; } { package safezone; sub eval_it { eval shift; } } warn "regular eval"; eval $test; warn "eval_it"; eval_it $test; warn "safezone::eval_it"; safezone::eval_it $test; warn "Prepend package"; eval "package safezone; $test"; __DATA__ regular eval at eval.pl line 18. doesn't stop me eval_it at eval.pl line 21. doesn't stop me safezone::eval_it at eval.pl line 24. doesn't stop me Prepend package at eval.pl line 27. doesn't stop me

___________
Eric Hodges

Replies are listed 'Best First'.
Re^2: limiting scope of 'eval'??
by Crackers2 (Parson) on Aug 22, 2004 at 14:03 UTC

    For your implementation of BrowserUK's solution, you forgot the local line that's needed to hide the variables:

    { package safezone; local $a; sub eval_it { eval shift; } }

    Of course since you need a declaration for all variables you want to hide, this can get cumbersome rather quickly, but it does work

    Update: This blows up with a Can't localize lexical variable $a at x line 12. Using my $a instead of local $a does seem to work here though.

Re^2: limiting scope of 'eval'??
by Anonymous Monk on Aug 23, 2004 at 15:14 UTC
    You need to move the sub declarations above the my declarations.