Programmatically read from STDIN or input file in Perl

perl, stdin

Solution

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

Problem

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

Original source