Programmatically read from STDIN or input file in Perl

PerlStdin

Perl Problem Overview


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

Perl Solutions


Solution 1 - Perl

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

Solution 2 - Perl

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

Solution 3 - Perl

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.

Solution 4 - Perl

You need to use <> operator:

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

Which can be compacted to:

print while (<>);

Arbitrary file:

open F, "<file.txt" or die $!;
while (<F>) {
    print $_;
}
close F;

Solution 5 - Perl

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

Solution 6 - Perl

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

Solution 7 - Perl

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.

Solution 8 - Perl

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsykerView Question on Stackoverflow
Solution 1 - PerlennuikillerView Answer on Stackoverflow
Solution 2 - PerlRonView Answer on Stackoverflow
Solution 3 - PerlNeil BestView Answer on Stackoverflow
Solution 4 - Perlel.pescado - нет войнеView Answer on Stackoverflow
Solution 5 - PerlSigusr2View Answer on Stackoverflow
Solution 6 - PerlThorsten NiehuesView Answer on Stackoverflow
Solution 7 - PerlWestrockView Answer on Stackoverflow
Solution 8 - Perltrapd00rView Answer on Stackoverflow