diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

In Finding the right $<*digit*> capture variable Enlil mentioned $^N which I hadn't seen before. Since looking at it I'm not quite sure when one is more useful than other especially since they appear to have identical functionality. In 5.6.1 I'd use $+ in (?{...}) blocks to work with the value most recently captured by a (...) block. I suppose it might also have uses outside a regex but that appears to be pretty stilted - I'm not sure how it is useful to get the last captured value outside of a regex. Or at least the documentation describes $^N as working rather like I thought that $+ worked. So what is the difference?

For reference:


The text matched by the last bracket of the last successful search pattern. This is useful if you don't know which one of a set of alternative patterns matched. For example:
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
(Mnemonic: be positive and forward looking.) This variable is read-only and dynamically scoped to the current BLOCK.
The text matched by the used group most-recently closed (i.e. the group with the rightmost closing parenthesis) of the last successful search pattern. (Mnemonic: the (possibly) Nested parenthesis that most recently closed.)
This is primarily used inside "(?{...})" blocks for examining text recently matched. For example, to effectively capture text to a variable (in addition to $1, $2, etc.), replace "(...)" with
(?:(...)(?{ $var = $^N }))
By setting and then using $var in this way relieves you from having to worry about exactly which numbered set of parentheses they are.
This variable is dynamically scoped to the current BLOCK.

Replies are listed 'Best First'.
Re: $+ versus $^N
by Zaxo (Archbishop) on Apr 16, 2003 at 05:41 UTC

    The difference is that $^N contains the last closed capture while $+ contains the last opened one. That is, $^N looks to right parens and $+ to left ones. That makes a difference for nested captures.

    From the command line in 5.8.0:

    $ perl -e'$_=foobar;/(oo(ba)r)/;print $+,$/' ba $ perl -e'$_=foobar;/(oo(ba)r)/;print $^N,$/' oobar $

    After Compline,

Re: $+ versus $^N
by Enlil (Parson) on Apr 16, 2003 at 05:48 UTC
    Here is some code to show the difference (perlvar as you quoted gives a good description):
    use strict; use warnings; my $plus; my $N; my $text = "abc123def"; my $text_match = qr/((\D+)(\d+))(?{ $plus = $+;$N = $^N })(\D+)/; $text =~ $text_match; print "$plus, $N\n"; $text_match = qr/(\D+)(\d+)(?{ $plus = $+;$N = $^N })(\D+)/; $text =~ $text_match; print "$plus, $N\n";

    update:oh yeah the results:

    __END__ 123, abc123 123, 123

      The description may have been good but I wasn't up to the task of noticing the difference. Thank you all for clarifying this.

Re: $+ versus $^N
by BrowserUk (Pope) on Apr 16, 2003 at 05:51 UTC

    This demonstrates the (a?) difference. How you interprete the results relative to the descriptions I can't quite figure.

    perl> $s = join'','a'..'z' perl> 1 while $s =~ m[((.).)(?{ print "$^N:$+\n"; })]g ab:a cd:c ef:e gh:g ij:i kl:k mn:m op:o qr:q st:s uv:u wx:w yz:y perl> 1 while $s =~ m[((..).)(?{ print "$^N:$+\n"; })]g abc:ab def:de ghi:gh jkl:jk mno:mn pqr:pq stu:st vwx:vw

    One interesting difference in the description that had me salivating breifly was that $+ is explicity defined read-only whereas (in your snippet) $^N it's not but whilst attempting to assign to it doesn't result in an error, it also has no effect on the bound string.

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.