Perl provides a simple mechanism for specifying subroutine argument types called prototypes. Prototypes do not affect functions defined using the &
character. Furthermore, the perlfunc manpage statesappear to indicate the number and types of arguments that a function takes. For instance, this function appears to require two arguments, the first being a scalar, and the second being a list:
Code Block | ||||
---|---|---|---|---|
| ||||
sub function ($@) {
# ... function body
} |
However, prototypes are problematic in many ways. The biggest problem is that prototypes are not enforced by Perl's parser. That is, prototypes do not cause Perl to emit any warnings if a prototyped subroutine is invoked with arguments that violate the prototype.. Perl does not issue any warnings of prototype violations, even if the -w
switch is used.
Prototypes suffer from several other problems, too. They can change function behavior, by forcing scalar context when evaluating arguments that might not be scalars, or by forcing list context when evaluating arguments that might not be lists. A function's prototype is ignored when that function is invoked with the &
character. Finally, according to the perlfunc manpage [Wall 2011]:
Method calls are not influenced by prototypes either, because the function to be called is indeterminate at compile time, since the exact code called depends on inheritance.
Prototypes do not cause Perl to emit any warnings if a subroutine's invocation uses methods that don't match its prototype, not even if the -w
switch is used. They also can change function behavior and consequently should not be usedBecause of these problems, subroutine prototypes must not be used when defining subroutines.
Noncompliant Code Example
...
Code Block | ||||
---|---|---|---|---|
| ||||
sub function ($@) {
my ($item, @list) = @_;
print "item is $item\n";
my $size = $#list + 1;
print "List has $size elements\n";
foreach my $element (@list) {
print "list contains $element\n";
}
}
my @elements = ("Tom", "Dick", "Harry");
function( @elements);
|
However, this program generates the following counterintuitive output:
Code Block |
---|
item is 3 List has 0 elements |
...
Code Block | ||||
---|---|---|---|---|
| ||||
sub function {
my ($item, @list) = @_;
print "item is $item\n";
my $size = $#list + 1;
print "List has $size elements\n";
foreach my $element (@list) {
print "list contains $element\n";
}
}
my @elements = ("Tom", "Dick", "Harry");
function( @elements);
|
...
Tool | Diagnostic |
---|---|
Perl::Critic | Subroutines::ProhibitSubroutinePrototypes |
Bibliography
[Conway 052005] | "Prototypes," p. 194 |
[CPAN] | Elliot Shank, Perl-Critic-1.116 Subroutines::ProhibitSubroutinePrototypes |
[Wall 2011] | perlsub |
...