http://www.perlmonks.org?node_id=250788

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.
$^N
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.