in reply to Re^2: Problem with regexp or saved match variable.
in thread Problem with regexp or saved match variable.

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.

Replies are listed 'Best First'.
Re^4: Problem with regexp or saved match variable.
by ig (Vicar) on Aug 27, 2009 at 19:23 UTC

    Thanks for spotting my error. I had confused dynamic and lexical. I'll go re-read Variables and Scoping now.

    As with the foreach loop, if you add a continue block (even an empty one) the behavior through the main loop block changes.

    my $i=1; $i=~/(.)/; while ($i++<=4) { print "\$i = $i\n"; print "first: \$1 = $1\n"; $i=~/(3)/; print "second: \$1 = $1\n"; }continue{} 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 = 1 second: $1 = 1 $i = 5 first: $1 = 1 second: $1 = 1 after: $1 = 1

    I find the behavior without the continue block quite surprising but it might be intentional and a beneficial "feature" in some cases. If it's an intentional feature, it should be documented but there is no mention of this behavior in the obvious places. Thus my bug report.

    At least the behavior is consistent between while and for loops.

      Ah, I hadn't grokked the effect of the continue block. I agree, if this is all "as intended", it's not at all clear from the documenatation.

      The unfortunate first-letter collision of lexical and local has tripped up almost everyone at some point or another, I think.

        Also, the difficulty of using local (in any of its grammatical forms) without wanting to imply the local function frustrates me, and confuses me when others do so without being explicit.

        The following passage from perlsyn is an example that mislead me, and I suspect others, until I realized that local means local but localized means my and localization refers ambiguously to both local or my. It is very difficult for me not to see the local function all three times - I trip on this every time I read the passage.

        The "foreach" loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword "my", then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with "my", it uses that variable instead of the global one, but it’s still localized to the loop. This implicit localisation occurs only in a "foreach" loop.