Programmatically read from STDIN or input file in Perl

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

160678 次浏览

You need to use <> operator:

while (<>) {
print $_; # or simply "print;"
}

Which can be compacted to:

print while (<>);

Arbitrary file:

open my $F, "<file.txt" or die $!;
while (<$F>) {
print $_;
}
close $F;
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

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(<>);
}

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

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

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.

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($_);
}

Here is how I made a script that could take either command line inputs or have a text file redirected.

if ($#ARGV < 1) {
@ARGV = ();
@ARGV = <>;
chomp(@ARGV);
}


This will reassign the contents of the file to @ARGV, from there you just process @ARGV as if someone was including command line options.

WARNING

If no file is redirected, the program will sit their idle because it is waiting for input from STDIN.

I have not figured out a way to detect if a file is being redirected in yet to eliminate the STDIN issue.