in reply to perl grep

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

Replies are listed 'Best First'.
Re: Re: perl grep
by RandomWalk (Beadle) on Nov 22, 2003 at 17:15 UTC
    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