This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge re_eval jumbo fix branch into blead
authorDavid Mitchell <davem@iabyn.com>
Thu, 14 Jun 2012 08:21:29 +0000 (09:21 +0100)
committerDavid Mitchell <davem@iabyn.com>
Thu, 14 Jun 2012 08:21:29 +0000 (09:21 +0100)
commiteb58a7e122f6228be1255f72e5f8e9834727ffdf
tree04e977259263b614960ef78b0a8fb32ba8280a4c
parent3630f57ef8a29a646a6848f4e93d25ac47093a3c
parentdb703679cab524f30dcaf14188afc73260ed21e7
Merge re_eval jumbo fix branch into blead

This re_eval branch contains around 130 commits that collectively
reimplement the /(?{})/ mechanism. See the individual commits
and the changes to pod/* for more details, but the main highlights are:

=item *

Code blocks within patterns are now parsed in the same pass as the
surrounding code; in particular it is no longer necessary to have balanced
braces: this now works:

    /(?{  $x='{'  })/

This means that this error message is longer generated:

    Sequence (?{...}) not terminated or not {}-balanced in regex

but a new error may be seen:

    Sequence (?{...}) not terminated with ')'

In addition, literal code blocks within run-time patterns are only
compiled once, at perl compile-time:

    for my $p (...) {
        # this 'FOO' block of code is compiled once, at the same time as
        # the surrounding 'for' loop
        /$p{(?{FOO;})/;
    }

=item *

Lexical variables are now sane as regards scope, recursion and closure
behaviour. In particular, C</A(?{B})C/> behaves (from a closure viewpoint)
exactly like C</A/ && do { B } && /C/>, while  C<qr/A(?{B})C/> is like
C<sub {/A/ && do { B } && /C/}>. So this code now works how you might
expect, creating three regexes that match 1,2, and 3:

    for my $i (0..2) {
        push @r, qr/^(??{$i})$/;
    }
    "1" =~ $r[1]; # matches

=item *

The C<use re 'eval'> pragma is now strictly only required for code blocks
defined at runtime; in particular in the following, the text of the $r
pattern is still interpolated into the new pattern and recompiled, but
the individual compiled code-blocks within $r are reused rather than being
recompiled, and C<use re 'eval'> isn't needed any more:

    my $r = qr/abc(?{....})def/;
    /xyz$r/;

=item *

Flow control operators no longer crash. Each code block runs in a new
dynamic scope, so C<next> etc. will not see any enclosing loops and
C<caller> will not see any calling subroutines. C<return> returns a value
from the code block, not from any enclosing subroutine.

=item *

Perl normally caches the compilation of run-time patterns, and doesn't
recompile if the pattern hasn't changed; but this is now disabled if
required for the correct behaviour of closures; for example:

    my $code = '(??{$x})';
    for my $x (1..3) {
        $x =~ /$code/; # recompile to see fresh value of $x each time
    }

=item *

C</msix> and C<(?msix)> etc. flags are now propagated into the return
value from C<(??{})>; this now works:

    "AB" =~ /a(??{'b'})/i;

=item *

Warnings and errors will appear to come from the surrounding code (or for
run-time code blocks, from an eval) rather than from an C<re_eval>:

    use re 'eval'; $c = '(?{ warn "foo" })'; /$c/;
    /(?{ warn "foo" })/;

formerly gave:

    foo at (re_eval 1) line 1.
    foo at (re_eval 2) line 1.

and now gives:

    foo at (eval 1) line 1.
    foo at /tmp/foo line 2.

=item *

In the pluggable regex API, the regexp_engine struct has acquired a new
field C<op_comp>, which is currently just for perl's internal use, and
should be initialised to NULL by other regexp plugin modules.