Default file source for <> in perl

I want to read data with the <> operator.

It reads data from stdin or from files specified as script args

But, if none of the STDIN is specified, no file is specified, I want to read the data from the default path;

So it should be like

my $file = '';
if ($ARGC) { open $file, '<default.txt'; }
while (<$file>)  # if no ARGs it should be <>
{
   do_all;
}
+4
source share
3 answers

The operator <>reads a list of input file names from @ARGV. Thus, one way to set the default input file name is to check @ARGVif it is empty, and if so, click on the default file name:

push @ARGV, "default.txt" unless @ARGV;

, "no STDIN", , script foo.txt default.txt, , , :

perl script.pl < foo.txt

cat foo.txt | perl script.pl

, , STDIN , -t test. STDIN tty, , :

push @ARGV, "default.txt" unless @ARGV or !-t STDIN;
+4

, STDIN , ,

, "no STDIN" perl STDIN

push @ARGV, "default.txt" if [email protected] and -t STDIN;
+2
@ARGV = 'default.txt'
   if [email protected] && !defined(fileno(STDIN));

,
STDIN, ,
default.txt.

-t script < file foo | script.

+1

All Articles