Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: passing array of args to prototyped subs

by afoken (Parson)
on Sep 10, 2013 at 05:24 UTC ( #1053164=note: print w/ replies, xml ) Need Help??


in reply to passing array of args to prototyped subs

You should really read perlsub. Perl prototypes do not work as you expect.

In Perl, you do not check for the correct number and count of arguments at compile time, but at run time. The minimal solution for exactly two arguments is:

sub f # *NO* prototype! { @_==2 or die "f needs two arguments"; # ... }

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)


Comment on Re: passing array of args to prototyped subs
Download Code
Re^2: passing array of args to prototyped subs
by pldanutz (Acolyte) on Sep 10, 2013 at 05:35 UTC

    Fine. I was under the impression that Perl prototypes were still minimally useful. I expected the compiler to check that f2 has 2 scalar args WHEN FEASIBLE (e.g. when the call is f2 ($a, $b, $c)) and ignore the prototype when compile-time checking is unfeasible. I guess it's too much.

    Your link to a reply in another thread only proves that you can defeat compile-time prototype checking via using &f. however indirectly (which I knew)

      Prototypes are useful; they're just not useful for checking the number and type of arguments to a function.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      div class=

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053164]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2014-09-22 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls