Re: [SPOILER] Re: New Quiz: "What does this code do?" (1-December-2006)

"Ben Prew" <[email protected]> Thu, 14 Dec 2006 14:04:36 -0800
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
On 12/14/06, Jay Savage <[email protected]> wrote:
> On 12/14/06, Joshua Kronengold <[email protected]> wrote:

> 1) whether $1 is lexically or dynamically scoped (it's dynamic), and
>
> 2) whether the grounds on which the proposal that $1 is lexical are
> justified (they aren't).
>

So the thing I don't understand is, if a pattern match is the same
thing as a subroutine call, and it's localizing the match, why do I
see the new value of $1 in the line after it.

ex.

'bar' =~ /(bar)/;
warn $1;
'baz' =~ /(baz)/;
warn $1;

And, this prints out bar, baz, which I expect.  But, it's equivalent to this:

local $foo;
regex_match_to_bar($foo);
warn $foo;

local $foo = $foo;
regex_match_to_baz($foo);
warn $foo;

sub regex_match_to_bar
{
    $foo = 'bar';
}

sub regex_match_to_baz
{
    $foo = 'baz';
}

But not this:


local $foo = 'default';
regex_match_to_bar($foo);
warn $foo;


regex_match_to_baz($foo);
warn $foo;

sub regex_match_to_bar
{
    local $foo = $foo;
    $foo = 'bar';
}

sub regex_match_to_baz
{
    local $foo = $foo;
    $foo = 'baz';
}


And, what I hear from you guys is "regex's are just another name for a
method call, and scope doesn't matter".  However, there's some sort of
implicit addition of local scoping going on that I didn't explicitly
ask for, and it doesn't work the way it would if the regex call was
*really* just a subroutine call.

Or is there something I'm missing?

-- 
--Ben