citromatik has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I usually design my scripts to get their input from a file. In some cases, specially when the script could be used in a pipe, I allow to get the input from STDIN when no file is specified at the command line. I do it this way:
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>){ # Process } close $ifh unless $is_stdin;
I have included this in production code, is it safe?
Thanks in advance
citromatik
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: input from STDIN or from a file
by Fletch (Bishop) on Jan 28, 2008 at 17:12 UTC | |
by citromatik (Curate) on Jan 28, 2008 at 18:02 UTC | |
by citromatik (Curate) on Jan 28, 2008 at 19:58 UTC | |
by Fletch (Bishop) on Jan 28, 2008 at 20:22 UTC | |
Re: input from STDIN or from a file
by kyle (Abbot) on Jan 28, 2008 at 17:12 UTC | |
Re: input from STDIN or from a file
by naikonta (Curate) on Jan 28, 2008 at 17:16 UTC | |
by almut (Canon) on Jan 28, 2008 at 19:17 UTC |
Back to
Seekers of Perl Wisdom