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

Hi,

In function there is a way to denote number of parameters (eg)

sub callmeth($$){ $arg_method=shift; @arg_meth=shift; }
Is there is a way to denote the type of parameters too ? Like 1st argument should be scalar and 2nd argument should be array.

Regards
perl_devel

CODE tags added by Arunbear

Replies are listed 'Best First'.
Re: Parameters to functions
by dragonchild (Archbishop) on May 12, 2005 at 13:20 UTC
    Params::Validate is probably what you're looking for. Prototypes are NOT what you're looking for, Animator's reply aside. Prototypes are a failed experiment that should not be used (unless, of course, you know why they shouldn't be used).

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Parameters to functions
by Animator (Hermit) on May 12, 2005 at 13:18 UTC

    What you are refering to is called prototypes (the ($$)-thingie), there are several posts on this site explaining why prototypes are bad and shoudln't be used... I suggest you try a super-search for prototype.

    Also be careful when you say: 'second param should be an array', you cannot pass an array (or to be correct, you can pass it, but you can't extract it from the @_ array), you can only pass an array-reference (you can read more about references in the perlreftut, perlref and perldsc POD).

    Update: If you want to verify the parameters then you need to do that yourself in your sub, for that you can use write your own code for this or use a module like dragonchild. (If you want to write your own code then it might be a good idea to look at the ref-function (to see if something is an array-reference for example))

      To expand on the parent's post update, when passing refs you *can* check if it's the proper one. To take your example:
      sub callmeth { my @arguments = @_; my $scalar = shift(@arguments); my $arrayref = shift(@arguments); unless(ref($arrayref) eq 'ARRAY')) { carp/croak/die/whatever you feel like; } * do stuff here }
      Personally I spend less and less time on making sure methods that are available from outside a package check what their input is. If it's code that at some point might be used by others then i might check some inputs and do a little carping when it's wrong, but Perl is a highly flexible language and it's neigh impossible to account for every possible way someone using your code might screw things up. If the code works and you've accounted for anything *you* could do wrong and the documentation is clear and complete then your job is done in my opinion.

      Remember rule one...

Re: Parameters to functions
by tlm (Prior) on May 12, 2005 at 13:33 UTC

    BTW, since you used "function" in your title, but "meth(od)" in the body of your post, it's worth pointing out (particularly in the context of prototypes) that in Perl these two concepts (functions and methods) are not interchangeable. Function calls and method calls follow a different syntax and are governed by different rules. In particular, prototypes are not honored in method calls. perlsub is the place to go for the gory details.

    the lowliest monk

Re: Parameters to functions
by sh1tn (Priest) on May 12, 2005 at 13:22 UTC
    In addition:
    sub _sub_name ($@) { } #takes scalar and array