Permalink
Browse files

compile each thing in a separate package, to avoid leakage

  • Loading branch information...
1 parent 0fb2ea4 commit 794dc9df98d2aaf2f143f32ac7dfa42fa46ce07e @doy committed Aug 2, 2011
Showing with 34 additions and 13 deletions.
  1. +11 −7 lib/Eval/Closure.pm
  2. +23 −6 t/compiling-package.t
View
@@ -207,14 +207,18 @@ sub _clean_eval_closure {
}
}
+$Eval::Closure::SANDBOX_ID = 0;
+
sub _clean_eval {
- package # hide from PAUSE
- Eval::Closure::Sandbox;
- local $@;
- local $SIG{__DIE__};
- my $compiler = eval $_[0];
- my $e = $@;
- return [ $compiler, $e ];
+ $Eval::Closure::SANDBOX_ID++;
+ return eval <<EVAL;
+package Eval::Closure::Sandbox_$Eval::Closure::SANDBOX_ID;
+local \$@;
+local \$SIG{__DIE__};
+my \$compiler = eval \$_[0];
+my \$e = \$@;
+[ \$compiler, \$e ];
+EVAL
}
sub _make_compiler_source {
View
@@ -5,13 +5,30 @@ use Test::More;
use Eval::Closure;
-my $code = eval_closure(
- source => 'no strict "refs"; sub { keys %{__PACKAGE__ . "::"} }',
-);
+{
+ my $code = eval_closure(
+ source => 'no strict "refs"; sub { keys %{__PACKAGE__ . "::"} }',
+ );
-# defining the sub { } creates __ANON__, calling 'no strict' creates BEGIN
-my @stash_keys = grep { $_ ne '__ANON__' && $_ ne 'BEGIN' } $code->();
+ # defining the sub { } creates __ANON__, calling 'no strict' creates BEGIN
+ my @stash_keys = grep { $_ ne '__ANON__' && $_ ne 'BEGIN' } $code->();
-is_deeply([@stash_keys], [], "compiled in an empty package");
+ is_deeply([@stash_keys], [], "compiled in an empty package");
+}
+
+{
+ # the more common case where you'd run into this is imported subs
+ # for instance, Bread::Board::as vs Moose::Util::TypeConstraints::as
+ my $c1 = eval_closure(
+ source => 'no strict "vars"; sub { ++$foo }',
+ );
+ my $c2 = eval_closure(
+ source => 'no strict "vars"; sub { --$foo }',
+ );
+ is($c1->(), 1);
+ is($c1->(), 2);
+ is($c2->(), -1);
+ is($c2->(), -2);
+}
done_testing;

0 comments on commit 794dc9d

Please sign in to comment.