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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This question is prompted by the discussion of $1 not "freezing" in an addition.

perlre 5.14.2 (in the sub-section 'Capture groups') sez (emphases added):

Capture group contents are dynamically scoped and available to you
outside the pattern until the end of the enclosing block or until the
next successful match, whichever comes first. (See "Compound Statements"
in perlsyn.) You can refer to them by absolute number (using "$1" ...

(The reference in the quote above to the discussion in Compound Statements) does not seem to shed any light on the particular question of this post.

In the code example below, some dynamic scoping is clearly taking place since $1 begins and ends undefined. However, $1, set to '1' by the last successful match at the lowest-but-one level of recursion, is propagated upward unchanged through several levels of subroutine 'blocks' (as I understand them) until it exits the topmost. (local-izing $1 within the subroutine has no effect on this behavior.)

Might this behavior have something to do with the recursive nature of the subroutine: the compiler rewrites the recursive call as a branch to a point within the same block, and so $1 is only restored once because there is only one real block exit?

Can anyone throw any light on this? In particular, any links to documentation?

>perl -wMstrict -le "$_ = 'x55x666x7777x1x'; ;; print 'before: $1 is ', defined($1) ? qq{'$1'} : 'undefined'; print R(); print 'after: $1 is ', defined($1) ? qq{'$1'} : 'undefined'; ;; sub R { printf qq{ \$_ is '$_'}; printf qq{ \$1 is %s \n}, defined($1) ? qq{'$1'} : 'undefined'; return s/(\d+)// ? $1 + R() : 0; } " before: $1 is undefined $_ is 'x55x666x7777x1x' $1 is undefined $_ is 'xx666x7777x1x' $1 is '55' $_ is 'xxx7777x1x' $1 is '666' $_ is 'xxxx1x' $1 is '7777' $_ is 'xxxxx' $1 is '1' 4 after: $1 is undefined

Updates:

  1. Just in case the behavior above was an artifact of $1 being undefined initially, I tried setting $1 to a defined value via a successful match prior to the print $1/print R()/print $1 sequence. The result is no different: the value $1 starts out with at the 'top' level is the one it winds up with.
  2. I should mention I am running all my example code in this and other postings in this thread under Strawberry 5.14.2.1.


In reply to '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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (6)
    As of 2014-10-25 09:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (142 votes), past polls