perl

 view release on metacpan or  search on metacpan

pod/perl5100delta.pod  view on Meta::CPAN

    /x

PCRE users should note that Perl's recursive regex feature allows
backtracking into a recursed pattern, whereas in PCRE the recursion is
atomic or "possessive" in nature.  As in the example above, you can
add (?>) to control this selectively.  (Yves Orton)

=item Named Capture Buffers

It is now possible to name capturing parenthesis in a pattern and refer to
the captured contents by name. The naming syntax is C<< (?<NAME>....) >>.
It's possible to backreference to a named buffer with the C<< \k<NAME> >>
syntax. In code, the new magical hashes C<%+> and C<%-> can be used to
access the contents of the capture buffers.

Thus, to replace all doubled chars with a single copy, one could write

    s/(?<letter>.)\k<letter>/$+{letter}/g

Only buffers with defined contents will be "visible" in the C<%+> hash, so
it's possible to do something like

    foreach my $name (keys %+) {
        print "content of buffer '$name' is $+{$name}\n";
    }

The C<%-> hash is a bit more complete, since it will contain array refs
holding values from all capture buffers similarly named, if there should
be many of them.

C<%+> and C<%-> are implemented as tied hashes through the new module
C<Tie::Hash::NamedCapture>.

Users exposed to the .NET regex engine will find that the perl
implementation differs in that the numerical ordering of the buffers
is sequential, and not "unnamed first, then named". Thus in the pattern

   /(A)(?<B>B)(C)(?<D>D)/

$1 will be 'A', $2 will be 'B', $3 will be 'C' and $4 will be 'D' and not
$1 is 'A', $2 is 'C' and $3 is 'B' and $4 is 'D' that a .NET programmer
would expect. This is considered a feature. :-) (Yves Orton)

=item Possessive Quantifiers

Perl now supports the "possessive quantifier" syntax of the "atomic match"
pattern. Basically a possessive quantifier matches as much as it can and never
gives any back. Thus it can be used to control backtracking. The syntax is
similar to non-greedy matching, except instead of using a '?' as the modifier
the '+' is used. Thus C<?+>, C<*+>, C<++>, C<{min,max}+> are now legal
quantifiers. (Yves Orton)

=item Backtracking control verbs

The regex engine now supports a number of special-purpose backtrack
control verbs: (*THEN), (*PRUNE), (*MARK), (*SKIP), (*COMMIT), (*FAIL)
and (*ACCEPT). See L<perlre> for their descriptions. (Yves Orton)

=item Relative backreferences

A new syntax C<\g{N}> or C<\gN> where "N" is a decimal integer allows a
safer form of back-reference notation as well as allowing relative
backreferences. This should make it easier to generate and embed patterns
that contain backreferences. See L<perlre/"Capture buffers">. (Yves Orton)

=item C<\K> escape

The functionality of Jeff Pinyan's module Regexp::Keep has been added to
the core. In regular expressions you can now use the special escape C<\K>
as a way to do something like floating length positive lookbehind. It is
also useful in substitutions like:

  s/(foo)bar/$1/g

that can now be converted to

  s/foo\Kbar//g

which is much more efficient. (Yves Orton)

=item Vertical and horizontal whitespace, and linebreak

Regular expressions now recognize the C<\v> and C<\h> escapes that match
vertical and horizontal whitespace, respectively. C<\V> and C<\H>
logically match their complements.

C<\R> matches a generic linebreak, that is, vertical whitespace, plus
the multi-character sequence C<"\x0D\x0A">.

=item Optional pre-match and post-match captures with the /p flag

There is a new flag C</p> for regular expressions.  Using this
makes the engine preserve a copy of the part of the matched string before
the matching substring to the new special variable C<${^PREMATCH}>, the
part after the matching substring to C<${^POSTMATCH}>, and the matched
substring itself to C<${^MATCH}>.

Perl is still able to store these substrings to the special variables
C<$`>, C<$'>, C<$&>, but using these variables anywhere in the program
adds a penalty to all regular expression matches, whereas if you use
the C</p> flag and the new special variables instead, you pay only for
the regular expressions where the flag is used.

For more detail on the new variables, see L<perlvar>; for the use of
the regular expression flag, see L<perlop> and L<perlre>.

=back

=head2 C<say()>

say() is a new built-in, only available when C<use feature 'say'> is in
effect, that is similar to print(), but that implicitly appends a newline
to the printed string. See L<perlfunc/say>. (Robin Houston)

=head2 Lexical C<$_>

The default variable C<$_> can now be lexicalized, by declaring it like
any other lexical variable, with a simple

    my $_;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.474 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )