Thanks. Very informative.
Perhaps I missed it or perhaps I misunderstand how things work, but isn't there a copy operation that happens at run time that you didn't cover? I'm thinking a copying from the outer scope's match variables to the inner scope's match variables (when the inner scope is entered?), but I'm basing this on what I've heard others say not stuff I've investigated myself.
And it seems \$1 gives you a reference to a magic variable that looks up the value using the match variables of the opcode where you dereference it. That would be one way to explain why this code:
$_= "foobar";
/(oo)/;
my $out= \$1;
my $in;
{
/(ar)/;
$in= \$1;
}
print "in=out\n" if $in == $out;
prints "in=out". So I'd think the problem with returning a reference to a match variable would be that you'd get the outer scope's matches rather than that the value would be overwritten by subsequent calls to the subroutine.
-
tye