$1 and friends are neither global nor lexical variables. They're Weird Magical Things, and they're tied to the optree, not to any scratchpad or symbol table.

The optree is the parsed and processed form of your program that the perl interpreter actually executes. When you have a regex, perl reserves space for the potential match variables and hangs a pointer to them off the optree (sort of a mini-scratchpad. Sort of) which the rest of the code in the lexical scope (and any subsequent inner scopes, at least some times) will access. (The compiler handles the visibility to inner lexical scopes thing--there's still no scratchpads involved)

Because it's just some odd, regex-engine-private memory, it doesn't really behave the same way that other variables do. They don't get explicitly reset--they're just set when a match happens. So if you skip a match, they retain their old values.

This semi-sorta-global behavior also makes for fun with threads--you must mutex-protect any regex with capturing parens in a threaded program when using 5.005-style threads, or when using ithreads with a 5.6.x perl. (5.8.0 unshares them so its safe) This includes ActivePerl's fork-emulation, though since that doesn't actually expose mutexes to do so it's kind of tricky. (Recent ActivePerl releases might have fixed this--check the release notes)


In reply to Re: Regex Capturing: Is this a bug or a feature? by Elian
in thread Regex Capturing: Is this a bug or a feature? by shotgunefx

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!
  • 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, details, 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, summary, 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.