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

Discussion in 'Perl Misc' started by Dave Slayton, Mar 11, 2007.

  1. Dave Slayton

    Dave Slayton Guest

    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?
     
    Dave Slayton, Mar 11, 2007
    #1
    1. Advertising

  2. Dave Slayton

    Ben Morrow Guest

    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::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,
     
    Ben Morrow, Mar 11, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Etienne Antoniutti Di Muro

    read(): 1st argument can't be coerced to byte[]

    Etienne Antoniutti Di Muro, Mar 2, 2004, in forum: Python
    Replies:
    2
    Views:
    365
    Ype Kingma
    Mar 2, 2004
  2. Roman Susi
    Replies:
    8
    Views:
    302
    Nick Vatamaniuc
    Jul 29, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,308
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    2
    Views:
    586
  5. Replies:
    5
    Views:
    246
Loading...

Share This Page