Maybe I'm misunderstanding what you're trying to say in your last paragraph, but localizing a variable *is* dynamic scoping. my is lexical scoping.

Note that if you use while things get hairier:

my $i=1; $i=~/(.)/; while ($i++<=4) { print "\$i = $i\n"; print "first: \$1 = $1\n"; $i=~/(3)/; print "second: \$1 = $1\n"; } print "after: \$1 = $1\n";
Gives
$i = 2 first: $1 = 1 second: $1 = 1 $i = 3 first: $1 = 1 second: $1 = 3 $i = 4 first: $1 = 3 second: $1 = 3 after: $1 = 1

Clearly there's some dynamic scoping going on, which is likely to save a lot of people from small mistakes. But in my example, it's all one block of code, so the inner result persists. I'm pretty sure it's easy to come up with lots of real-world examples of iterating with while and a capturing regex...so, caveat programmer.


In reply to Re^3: Problem with regexp or saved match variable. by ssandv
in thread Problem with regexp or saved match variable. by steve077

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.