[JaxPM] Modified RO vars ?

J Proctor jproctor at marlboro.edu
Tue May 7 13:58:14 CDT 2002


On the jacksonville-pm-list; Jax.PM'er J Proctor <jproctor at marlboro.edu> wrote -



> perl -e '(*a,*b,*c)=(\1,\2,\3); for ( @x = ($a,$b,$c) ) { $_++; print }'

Let's pare this down.

*a = \1;
for (@x = ($a))
    { $_++; print; }

My first throught was that the assignment in the for was making a
copy-by-value.  Sounded fishy, but I couldn't find a rule on that either
way in the camel.

So I looked at what happens with $_.  $_ works because it "is an implicit
alias for each item in the list".  But, "if no lexical declaration is in
scope, the loop variable will be a localized (dynamically scoped) global
variable; ... any previous value ... will be restored automatically upon
loop exit." (Camel 3, p. 119)

My next theory was that $_++ is modifying a local copy of the read-only
variable, and that there is no conflict.  It's not hard to imagine code
along the lines of "if this is localized, it can't be read-only".

But $x[0]++ works, too.  And it works after the for loop's closing brace.
So I'm back to my original.

*a = \1 creates a typeglob *a which refers to the read-only value 1.
Interpreting *a as a scalar (i.e. $a) gets you a 1.  Assigning a list with
this roundabout read-only 1 in it is no different from:

for (@x = (1)) {$_++; print;}

which we all know should work just fine.


j


Jax.PM Moderator's Note:
This message was posted to the Jacksonville Perl Monger's Group listserv.
The group manager can be reached at -- owner-jacksonville-pm-list at pm.org
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list