http://qs1969.pair.com?node_id=11140615


in reply to Refer by relative way to regex group (eg. the last one)

You do not say how your code fails, but if you add use strict; to your example you get

$ perl -Mstrict -Mwarnings -e 'my $word; "foobar" =~ /(oo)(bar)(?{$word=${-1}})/'
Can't use string ("-1") as a SCALAR ref while "strict refs" in use at -e line 1.

Suggestions:

$ perldoc -v '@-'
$ perldoc -v '@+'

Or, under at least Perl 5.10, you could use named captures.

Replies are listed 'Best First'.
Re^2: Refer by relative way to regex group (eg. the last one)
by LanX (Sage) on Jan 19, 2022 at 17:14 UTC
    > Suggestions:

    sigh...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    update

    DB<29> "ab" =~ /^((.)(.))(?{say $+;say $^N; say "$1 $2 $3\n@{^CAPTUR +E}"})/ b ab ab a b ab a b