Garayed.com  

Go Back   Garayed.com > Perl
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-11-2007, 03:00 AM
Dave Slayton
 
Posts: n/a
Default why doesn't this argument list need a comma after the 1st argument?

Sorry, another newbie question:

I'm reading this very interesting book on Perl (Effective Perl Programming
by Joseph N. Hall with Randal L. Schwartz), and here on page 110 there's an
example of a
call to a (prototyped) subroutine that requires 3 arguments: a coderef, a
scalar, and an array, and here's the call:

for_n {print "$_[0], $_[1]\n"} 2, @a;

I understand the parentheses around the list of arguments are optional, and
that the anonymous subroutine does not require the "sub" keyword, but what I
don't understand is how the call gets away with not having a comma after the
closing curly brace and before the 2. Can anyone shed some light on this
for me?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-11-2007, 03:25 AM
Ben Morrow
 
Posts: n/a
Default Re: why doesn't this argument list need a comma after the 1st argument?


Quoth "Dave Slayton" <evad.notyals@liamg.moc>:
> Sorry, another newbie question:
>
> I'm reading this very interesting book on Perl (Effective Perl Programming
> by Joseph N. Hall with Randal L. Schwartz), and here on page 110 there's an
> example of a
> call to a (prototyped) subroutine that requires 3 arguments: a coderef, a
> scalar, and an array, and here's the call:
>
> for_n {print "$_[0], $_[1]\n"} 2, @a;
>
> I understand the parentheses around the list of arguments are optional, and
> that the anonymous subroutine does not require the "sub" keyword, but what I
> don't understand is how the call gets away with not having a comma after the
> closing curly brace and before the 2. Can anyone shed some light on this
> for me?


It's a special case. Prototypes were introduced to allow you to write
subs that parse like Perl builtins; so, to allow a map-like sub to be
written, a sub with its first argument prototyped '&' will accept a bare
block (without a comma) like this, and treat it as an anon sub.

Actually, this is the only case where the sub keyword is optional: a sub
prototyped as ($&$) would still need to be called like

foo 1, sub {...}, 2;

See perldoc perlsub for all the details.

Ben

--
BEGIN{*(=sub{$,=*)=sub{print@_};local($#,$;,$/)=@_;for(keys%{ #ben@morrow.me.uk
$#}){/m/&&next;**=${$#}{$_};/(\w):/&&(&(($#.$_,$;.$+,$/),next);$/==\$*&&&)($;.$
_)}};*_=sub{for(@_){$|=(!$|||$_||&)(q) )));&((q:\:\::,q,,,\$_);$_&&&)("\n")}}}_
$J::u::s::t, $a::n:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:05 PM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.