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:$^N/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 | |
Re: $+ versus $^N
by Enlil (Parson) on Apr 16, 2003 at 05:48 UTC | |
by diotalevi (Canon) on Apr 16, 2003 at 13:09 UTC | |
Re: $+ versus $^N
by BrowserUk (Patriarch) on Apr 16, 2003 at 05:51 UTC |
Back to
Seekers of Perl Wisdom