in reply to How to use <> responsibly?
The problem is, if you want your script to read STDIN, and they don't pipe anything in or supply filenames to read, it's going to hang waiting for input. Instead, how about this sort of typical usage, where you supply a `-' as the filename to indicate you'll be supplying input from STDIN instead:
my $filename = shift // die "Usage: $0 <filename | ->\n"; my $fh = *STDIN; if (filename ne '-') { open $fh, '<', $filename or die "Can't open $filename: $!"; } while (<$fh>) { # Process lines of input }
Edit: Me fail English? That's unpossible!
Alternately, simpler structure with autodie and re-opening STDIN instead:
use 5.012; use warnings; use autodie; my $filename = shift // die "Usage: $0 <filename | ->\n"; open STDIN, '<', $filename if $filename ne '-'; while (<STDIN>) { ... }
|
|---|