Programmatically read from STDIN or Perl input file

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

+58
perl stdin
Jun 29 '10 at 7:23
source share
9 answers
while (<>) { print; } 

will be read either from the file specified on the command line or from stdin if the file is not specified

If you need this loop construct on the command line, you can use the -n option:

 $ perl -ne 'print;' 

Here you simply put the code between {} from the first example in '' in the second

+75
Jun 29 '10 at 7:28
source share

This provides a named variable to work with:

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

To read a file, swipe it like this:

 program.pl < inputfile 
+39
Feb 15 '13 at 17:15
source share

You need to use <> operator:

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

which can be compressed to:

 print while (<>); 

Arbitrary file:

 open F, "<file.txt" or die $!; while (<F>) { print $_; } close F; 
+12
Jun 29 '10 at 7:25
source share

In some situations, the "slippery" way is to use the -n switch. It implicitly wraps your code with a while(<>) and flexibly controls the input signal.

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:

  • Wait for user input

     ./slickestWay.pl 
  • Reading from files named in arguments (no redirection required)

     ./slickestWay.pl input.txt ./slickestWay.pl input.txt moreInput.txt 
  • Use the handset

     someOtherScript | ./slickestWay.pl 

A BEGIN necessary if you need to initialize some kind of object-oriented interface, such as Text :: CSV or some that you can add to shebang with -M .

-l and -p are also your friends.

+11
Feb 10 '14 at 17:25
source share

If there is a reason, you cannot use the simple solution provided by ennuikiller above, then you will have to use Typeglobs to manage the file descriptors. It works more. In this example, it is copied from the file in $ARGV[0] to the file $ARGV[1] . By default, these are STDIN and STDOUT if no files are 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($_); } 
+7
Dec 07 '14 at 18:52
source share

Do

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

if you want to read only one line

+6
Jan 24 '13 at 13:59
source share

This is how I created a script that can accept either command line inputs or redirect a text file.

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


This reassigns the contents of the file to @ARGV, from there you just process @ARGV, as if someone included command-line options.

WARNING

If the file is not redirected, the program will sit in standby mode because it is waiting for input from STDIN.

I did not understand the way to determine if the file is being redirected, but to fix the problem with STDIN.

0
Sep 20 '15 at 18:01
source share

Shamelessly stolen from Neil Best and Ron

Assume script name: stdinProcessor.pl

 #!/usr/bin/perl BEGIN: { print "Doing some stuff first\n"; } foreach $line ( <STDIN> ) { chomp($line); print "$line\n"; } # Do something at the exit print "Iteration done\n" 

Example:

 $echo '1\n2\n3' | ./stdinProcessor.pl Doing some stuff first 1 2 3 Iteration done 
0
Jun 23 '17 at 2:04 on
source share
 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(<>); } 
-2
Jun 29 '10 at 7:44
source share



All Articles