Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: passing array of args to prototyped subs

by afoken (Abbot)
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"; # ... }


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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053164]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-03-20 00:41 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.