Programmatically read from STDIN or input file in Perl

前端 未结 8 1077
南方客
南方客 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 19:56

    Do

    $userinput =  <STDIN>; #read stdin and put it in $userinput
    chomp ($userinput);    #cut the return / line feed character
    

    if you want to read just one line

    0 讨论(0)
  • 2020-12-12 20:01

    This provides a named variable to work with:

    foreach my $line ( <STDIN> ) {
        chomp( $line );
        print "$line\n";
    }
    

    To read a file, pipe it in like this:

    program.pl < inputfile
    
    0 讨论(0)
  • 2020-12-12 20:01

    The "slickest" way in certain situations is to take advantage of the -n switch. It implicitly wraps your code with a while(<>) loop and handles the input flexibly.

    In slickestWay.pl:

    #!/usr/bin/perl -n
    
    BEGIN: {
      # do something once here
    }
    
    # implement logic for a single line of input
    print $result;
    

    At the command line:

    chmod +x slickestWay.pl
    

    Now, depending on your input do one of the following:

    1. Wait for user input

      ./slickestWay.pl
      
    2. Read from file(s) named in arguments (no redirection required)

      ./slickestWay.pl input.txt
      ./slickestWay.pl input.txt moreInput.txt
      
    3. Use a pipe

      someOtherScript | ./slickestWay.pl 
      

    The BEGIN block is necessary if you need to initialize some kind of object-oriented interface, such as Text::CSV or some such, which you can add to the shebang with -M.

    -l and -p are also your friends.

    0 讨论(0)
  • 2020-12-12 20:02
    while (<>) {
    print;
    }
    

    will read either from a file specified on the command line or from stdin if no file is given

    If you are required this loop construction in command line, then you may use -n option:

    $ perl -ne 'print;'
    

    Here you just put code between {} from first example into '' in second

    0 讨论(0)
  • 2020-12-12 20:06

    If there is a reason you can't use the simple solution provided by ennuikiller above, then you will have to use Typeglobs to manipulate file handles. This is way more work. This example copies from the file in $ARGV[0] to that in $ARGV[1]. It defaults to STDIN and STDOUT respectively if files are not specified.

    use English;
    
    my $in;
    my $out;
    
    if ($#ARGV >= 0){
        unless (open($in,  "<", $ARGV[0])){
          die "could not open $ARGV[0] for reading.";
        }
    }
    else {
        $in  = *STDIN;
    }
    
    if ($#ARGV >= 1){
        unless (open($out, ">", $ARGV[1])){
          die "could not open $ARGV[1] for writing.";
        }
    }
    else {
        $out  = *STDOUT;
    }
    
    while ($_ = <$in>){
        $out->print($_);
    }
    
    0 讨论(0)
  • 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(<>);
    }
    
    0 讨论(0)
提交回复
热议问题