Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array elements gets deleted when looping files

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.)

like image 721
gmezos Avatar asked Apr 15 '19 13:04

gmezos


2 Answers

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>)
like image 144
ikegami Avatar answered Sep 28 '22 03:09

ikegami


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 $_;
}
like image 23
AKHolland Avatar answered Sep 28 '22 02:09

AKHolland