in reply to Prevent import of subroutines from eval

You can forget the definition of the sub, just put the following before the second eval:
delete $::{foo};

Alternatively, use different packages in the evals. Just prepend

package eval1; # eval2 respectively

to each string.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Prevent import of subroutines from eval
by AnomalousMonk (Archbishop) on Oct 10, 2013 at 22:52 UTC
    ... use different packages in the evals. Just prepend ...

    ++Ooo, that works very nicely!

    c:\@Work\Perl>perl -wMstrict -le "sub foo { print __PACKAGE__, ' foo'; } ;; my $e_str = q{sub foo { print __PACKAGE__, ' foo'; }; foo(); }; ;; for my $n (1 .. 3) { my $p = qq{package Bar$n; }; eval $p . $e_str; } ;; foo(); Bar1::foo(); Bar2::foo(); Bar3::foo(); " Bar1 foo Bar2 foo Bar3 foo main foo Bar1 foo Bar2 foo Bar3 foo
Re^2: Prevent import of subroutines from eval
by Swandog (Initiate) on Oct 11, 2013 at 16:42 UTC

    Something about modifying other people's code programmatically rubs me the wrong way. But given what the original code is doing, maybe I just need to get over it.

    Thanks for the suggestion.

      This does not modify any code. It just adjusts the symbol table.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ