[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Re: Localization method of $1
At 01:02 PM 1/15/00 -0500, Jeff Pinyan wrote:
>On Jan 15, Peter Scott said:
> > $_ = "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"
> > }
>
>That's because $_[0] is /really, truly/ $1.
>
> @_ = ("new thing") ;
>
>Will not affect $1. Assigning to a specific index of @_ will modify the
>variable that was passed to the function in that position (see the code
>below). [code showing that @_ is aliased to the inputs snipped]
I'm aware of this. I noted that attempts to assign anything to $_[0] fail
with a read-only variable error.
I'm not assigning anything to $_[0] - I'm just binding it to a regex. The
results I posted show that the effect of this is to overwrite the original
$1 that was passed in, even though some kind of localization happened which
caused its value to be restored after the subroutine returned.
'Localization' to me means, "created a new version of this variable at a
new location, leaving the old one alone until we exit the current scope,"
and that's what happened with the example I posted using 'local'. But the
way perl localized $1 appears to be different.
--
Peter Scott
Pacific Systems Design Technologies
- Follow-Ups from:
-
Ilya Zakharevich <ilya@math.ohio-state.edu>
- References to:
-
Peter Scott <Peter@PSDT.com>
Jeff Pinyan <jeffp@crusoe.net>
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]