Update #2: I had a great missunderstanding about the -p
option, now it's clear to me that it's used to process all the files provided as an argument, but I still don't quite understand why this is needed when reading the input from stdin
.
Update #1: I've noticed that the -p
option is always needed (at least when performing a substitution), even when using the deafult separator (newline character) and when the input file is a single line, and I don't quite understand why dropping the -p
option to process a single line (which seems to be something appropriate) breaks everything (i.e. no output).
For example, running echo -n 'test' | perl -pe 's/test/newstring/'
outputs newstring
as expected, but echo -n 'test' | perl -e 's/test/newstring/'
outputs nothing.
Original question:
Why does slurping the whole file reading from stdin
in a Perl command (e.g. perl -0777 -e [...]
) still needs the -p
option to be given in order to actually process the file (e.g. perl -0777 -pe [...]
)?
I understand that the -p
option places a loop around each line of the command, but why isn't changing (in this case removing) the separator enough for Perl to process the file? Or, why is it necessary, despite the fact that the file is going to be processed only once, to tell Perl to place a loop around each line of the command?
The
perl --help
command states that-p assumes a loop like -n but prints line also, like sed
.sed
is a stream editor, and is used to manipulate text much like how the-p
option works withperl
.-p
is the only option forperl
to print the processed output. Without the-p
option, the processed output cannot be printed. So as it's shown in my example below, if the print doesn't happen, the string is not replaced.WARNING: DO NOT REPLACE THE
-p
WITH-n
AS IT WILL BLANK OUT YOUR FILE!