Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > why doesn't this argument list need a comma after the 1st argument?

Reply
Thread Tools

why doesn't this argument list need a comma after the 1st argument?

 
 
Dave Slayton
Guest
Posts: n/a
 
      03-11-2007
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?


 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      03-11-2007

Quoth "Dave Slayton" <>:
> 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%{ #
$#}){/m/&&next;**=${$#}{$_};/(\w):/&&(&(($#.$_,$;.$+,$/),next);$/==\$*&&&)($;.$
_)}};*_=sub{for(@_){$|=(!$|||$_||&)(q) )));&((q:\:\::,q,,,\$_);$_&&&)("\n")}}}_
$J::u::t, $a::n:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
peterborough uk shortage of 1st 1st line jobs gerry MCSE 91 03-12-2008 06:03 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Passing a semicolon or comma as a Macro argument MattyWix C Programming 7 09-17-2006 04:21 AM
Comma is not always OK in the argument list?! Roman Susi Python 8 07-29-2006 10:36 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57