Programmatically read from STDIN or input file in Perl

syker picture syker · Jun 29, 2010 · Viewed 145.5k times · Source

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

Answer

ennuikiller picture ennuikiller · Jun 29, 2010
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