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

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 1

And 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:

% cat >foo foo bar: ^D % cat foo | perl -p syntax error at - line 2, near "foo bar:" Execution of - aborted due to compilation errors.
</code>

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.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

In reply to 'perl -p' ne 'cat' by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.