#!/usr/bin/perl -w
use strict;
sub readZip() {
open FH, '/dev/null';
while (<FH>) { print "Got something!\n"; }
}
my $x = 'hi';
for ($x) { readZip(); }
print "Undefined!\n" unless defined $x;
(I'm using perl 5.6.1)
Thanks,
Ted Hopp
t...@newslate.com
for ($x) { .... } makes $_ an alias for $x
inside readZip(), while (<FH>) modifies $_ (which is still an alias for $x)
maybe adding
local $_;
or
my $_;
inside readZip will produce the results you want
It's not a perl's bug.
for ($x) { readZip(); }
This loop run once, $_ is set to $x, in actually, $_ is an alias of
$x, so if something in the for BLOCK changes $_, $x also changed,
while, now look at your readZip.
while(<FH>) { print "Got something!\n"; }
The loop's BLOCK doesn't run, but <FH> is evaluted once. while(<FH>)
is same as
while($_=<FH>), so, $_ is set to undef, and, the outer $x is set to
undef. That's it.
If you change your while statement to something like:
while(my $line = <FH> ) { .. }
everything is OK.
the $_ is local in the for BLOCK, so it's visible for all the chained
subroutine in runtime, redeclare $_ as local will break the rules.
sub readZip()
{
local $_; #This will protect the outer $_ well.
...
}
I heard that "There's more than one way to do it", ok, here's the No 2
No.3
change your for statement to:
for $y($x) { ... }
Ted Hopp
t...@newslate.com