It doesn't do what you think it does (Perl doesn't have named formal parameters, and yes I read your mind).
Prototypes cause trouble and are best avoided. But to answer your question, prototypes allow you to change Perls usual argument-passing mechanism. It is an understatement to say this is tricky. If you want to validate arguments passed in you might want to take a look at Params::Validate. Also the PBP has a few good tips on how to use subs.
HTH
Harry
| [reply] |
| [reply] |
| [reply] |
The only really good use for prototypes is the creation of syntactic sugar. Perl prototypes can let you do neat things like write your own custom grep function that you can call like this:
grepilicious {...} @someArray
Prototypes can also provide limited parameter checking but there are a lot of gotchas involved. In fact, so many that the standard advice is not to use them that way. Here are two:
- There is no guarantee that parameter checking will even take place. Perl only checks parameter prototypes when a subroutine is called as a function and has no leading &. If user decides to call your subroutine like $x->foo() or MyClass::X->foo() or even &foo($x,...), then Perl will happily ignore your prototype. A subroutine author has no control over how people call his/her code, the subroutine has to be written as if there were no error checking at all.
- Even if parameter checking does take place, a
prototype will only complain about bad parameters
if it can't coerce the arguments you pass into good
parameters. This can lead to strange and hard to
track
down bugs. For example, normally you would expect that
foo(@someArray) would pass one parameter for each
member of the array @someArray. So, for example,
if @someArray=(1,2) then foo(@someArray)
would be the same as passing foo(1,2)
However, if you declare something with a prototype
foo($;$) and then try to pass it
foo(@someArray) or some other array, Perl will
pass the size of @someArray, not the
array elements. This is because the first $ of foo($;$)
forces a scalar context on @someArray and an array in scalar
context evaluates to its size.
Protypes can also interfere with good programming
practice if you aren't careful. Passing @_ is a good way to cleanly define two almost alike
subroutines. It is a lot less prone to typos than
extracting all the parameters and then passing them
one by one to a subroutine that takes the same parameters:
sub fooExtra {
my $somethingExtra=shift;
my $result=foo(@_);
# combine $somethingExtra and $result in some way
# and return, for example ....
return $result + $somethingExtra;
}
But if you define foo($;$) with a prototype, the above code won't work. Instead of passing the parameters, you'll pass the size of @_, which is not at all what you wanted.
Best, beth | [reply] [d/l] [select] |
prototypes can act as a check on the code to ascertain that function calls are performed properly, they indicate to Perl:
- Types of arguments the function can take.
- the number of arguments intended to be passed to the function.
(updated) prototypes are flexible, you can also indicate to a function whether a certain prototype is optional and you can include references to hashes in there.
example:
sub NAME($$;$){...} #mistake corrected
this function expects 2 scalar arguments and one optional argument after the ";"...so bear in mind that not all of this is irrelevant and there will come a time when you'd better use a prototype in a function definition than pass it over.
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
| [reply] [d/l] |
How would you go about inspecting the contents of a scalar to decide if it is a number using only prototypes?
That sounds like more of a job for:
confess("Parameter $n stinks") unless $param[$n] =~ /$goodRule[$n]/;
Append:
Example of extensive parameter handling:sub buyWidgets
{
my $numWidgets = shift;
my $supplier = shift;
#validate parameters
unless ($numWidgets =~ /^\d+$/)
{
carp("'$numWidgets' is not a valid number of widgets: purchase
+ canceled\n";
return undef;
}
# Validate, and be flexible if possible. Accept both "ACME" and
+"hash(0xPtrToACME)", and even ["ACME", "Bob's Widgets", "Black Market
+"]
if (ref($supplier) eq 'ARRAY')
{
my $buyCount = 0;
foreach (@$supplier)
{
my $result = buyWidgets($numWidgets-$buyCount, $_);
$buyCount += $result if defined($result);
}
return $buyCount;
}
if (ref($supplier) ne 'Supplier')
{
unless (exists $globalSupplierDirectory->{$supplier})
{
carp("Cannot buy from '$supplier': not a valid supplier\n"
+);
return undef;
}
$supplier = $globalSupplierDirectory->{$supplier};
}
# Do stuff with the good parameters
return $supplier->buyWidgets($numWidgets);
}
Untested, and not self-contained. | [reply] [d/l] [select] |