2

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?

kos
  • 35,891
  • I am going to weigh in here, but perl --help for -p means -p assume loop like -n but print line also, like sed – Terrance Jun 09 '15 at 17:53
  • @Terrance Thanks for your comment, but here the problem is I had a great missunderstanding about the -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 processing stdin tough – kos Jun 09 '15 at 18:03
  • 1
    The -p option is needed to print, as it is the only switch I can see with any printing involved. The man perl doesn't really cover what the switches do, but perl --help does. I have used the perl -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
  • One other thing, -p assume loop like -n but print line also, like sed and sed is a stream editor, so -p is required to do any stream editing. – Terrance Jun 09 '15 at 18:17
  • @Terrance The problem was since I don't know nothing about Perl, I assumed that substituting sed 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 a print 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

1 Answers1

2

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 with perl. -p is the only option for perl 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!

~$ sudo grep "CMDLINE_LINUX_DEFAULT" /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="nosplash"

~$ sudo perl -pi -e 's/nosplash/splash/' /etc/default/grub

~$ sudo grep "CMDLINE_LINUX_DEFAULT" /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="splash"

~$ sudo perl -i -e 's/splash/nosplash/' /etc/default/grub

~$ sudo grep "CMDLINE_LINUX_DEFAULT" /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="splash"
kos
  • 35,891
Terrance
  • 41,612
  • 7
  • 124
  • 183
  • I missed you answer previously. Can you point out that the problem is the missing print statement? "Without the -p the stream cannot be replaced" sounds incorrect, since the stream is actually replaced, the problem is that it's not printed. Drop me a comment once you've done it and I'll accept the answer – kos Jun 12 '15 at 02:17
  • @kos So sorry about that, good catch. I have updated it to the print showing it. – Terrance Jun 12 '15 at 03:38
  • a perl lover? =) +1 – A.B. Jul 07 '15 at 15:45
  • @A.B. I am in the process of learning it still, but I really do like it. Very powerful commands for it, and the scripting does not have a very difficult to understand structure to it. =) – Terrance Jul 07 '15 at 15:53