Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

perl grep

by RandomWalk (Beadle)
on Nov 22, 2003 at 16:38 UTC ( [id://309170]=perlquestion: print w/replies, xml ) Need Help??

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

What does
grep /regex/ => @_
mean? I don't see how it follows the format "EXPR, LIST". Thanks.

Replies are listed 'Best First'.
Re: perl grep
by !1 (Hermit) on Nov 22, 2003 at 16:42 UTC

    => is a glorified comma. So now we actually have:

    grep /regex/, @_; # EXPR , LIST

    The regex is an expression. @_ can provide data like a list. And thus it is grep EXPR, LIST.

Re: perl grep
by Corion (Patriarch) on Nov 22, 2003 at 16:42 UTC

    The "fat arrow" => is often also called the "fat comma", because it functions the same as the comma. So the following two pieces of code are the same, functionally:

    grep /regex/ => @_; # and grep /regex/, @_;

    I think that perldoc perlop discusses the peculiarities of the fat arrow, but for many cases, you can simply replace the arrow by a comma.

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
      Ouch! I knew => was a comma in hash definition from the Llama but I didn't make the connection, sorry. But why would an author be using this form
      unless (grep /regexp/ => @_) {...}
      ? Is it just easier to read than it would be using a simple comma? Or does the special property of interpreting the word to the left of => as a string have a beneficial effect? Thank you for your kind help.

        I think the fat comma is used here for aestethical reasons, as /regexp/ is applied to @_. I am also guilty of that sometimes. The notation makes sense sometimes, originally for key-value pairs in hashes :

        my %hash = ( foo => bar );

        You can also use it for the parameters if you have a subroutine that acts as a pipeline :

        sub transform { my ($source, $target) = @_; ... }; transform( 'input/start.file' => 'output/' );

        In that case, the "arrow" could be interpreted also in the mapping sense.

        perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: perl grep
by tcf22 (Priest) on Nov 22, 2003 at 16:49 UTC
    It does the equivalent of grep /regex/, @_. => has most of the same properties of a comma.

    For example
    %hash = (this => 'that', now => 'then');
    is equivalent to
    %hash = ('this', 'that', 'now', 'then');
    You can see how it differs from the comma in the quoting of the keys being optional in most cases. But for most normal uses, its just a funny looking comma.

    - Tom

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-18 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found