Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

expression which does nothing

by jesuashok (Curate)
on Dec 26, 2006 at 07:38 UTC ( [id://591652]=perlquestion: print w/replies, xml ) Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

echo test | perl -ple 42 Output :- test
The expression meaning was given as "Does nothing". what is the behaviour of '42' in that one line code ?

Replies are listed 'Best First'.
Re: expression which does nothing
by Anonymous Monk on Dec 26, 2006 at 07:45 UTC
    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
      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.

Re: expression which does nothing
by Cabrion (Friar) on Dec 26, 2006 at 13:43 UTC
Re: expression which does nothing
by logie17 (Friar) on Dec 27, 2006 at 18:23 UTC
    Which has already been mentioned, this appears to be a Noop. Maybe try running with warnings.
    Cheers,
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://591652]
Approved by planetscape
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found