Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

prototypes parens/parenthesis hash initialization/declaration / Too many arguments for / $$

by Anonymous Monk
on Feb 12, 2013 at 09:34 UTC ( [id://1018325]=perlquestion: print w/replies, xml ) Need Help??

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

I'm confused, I thought this would work

$ perl -MData::Dump -e " sub ff($$){ qq{@_} } my %f = ( 12 => ff 1,2, +34 => ff 3,4 ); dd\%f " Too many arguments for main::ff at -e line 1, near "4 )" Execution of -e aborted due to compilation errors.
without having to type parens like
$ perl -MData::Dump -e " sub ff($$){ qq{@_} } my %f = ( 12 => ff( 1,2) + , 34 => ff( 3,4) ); dd\%f " { 12 => "1 2", 34 => "3 4" }

A prototype of ($) doesn't require parens ... anyone?

  • Comment on prototypes parens/parenthesis hash initialization/declaration / Too many arguments for / $$
  • Select or Download Code

Replies are listed 'Best First'.
Re: prototypes parens/parenthesis hash initialization/declaration / Too many arguments for / $$
by choroba (Cardinal) on Feb 12, 2013 at 09:48 UTC
    Another reason why people should not use prototypes. They do not do what you think. See Are prototypes evil?.

    Update: BTW, the mentioned behaviour is normal:

    $ perl -e 'print atan2 1, 2, 3' Too many arguments for atan2 at -e line 1, at EOF
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Another reason why people should not use prototypes. They do not do what you think. See Are prototypes evil?

      Oh, context coersion gone amuch, icky

      I guess if that worked there wouldn't be a need for Devel::Declare and the like

Re: prototypes parens/parenthesis hash initialization/declaration / Too many arguments for / $$
by AnomalousMonk (Archbishop) on Feb 12, 2013 at 18:12 UTC

    Why not try a different approach?

    >perl -wMstrict -MData::Dump -le "sub ff { return map { $_ => join ' ', m{ \d }xmsg } map { m{ \A \d{2} \z }xms ? $_ : die qq{bad arg: '$_'} } @_; } ;; my %f = ff(12, 34); dd \%f; " { 12 => "1 2", 34 => "3 4" }
Re: prototypes parens/parenthesis hash initialization/declaration / Too many arguments for / $$
by kcott (Archbishop) on Feb 12, 2013 at 21:57 UTC
    "I'm confused, I thought this would work ..."

    Using -MO=Deparse,-p will show you how Perl parses the code and why it doesn't work:

    $ perl -MO=Deparse,-p -e 'sub ff($$){ qq{@_} } my %f = ( 12 => ff 1,2, + 34 => ff 3,4 );' Too many arguments for main::ff at -e line 1, near "4 )" -e had compilation errors. sub ff ($$) { "@_"; } (my(%f) = (12, &ff(1, 2, 34, ff(3, 4))));

    On the last line of the output, you'll see &ff(1, 2, 34, ff(3, 4)) has 4 arguments - your prototype specifies 2.

    -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found