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

Re: ??{ } closure semantics

Thread Previous | Thread Next
From:
Rafael Garcia-Suarez
Date:
June 5, 2008 01:23
Subject:
Re: ??{ } closure semantics
Message ID:
b77c1dce0806050123s22ddd23ch68b0ae6cbc551834@mail.gmail.com
2008/6/5 Yuval Kogman <nothingmuch@woobling.org>:
> 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?

That's known. I think Dave has plans to fix that (but it won't be
completely trivial).

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About