My perl script takes any number of files, opens them, and reads one line from each. But it\'s not working. Here\'s my code.
#!/bin/perl
$numfile = scalar @ARGV
Your <>
is being interpreted as a file glob instead of a readline.
Use the following to explicitly specify your intent:
$line[$i] = readline $fh[$i];
[Not an answer, but a comment that doesn't fit as a comment]
Always use use strict; use warnings;
. You are severely handicapping yourself without them.
You don't need to use multiple handles since you never need more than one at a time.
Some other cleanups:
#!/bin/perl
use strict;
use warnings;
my @first_lines;
for my $qfn (@ARGV)
{
open(my $fh, '<', $qfn)
or die("Can't open $qfn: $!\n");
push @first_lines, scalar( <$fh> );
}
for my $i (0..$#first_lines)
{
print "$i => $first_lines[$i]";
}
Use readline
instead of <>
.
perlop
says:
If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed