From: Matt S Trout Date: Mon, 3 Jan 2011 14:28:15 +0000 (+0000) Subject: make anon subs work X-Git-Tag: v1.001000~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FEval-WithLexicals.git;a=commitdiff_plain;h=4a3d69ab3eeaa2c4383a988cf6cbf3f0c7fe6073 make anon subs work --- diff --git a/Changes b/Changes index 1bb23ce..5b1fd41 100644 --- a/Changes +++ b/Changes @@ -1,2 +1,4 @@ + - Filter out lexicals called '&' since these are anon subs + 1.0.0 2010-12-05 17:04:00 - Initial release (MSTROUT) diff --git a/lib/Eval/WithLexicals.pm b/lib/Eval/WithLexicals.pm index 1a3168f..6571291 100644 --- a/lib/Eval/WithLexicals.pm +++ b/lib/Eval/WithLexicals.pm @@ -92,7 +92,7 @@ sub _eval_do { sub capture_list { my $pad_capture = \&Eval::WithLexicals::Cage::pad_capture; - my @names = map $_->PV, grep $_->isa('B::PV'), + my @names = grep $_ ne '&', map $_->PV, grep $_->isa('B::PV'), svref_2object($pad_capture)->OUTSIDE->PADLIST->ARRAYelt(0)->ARRAY; $Eval::WithLexicals::current_code .= '+{ '.join(', ', map "'$_' => \\$_", @names).' };' diff --git a/t/simple.t b/t/simple.t index d529ac8..e8157ba 100644 --- a/t/simple.t +++ b/t/simple.t @@ -39,4 +39,12 @@ is_deeply( 'No capture of invisible $y' ); +$eval->eval('my $y = sub { $_[0]+1 }'); + +is_deeply( + [ $eval->eval('$y->(2)') ], + [ 3 ], + 'Sub created ok' +); + done_testing;