in reply to Do a named regex group in 5.8?

The (?{}) and (??{}) constructs are available for use in perl5.8, but you'd have to ask yourself if such ugliness is really worth it:

$ perl -MData::Dumper -e 'q(abc de f abcdef) =~ m/(?<x>\w{3}).*\k<x>/ +and print Dumper {%+};' $ perl5.8.8 -MData::Dumper -e 'q(abc de f abcdef) =~ m/(?{$+{x} = pos} +)(\w{3})(?{$+{x} = substr($_, $+{x}, -$+{x}+pos)}).*(??{$+{x}})/ and +print Dumper {%+};'

Replies are listed 'Best First'.
Re^2: Do a named regex group in 5.8?
by AnomalousMonk (Archbishop) on Feb 09, 2016 at 07:37 UTC

    This can be made slightly less ugly by

    c:\@Work\Perl>perl -wMstrict -le "use Data::Dumper qw(Dumper); ;; print qq{perl version $]}; ;; q(abc de f abcdef) =~ m/ ( (\w{3}) (?{ $+{x} = $^N; }) .* (??{ $+{x}; }) ) /x and print qq{\$1 '$1' \n}, Dumper \%+; " perl version 5.008009 $1 'abc de f abc' $VAR1 = { 'x' => 'abc' };
    if it's really necessary to have named backreferences in the regex via a hash.

    Otherwise, I'm not sure I see its advantage over something like

    c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; q(abc de f abcdef) =~ m/ ( (\w{3}) .* \2 ) /x and print qq{\$1 '$1'}; " perl version 5.008009 $1 'abc de f abc'
    or perhaps like
    c:\@Work\Perl>perl -wMstrict -le "print qq{perl version $]}; ;; my $match = my ($whole, $first_part) = q(abc de f abcdef) =~ m/ ( (\w{3}) .* \2 ) /x ;; print qq{first part '$first_part' whole '$whole'} if $match; " perl version 5.008009 first part 'abc' whole 'abc de f abc'


    Give a man a fish:  <%-{-{-{-<