whio has asked for the wisdom of the Perl Monks concerning the following question:
My understanding was that foreach() implicitly did local $_; before starting the loop. But look at the output I get (from perl-5.8.0):#!/usr/bin/perl *alias0 = *_; *alias1 = \$_; @F = qw(A B C D); s//Sample/; $x = '%'; do { local $_ = 999; print "do: ( $_ $alias0 $alias1 )\n"; }; foreach (@F) { print $_; *_ = *x; print " : ( $_ $alias0 $alias1 )\n"; }
What's going on here? How does $alias0 manage to get a reference to the loop variable inside the foreach?do: ( 999 999 Sample ) A : ( % A Sample ) % : ( % B Sample ) % : ( % C Sample ) % : ( % D Sample )
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Understanding typeglobs and local()
by Fletch (Bishop) on Jan 13, 2006 at 01:09 UTC | |
by whio (Beadle) on Jan 13, 2006 at 02:08 UTC | |
by ikegami (Patriarch) on Jan 13, 2006 at 03:13 UTC | |
by ysth (Canon) on Jan 13, 2006 at 04:10 UTC | |
by dave_the_m (Monsignor) on Jan 13, 2006 at 12:25 UTC |