John W. Krahn wrote:
Errin Larsen wrote:
my @numbers; push @numbers, split while <>;
<snip>
why can't I put that variable declaration in the push function? like this:
push my @numbers, split while <>;
The original code could be written as:
my @numbers; while ( <> ) { push @numbers, split; }
Is that really identical to the original code as regards scoping?
No it isn't.
perldoc perlsyn [snip] NOTE: The behaviour of a "my" statement modified with a statement modifier conditional or loop construct (e.g. "my $x if ...") is undefined. The value of the "my" variable may be "undef", any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.
If you declare @numbers inside the while loop it will only be seen inside the loop.
use warnings; push my @numbers, split while <>; my @numbers = (1,2,3);
generates the warning: ""my" variable @numbers masks earlier declaration in same scope at ...".
Yes, the first declaration is in the same scope as the second declaration however, as it says in perlsyn, the value could be anything.
Since this is a beginners list I didn't want to get into all the gory details but this issue has been discussed in comp.lang.perl.misc and the perl5-porters mailing list if you want more details. :-)
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>