Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to $+ versus $^N by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found