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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 'perl -p' ne 'cat'
by Abigail-II (Bishop) on Jun 03, 2002 at 15:32 UTC | |
by grinder (Bishop) on Jun 03, 2002 at 20:00 UTC | |
by Abigail-II (Bishop) on Jun 04, 2002 at 09:00 UTC | |
by Juerd (Abbot) on Jun 04, 2002 at 09:55 UTC | |
by hacker (Priest) on Jun 04, 2002 at 16:13 UTC | |
|
perl -pne cat
by japhy (Canon) on Jun 03, 2002 at 20:19 UTC | |
|
Re: 'perl -p' ne 'cat'
by belg4mit (Prior) on Jun 03, 2002 at 20:11 UTC | |
|
(tye)Re: 'perl -p' ne 'cat'
by tye (Sage) on Jun 04, 2002 at 19:06 UTC | |
by danger (Priest) on Jun 04, 2002 at 19:57 UTC | |
|
Re: 'perl -p' ne 'cat'
by stefp (Vicar) on Jun 03, 2002 at 22:00 UTC | |
|
Re: 'perl -p' ne 'cat'
by Aristotle (Chancellor) on Jun 03, 2002 at 23:16 UTC | |
|
Re: 'perl -p' ne 'cat'
by buckaduck (Chaplain) on Jun 04, 2002 at 14:26 UTC |