Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by Anonymous Monk
on Feb 12, 2013 at 09:34 UTC ( #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
Re: prototypes parens/parenthesis hash initialization/declaration / Too many arguments for / $$
by choroba (Canon) 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 (Abbot) 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 (Abbot) 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
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? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2015-07-03 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls