[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Localization method of $1



The following code puzzles me.  It appears to show that within the /e sub, 
the original $1 is overwritten by a successful pattern match, even though 
its value is restored upon exit from the subroutine.

sub pref { \$_[0], " = ", shift || '(undef)', "\n" }

$_ = "abc";
print   "Before setting:  ", pref $1;
s/(ab)/bar($1)/e;
print   "After s///:      ", pref $1;

sub bar {
   $_[0] =~ /(b)/;
   print "Inside /e sub:   ", pref $1;
   print "Original \$1 now: ", pref $_[0];
# Note: attempt to assign to $_[0] here generates
# "Modification of a read-only value"
}


Before setting:  SCALAR(0x80f6964) = (undef)
Inside /e sub:   SCALAR(0x80f6964) = b
Original $1 now: SCALAR(0x80f6964) = b
After s///:      SCALAR(0x80f6964) = ab


This doesn't quite match the way localization works with 'local':

$x = "foo";
print   "Initial:     ", pref $x;
{
   local $x = "bar";
   print "In block:    ", pref $x;
}
print   "After block: ", pref $x;


Initial:     SCALAR(0x80f8db8) = foo
In block:    SCALAR(0x80eb66c) = bar
After block: SCALAR(0x80f8db8) = foo


So it appears that a different kind of localization is going on for $1 that 
doesn't involve putting it in a new storage location in inner scopes.  Is 
it supposed to work this way, and is there some documentation on how it 
works this way that I've missed?  Or is it just that the term 
'localization' has been overloaded?  (5.005_63, Linux.)

--
Peter Scott
Pacific Systems Design Technologies


Follow-Ups from:
Jeff Pinyan <jeffp@crusoe.net>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]