On Jun 3, Balint, Jess said:

>Hello all. I am working on my script, and I would like to be able to use
>STDIN as a filehandle input unless a filename is specified. I have used the
>getopt for the filename, let's say $opt_f.

I would use the magic of @ARGV and <>, and do this:

  @ARGV = "< $filename" if $opt_f;
  while (<>) {
    # do something with $_
  }

<> reads from the filenames listed in @ARGV, and if @ARGV is empty (which
I'm assuming it would be) then <> reads from STDIN.

Another approach:

  if ($opt_f) {
    open INPUT, "< $filename" or die "can't read $filename: $!";
  }
  else {
    *INPUT = *STDIN;  # like what you were trying
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to