in reply to Re: expression which does nothing
in thread expression which does nothing

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.

Replies are listed 'Best First'.
Re^3: expression which does nothing
by ferreira (Chaplain) on Dec 26, 2006 at 13:53 UTC

    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 ...;.

        Not just that. It's also to allow the bare 1; at the end of modules to make it end in a true value, without a complaint under perl -c Foo.pm.

        Note that no true value at the end of a module, whatever you choose, causes a complaint under normal cicrumstances, actually because a module is loaded in scalar context.

        As a test: I saved this little module as "Temp.pm" in the current directory:

        # Temp.pm package Temp; printf "Context = %s\n", defined wantarray ? 0+wantarray : 'undef'; # true value, but may cause warning: "nothing";
        This command line makes it work without warning:
        perl -lib=. -MTemp -we1
        Result:
        Context = 0
        

        But with perl -wc, you do get a warning:

        perl -wc Temp.pm
        Useless use of a constant in void context at Temp.pm line 3.
        Temp.pm syntax OK
        

        Replace the string "nothing" with 1, and you're fine:

        Temp.pm syntax OK