use Getopt::Long qw(:config bundling); GetOptions('input-file|i=s' => \my @file); my ($stdin_is_redir, $want_stdin) = !! (stat STDIN)[0]; for my $arg (@file) { my $fh; if ('-' eq $arg) { $want_stdin = 1; # This is never triggered under cron. warn "stdin isn't connected- missing pipe?\n" and next unless -p STDIN or ($stdin_is_redir and ! -t STDIN); $fh = *STDIN{IO}; } else { require Path::Tiny; my $file = path($arg); $fh = eval { $file->openr } or warn "$file: $@->{err}\n" and next; } # do_something($_) while <$fh>; } # This is always triggered under cron unless $stdin_is_redir warn "stdin is connected, missing `-i -`?\n" if ! $want_stdin and ! -t STDIN and ($stdin_is_redir or -p STDIN); # Tested with: # ./script.pl # ./script.pl -i - # ./script.pl -i /dev/null # ./script.pl -i - < /dev/null # echo test | ./script.pl # echo test | ./script.pl -i -