On Wed, Apr 9, 2008 at 12:14 PM, James Carman <<a href="mailto:developer@peelle.org">developer@peelle.org</a>> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hey all,<br>
<br>
Is it possible to do something like:<br>
<br>
<br>
&param_test("somestring", "another_string" $arrayref);<br>
<br>
sub param_test($tom; $george; @$array) {<br>
print $tom;<br>
....;<br>
}<br>
<br>
<br>
#################<br>
I am wanting to Specifiy how many arguments to take. Also I want to specify the name of the arguments. And also if possible the type of the arguments.</blockquote><div><br>Perl's concept of a subroutine prototype is unusual and there are some purists who will tell you to never use them. I think they are helpful when used carefully. In this case, this is about the best you can do:<br>
<br>sub param_test($$\@) { <br> my ($tom, $george, $array_param) = @_;<br> ...<br>}<br><br>You would then call the subroutine (make sure you *don't* use &param_test() or Perl will ignore the prototype):<br><br>
param_test("somestring", "otherstring", @array);<br><br>The other alternative would be to declare param_test() like this:<br><br>sub param_test($$@) {<br> my ($tom, $george, @array_param) = @_;<br> ...<br>
}<br><br>This could be called exactly the same way as before. The significant difference between the two is that in the first one, the $array_param variable is actually a references to the \@array passed, which means you can modify it in place. In the second case, you don't have a direct reference to the @array value to modify.<br>
<br>The good thing is that if you call param_test() this way, it will warn you during compile that something isn't right when you try to use it without three parameters, specifically two scalars followed by an array.<br>
<br>That's about it for compile time checking. For more details, you can see the "Prototypes" section of perlsub (<a href="http://search.cpan.org/search?query=perlsub&mode=all">http://search.cpan.org/search?query=perlsub&mode=all</a>). If you want robust runtime checking, I second Andy's recommendation of Param::Validate.<br>
<br>Cheers,<br>Andrew<br></div></div>