Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
'perl -p' ne 'cat'by grinder (Bishop) |
on Jun 03, 2002 at 15:08 UTC ( [id://171252]=perlmeditation: print w/replies, xml ) | Need Help?? |
I was doing some system maintenance, and at some point I had a script that spewed out a series of values, one per line. Rather than running the command again and more(1)ing it, the lightbulb went off over my head (actually it went on, but chalk that up to an inconsistency of the English language). It occurred to me that all I had to do was filter the output by changing newlines to spaces and I'd probably be able to see all the output in a screenful of space.
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
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)
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.
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> Now I'm beginning to see the light:
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. print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Back to
Meditations
|
|