Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically read from STDIN or input file in Perl

Tags:

stdin

perl

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

like image 718
syker Avatar asked Jun 29 '10 07:06

syker


People also ask

What is Stdin Perl?

STDIN in Scalar ContextIn order to take input from the keyboard or operator is used in Perl. This operator reads a line entered through the keyboard along with the newline character corresponding to the ENTER we press after input.

How do I read a command line argument in Perl?

To access your script's command-line arguments, you just need to read from @ARGV array. Perl allows using @ARGV array as filenames by using <>. The $ARGV contains the name of the current file when reading from <>.

Which tag is used to take inputs in Perl?

Input to a Perl program can be given by keyboard with the use of <STDIN>. Here, STDIN stands for Standard Input .

What does <> do in Perl?

It's an operator. Specifically, the readline operator. There's a reference to it as the "angle operator" in perlvar, although there isn't actually any such operator.


1 Answers

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

like image 121
ennuikiller Avatar answered Jan 16 '23 14:01

ennuikiller