Re: [SPOILER] Re: New Quiz: "What does this code do?" (1-December-2006)
Martijn Lievaart <[email protected]> Fri, 15 Dec 2006 22:08:59 +0100
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Joshua Kronengold wrote: >As of 5.8.5, perlre says this: > > The numbered match variables ($1, $2, $3, etc.) and the related punctu- > ation set ($+, $&, , , and $^N) are all dynamically scoped until > the end of the enclosing block or until the next successful match, > whichever comes first. (See "Compound Statements" in perlsyn.) > >Which on careful analysis (ie, mine, just now :^) would appear to match >the described and tested for behavior -- that, the dynamic scope of >numbered match variables expires at the end of their enclosing block, >making subroutine calls comparatively safe. > >As such, the behavior -is- documented -- and can be relied upon when >coding for modern perls. > > Yes, but I had to read this a few times to really get it. A new match can occur in the same block or another block called from within this block. If it occurs in the same block, you clobber $n. If it occurs in another block, you clobber $n until the end of that block. After that block $n is again the previous value. Thanks to everyone on this thread, learn something new every day! M4