in reply to expression which does nothing

Learn to use B::Deparse
C:\>echo test | perl -ple 42 test C:\>echo test | perl -MO=Deparse -ple 42 BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; '???'; } continue { print $_; } -e syntax OK

Replies are listed 'Best First'.
Re^2: expression which does nothing
by gaal (Parson) on Dec 26, 2006 at 08:23 UTC
    Not that B::Deparse is a bad thing to know, but surely recommending perlrun is more appropriate here :-)

    jesuashok: the 42 here is indeed essentially a noop, but Perl requires something to use as a program source. If you hadn't supplied -e, in the absence of a filename it would try to read the program from standard input; that would appear to make perl hang until you hit EOF (^D or ^Z according to platform) in the terminal window.

    -p, as the anonymous commenter indicated, surrounds a -e-oneliner with a read and print loop. The -e is still required.

      As gaal said 42 is a noop, just like any other constant in void context. I mean if you have a program where a constant is used as an statement like:

      "me"; 9999; { throw => 'away' };
      this program surely does nothing. By the way, if you ask for warnings, Perl will tell you so:
      $ perl -w -e 42 Useless use of a constant in void context at -e line 1.

      The one-liner is a noop over the standard input, which in this case comes from the pipe echo test |. So it allows you to do nothing, while consuming the input.

      As a side note, there's nothing special about 42 except it is the Answer to The Ultimate Question Of Life, the Universe and Everything in pop culture. Very common in humorous pieces of code.

        It's possible to avoid the warning by using 1 (or 0) instead of 42.
        >perl -w -e 42 Useless use of a constant in void context at -e line 1. >perl -w -e 1

        It's a special case to allow 1 while ...;.