What is the slickest way to programatically read from stdin or an input file (if provided) in 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.
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 <>.
Input to a Perl program can be given by keyboard with the use of <STDIN>. Here, STDIN stands for Standard Input .
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.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With