From: Matt S Trout Date: Sat, 4 Dec 2010 17:31:17 +0000 (+0000) Subject: handle inner scope lexicals X-Git-Tag: v1.000000~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FEval-WithLexicals.git;a=commitdiff_plain;h=40d8277ffc4b65605b2c972b269dfcda92c88fca handle inner scope lexicals --- diff --git a/lib/Eval/WithLexicals.pm b/lib/Eval/WithLexicals.pm index 1517ef7..c6f257c 100644 --- a/lib/Eval/WithLexicals.pm +++ b/lib/Eval/WithLexicals.pm @@ -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 { diff --git a/t/simple.t b/t/simple.t index e4316f3..22ea7c4 100644 --- a/t/simple.t +++ b/t/simple.t @@ -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;