Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

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

It's a good guess, because there is a the fact that $1 points into the original string, and can change when the original string changes. But it's not the source of the confusion here.

You can see that by changing the original code to use m/(\d+)/g instead of s/(\d+)//, thus not modifying the original string at all:

sub R { printf qq{before: \$_ is '$_'}; printf qq{ \$1 is %s \n}, defined($1) ? qq{'$1'} : 'undefined'; m/(\d+)/g ? $1 + R() : 0; printf qq{after: \$_ is '$_'}; printf qq{ \$1 is %s \n}, defined($1) ? qq{'$1'} : 'undefined'; } $_ = 'a81b2d34c1'; R(); __END__ before: $_ is 'a81b2d34c1' $1 is undefined before: $_ is 'a81b2d34c1' $1 is '81' before: $_ is 'a81b2d34c1' $1 is '2' before: $_ is 'a81b2d34c1' $1 is '34' before: $_ is 'a81b2d34c1' $1 is '1' after: $_ is 'a81b2d34c1' $1 is '1' after: $_ is 'a81b2d34c1' $1 is '1' after: $_ is 'a81b2d34c1' $1 is '1' after: $_ is 'a81b2d34c1' $1 is '1' after: $_ is 'a81b2d34c1' $1 is '1'

Let me repeat what I wrote earlier, but hopefully a bit clearer this time: There is only one variable $1. The first call to m/()/ or s/()// creates the dynamic variable $1, and all subsequent calls modify the existing variable $1. Since there is no mechanism for resetting $1 to a previous value, you can see the last value of $1 in all stack frames that have access to it.


In reply to Re^3: 'Dynamic scoping' of capture variables ($1, $2, etc.) by moritz
in thread 'Dynamic scoping' of capture variables ($1, $2, etc.) by AnomalousMonk

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 meditating upon the Monastery: (3)
As of 2024-04-18 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found