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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|