I have a problem with looping through file names, my input array elements gets deleted.
CODE:
use Data::Dumper;
use warnings;
use strict;
my @files = ("file1", "file2", "file3");
print Dumper(\@files);
for (@files) {
my $filename = $_ . '.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
while(<$fh>) {
print "$filename read line \n";
}
}
print Dumper(\@files);
OUTPUT:
$VAR1 = [
'file1',
'file2',
'file3'
];
file1.txt read line
file2.txt read line
file3.txt read line
$VAR1 = [
undef,
undef,
undef
];
FILE CONTENTS:
cat file1.txt
asdfsdfs
cat file2.txt
iasdfasdsf
cat file3.txt
sadflkjasdlfj
Why does the array contents get deleted? (I have 2 different workarounds for the problem, but I would like to understand what's the problem with this code.)
while (<$fh>)
is short for
while ($_ = <$fh>)
so you are clobbering $_
which is aliased to an element of @files
. You need to protect $_
as follows:
while (local $_ = <$fh>)
Better yet, use a different variable name.
while (my $line = <$fh>)
You're using $_
in two different ways inside of the loop (as the current filename and as the current line) and they're clobbering each other. Don't do this. Name your variables, e.g.:
for my $file (@files) {
...
while(my $line = <$fh>) {
...
}
}
You can imagine that your current code does this after reading each file:
for (@files) {
undef $_;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With