Why should I use <ARGV> or <> instead of <STDIN> in Perl?

Quote from Perl::Critic::Policy::InputOutput::ProhibitExplicitStdin

Perl has a useful magic file descriptor called * ARGV, which checks the command line and, if there are any arguments, opens and reads them as files. If there are no arguments, * ARGV behaves like * STDIN. This behavior is almost always necessary if you want to create a program that reads from STDIN. This is often written in one of the following two equivalent forms:

while (<ARGV>) {
  # ... do something with each input line ...
}
# or, equivalently:
while (<>) {
  # ... do something with each input line ...
}
  • Is this “just a convention” or is there some good reason not to use it <STDIN>?

I feel that <STDIN>makes my code intentions clearer than using <>or <ARGV>.

My code stream is like this

my @inp = <STDIN>;
my $len = $inp[0];

...

for(my $i = 0; $i < ($len + 0); $i++) {
    my @temp = split (' ', $inp[$i]);
    ...
}
+5
2

, <ARGV> , stdin . <STDIN>, .

, , , , , , .

+5

, , . . perlvar ARGV. ARGV , . , , , .

, Perl:: Critic. . STDIN , . , , , . . , .

, , STDIN, . , , . .

, , , Perl C, :)

 chomp( my $length = <STDIN> );

 my $count = 0;
 while ( <STDIN> ) {
     last if $count++ > $lines_to_read; 

     my @temp = split ' ';
     ...;
     }
+6

All Articles