Armed with my knowledge of Perl's -l (ell) switch, I quickly reran the command as:
an|extremely|long|pipe|perl -pl40... and was greeted with the rather cryptic
syntax error at - line 62, near "[some output from my command pipe]" Execution of - aborted due to compilation errors.
After fiddling around with things for a while, I realised that I had to:
an|extremely|long|pipe|perl -pl40 -e 1And all was well. So I thought that I had missed something in the documentation. It sez here (v5.6.0)
-p causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed: LINE: while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\n"; }
Well that's all well and good (and as I remembered). Annoyingly, it's also exactly what I want, but it doesn't really do that. Time to bring out B::Deparse.
% perl -MO=Deparse -pl40 ^D LINE: while (defined($_ = <ARGV>)) { chomp $_; } continue { print $_; } - syntax OK % perl -MO=Deparse -pl40 -e 1 LINE: while (defined($_ = <ARGV>)) { chomp $_; '???'; } continue { print $_; } -e syntax OK
Now I get really confused. The above code, with or without the -e 1 would be just fine. Stripped to its simplest expression, consider the following:
</code>% cat >foo foo bar: ^D % cat foo | perl -p syntax error at - line 2, near "foo bar:" Execution of - aborted due to compilation errors.
Now I'm beginning to see the light:
% cat >foo print "hello, world\n" ^D % cat foo | perl hello, world
That is, despite the presence of the -p (or -n no doubt as well), perl insists on looking for its code on STDIN. But -p has already supplied the code it needs, so why is it doing that? In other words, if perl -p had the same effect as cat(1), then you could do all sorts of things to the file, simply by playing with -l and -0, and possibly a few more besides.
update: bzzt! Abigail points out the flaw. I don't use -p on the shebang line, only for one-liners. Once it can't fit on the command line, I tend to write things out in full. Using -p in a file strikes me as being unecessarily golfish, but that might be just me. Oh well.
In reply to 'perl -p' ne 'cat' by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |