New to perl (and language in general), and have gone so far as OPEN FH
.
A little confused about the following two scenarios (outputs are different, but I couldn't understand why the second one is "wrong").
script 1
#! usr/bin/perl
use warnings;
use strict;
open FILE, "example.txt" or die $!;
while (<FILE>) {
print $_;
}
script2
#! usr/bin/perl
use warnings;
use strict;
open FILE, "example.txt" or die $!;
my @array = <FILE>;
while (<FILE>) {
print $_;
}
The example.txt contains 3 lines of sentence. When I assign filehandle FILE
to an array, suddenly, $_
becomes empty ??
If I replace <FILE>
in the while
loop with array (@array
), the loop cannot stop and $_
becomes "uninitialized value".
This is likely a very basic misunderstanding of filehandle
and array , but can someone please help explain them - in plain English - ? Or point to the references where I could find more info (have a few references handy and have consulted them, but obviously I still couldn't quite understand why script 2 (and 3) are not correct). Great many thanks.