0

Below is the snippet that works,

open(C, "Inputfile.txt")  || die "Cannot open the file\n";
use List::MoreUtils qw(uniq);
use Math::Round
while(<C>)

I wish to pass the file name in command line. I tried to use ARGV but it is not working. I modified it to:

use List::MoreUtils qw(uniq);
my $filename = $ARGV[1];
while($filename)

When I execute it with

perl file.pl inputfile.txt

I do not get any output. Can anyone say, why it is not working ?

Thanks,

AP

Arun
  • 649
  • 8
  • 24

2 Answers2

5

Issues

  1. In your test case $ARGV[1] is not defined since Perl arrays are zero-indexed
  2. You need to open a filehandle to $filename
  3. You need to do readline on the filehandle, e.g. while ( <$fh> ) { ... }

That said, just use the diamond operator <> or <ARGV>:

while (<>) {
    ...
}
Zaid
  • 36,680
  • 16
  • 86
  • 155
1

It should be $ARGV[0], not [1]. Per Perl Maven, @ARGV does not include the program name — that's separate variable $0.

cxw
  • 16,685
  • 2
  • 45
  • 81