Programmatically read from STDIN or input file in Perl

前端 未结 8 1076
南方客
南方客 2020-12-12 19:45

What is the slickest way to programatically read from stdin or an input file (if provided) in Perl?

8条回答
  •  失恋的感觉
    2020-12-12 20:06

    if(my $file = shift) { # if file is specified, read from that
      open(my $fh, '<', $file) or die($!);
      while(my $line = <$fh>) {
        print $line;
      }
    }
    else { # otherwise, read from STDIN
      print while(<>);
    }
    

提交回复
热议问题