handle inner scope lexicals
Matt S Trout [Sat, 4 Dec 2010 17:31:17 +0000 (17:31 +0000)]
lib/Eval/WithLexicals.pm
t/simple.t

index 1517ef7..c6f257c 100644 (file)
@@ -37,8 +37,8 @@ ${to_eval}
 ;sub Eval::WithLexicals::Cage::pad_capture { }
 BEGIN { Eval::WithLexicals::Util::capture_list() }
 sub Eval::WithLexicals::Cage::grab_captures {
-  no warnings 'closure'; no strict 'refs';
-  package Eval::WithLexicals::Cage;!;
+  no warnings 'closure'; no strict 'vars';
+  package Eval::WithLexicals::VarScope;!;
   $self->_eval_do(\$current_code, $self->lexicals);
   my @ret;
   my $ctx = $self->context;
@@ -51,11 +51,24 @@ sub Eval::WithLexicals::Cage::grab_captures {
   }
   $self->lexicals({
     %{$self->lexicals},
-    %{Eval::WithLexicals::Cage::grab_captures()}
+    %{$self->_grab_captures},
   });
   @ret;
 }
 
+sub _grab_captures {
+  my ($self) = @_;
+  my $cap = Eval::WithLexicals::Cage::grab_captures();
+  foreach my $key (keys %$cap) {
+    my ($sigil, $name) = $key =~ /^(.)(.+)$/;
+    my $var_scope_name = $sigil.'Eval::WithLexicals::VarScope::'.$name;
+    if ($cap->{$key} eq eval "\\${var_scope_name}") {
+      delete $cap->{$key};
+    }
+  }
+  $cap;
+}
+
 sub _eval_do {
   my ($self, $text_ref) = @_;
   local @INC = (sub {
index e4316f3..22ea7c4 100644 (file)
@@ -27,4 +27,10 @@ is_deeply(
   'Inner scope plus lexical ok'
 );
 
+is_deeply(
+  [ $eval->eval('{ my $y = 0 }; $x') ],
+  [ 1 ],
+  'Inner scope and other lexical ok'
+);
+
 done_testing;