$ cat test1.pl #!/usr/bin/perl use strict; use warnings; while (<>){ print; } $ cat > testfile two three $ echo "one" | perl test1.pl testfile one two three $ cat test2.pl #!/usr/bin/perl use strict; use warnings; my $file = shift @ARGV; my $ifh; my $is_stdin = 0; if (defined $file){ open $ifh, "<", $file or die $!; } else { $ifh = *STDIN; $is_stdin++; } while (<$ifh>){ print } close $ifh unless $is_stdin; $ echo "one" | perl test2.pl testfile two three #### unshift(@ARGV, '-') unless @ARGV; while ($ARGV = shift) { open(ARGV, $ARGV); while () { ... # code for each line } }