W
willem
running this script as
perl tst.pl < /dev/null
( or "perl tst.pl < nul" if you are running windows )
shows that the elements in the path array get modified if the loop
contains a '<>' operator.
is that a bug in perl? or am I missing something here.
I tried this with 5.8.3, 5.8.4, 5.8.5, various platforms ( freebsd,
linux, windows-cygwin, windows-activestate )
if you open a file, or pipe, and read it with <FH> the same thing
happens.
willem
-------------------------tst.pl
#!/usr/bin/perl -w
use strict;
#
# this script demonstrates how elements of the @path array
# get changed to 'undef' in the for loop when 'while(<>)'
# was executed somewhere in the loop
#
# I am not expecting @path to be changed by just looping over it.
#
my @path;
print "\n\n... array is fine just looping over it.\n\n";
@path=("usr", "bin");
writepath(\@path, "before first for");
for (@path) {
writepath(\@path, "in first for");
}
print "\n\n... array gets corrupted while reading from STDIN\n\n";
@path=("usr", "bin");
writepath(\@path, "before second for");
for (@path) {
writepath(\@path, "in second for");
while (<>) {
}
}
print "\n\n... and another element undeffed the next time\n\n";
writepath(\@path, "before third for");
for (@path) {
writepath(\@path, "in third for");
while (<>) {
}
}
sub writepath {
my $path= shift;
my $msg= shift;
print($msg, ": ", join(", ", map { sprintf("%d:%s", $_, defined
$path->[$_]?"ok":"NOTOK") } (0..$#$path) ), "\n");
}
perl tst.pl < /dev/null
( or "perl tst.pl < nul" if you are running windows )
shows that the elements in the path array get modified if the loop
contains a '<>' operator.
is that a bug in perl? or am I missing something here.
I tried this with 5.8.3, 5.8.4, 5.8.5, various platforms ( freebsd,
linux, windows-cygwin, windows-activestate )
if you open a file, or pipe, and read it with <FH> the same thing
happens.
willem
-------------------------tst.pl
#!/usr/bin/perl -w
use strict;
#
# this script demonstrates how elements of the @path array
# get changed to 'undef' in the for loop when 'while(<>)'
# was executed somewhere in the loop
#
# I am not expecting @path to be changed by just looping over it.
#
my @path;
print "\n\n... array is fine just looping over it.\n\n";
@path=("usr", "bin");
writepath(\@path, "before first for");
for (@path) {
writepath(\@path, "in first for");
}
print "\n\n... array gets corrupted while reading from STDIN\n\n";
@path=("usr", "bin");
writepath(\@path, "before second for");
for (@path) {
writepath(\@path, "in second for");
while (<>) {
}
}
print "\n\n... and another element undeffed the next time\n\n";
writepath(\@path, "before third for");
for (@path) {
writepath(\@path, "in third for");
while (<>) {
}
}
sub writepath {
my $path= shift;
my $msg= shift;
print($msg, ": ", join(", ", map { sprintf("%d:%s", $_, defined
$path->[$_]?"ok":"NOTOK") } (0..$#$path) ), "\n");
}