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

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


  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

In reply to 'Dynamic scoping' of capture variables ($1, $2, etc.) by AnomalousMonk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2018-01-17 09:22 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (198 votes). Check out past polls.