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?
perl --help
for-p
means-p assume loop like -n but print line also, like sed
– Terrance Jun 09 '15 at 17:53-p
option itself; I assumed it stated to process each line of the input, but it states instead to process each file provided as an argument; still I don't understand why this is needed while processingstdin
tough – kos Jun 09 '15 at 18:03-p
option is needed to print, as it is the only switch I can see with any printing involved. Theman perl
doesn't really cover what the switches do, butperl --help
does. I have used theperl -pi -e 's/what/whatever/' filename
to make an edit in place of a single line in a file. – Terrance Jun 09 '15 at 18:07-p assume loop like -n but print line also, like sed
andsed
is a stream editor, so-p
is required to do any stream editing. – Terrance Jun 09 '15 at 18:17sed
style (i.e. specifying only the substitution to perform as a command) would have print the result, but I understand from your comment that you need aprint
statement to do that. Thanks, if you want to post this as an answer I'll accept the answer – kos Jun 09 '15 at 18:29