develooper Front page | perl.perl5.porters | Postings from June 2008

??{ } closure semantics

Thread Next
Yuval Kogman
June 4, 2008 18:59
??{ } closure semantics
Message ID:
It appears that ??{ } behaves like a closure only on it's first
invocation, and not at the time that is actually
compiled/encountered lexically:

use Test::More 'no_plan';

use re 'eval';

    use re "eval";

    my $foo;

    my $bar  = qr{ (a+) ((??{$foo})) }x;

    $foo = qr{ b+ }x;

    sub match {
		# uncommenting this retains $foo, and makes the tests pass
		#{ no warnings 'void'; $foo; } # force capture of interpolated lexical
        $_[0] =~ m{ $bar }x;
        return ($1, $2);

	# uncommenting this also retains $foo
	#is_deeply([ match("aaabb") ], [qw(aaa bb)]);

is_deeply([ match("aaabb") ], [qw(aaa bb)]);

This test fails because $foo is uninitialized in the execution of $bar.

However, if $bar is executed once before $foo's scope is left then it will be
retained, and likewise if the subroutine forces $foo to be captured then it is
also retained, and the matching succeeds.

I'm guessing this is a bug?

  Yuval Kogman <>  0xEBD27418

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About