Have been playing with the following script but still couldn't understand the meaning behind the two different "kinds" of filehandle forms. Any insight will be hugely appreciated.
#! usr/bin/perl
use warnings;
use strict;
open (FH, "example.txt") or die $!;
while (<FH>) {
my @line = split (/\t/, $_); {
print "@line","\n";
}
}
The output is as expected: @line
array contains elements from line 1,2,3 ... from example.txt
. As I was told that open (FH, example.txt)
is not as good as open (my $fh, '<', 'example.txt')
, I changed it but then confusion arose.
From what I found, $fh
is scalar
and contains ALL info in example.txt
. When I assigned an array to $fh
, the array stored each line in example.txt
as a component in the array. However, when I tried to further split the component into "more components", I got the error/warning message "use of uninitialized value
". Below is the actual script that shows the error/warning message.
open (my $fh, '<', 'example.txt') or die $!;
foreach ($fh) {
my @line = <$fh>;
my $count = 0;
for $count (0..$#line) {
my @line2 = split /\t/, $line[$count];
print "@line2";
print "$line2[0]";
}
}
print "@line2"
shows the expected output but print "$line2[0]"
invokes the error/warning message. I thought if @line2
is a true array, $line2[0]
should be okay. But why "uninitialized value" ??
Any help will be appreciated. Thank you very much.
Added - the following is the "actual" script (I re-ran it and the warning was there)
#! usr/bin/perl
use warnings;
use strict;
open (my $fh, '<', 'example.txt') or die $!;
foreach ($fh) {
my @line = <$fh>;
print "$line[1]";
my $count = 0;
for my $count (0..$#line) {
my @line2 = split /\t/, $line[$count];
print "@line2";
#my $line2_count = $#line2;
#print $line2_count;
print "$line2[3]";
}
}
The warning is still use of uninitialized value $line2[3] in string at filename.pl line 15, <$fh> line3
.