...
Code Block | ||||
---|---|---|---|---|
| ||||
sub function ($@) {
my ($item, @list) = @_;
print "item is $item\n";
my $size = $#list + 1;
print "List has $size elements\n";
foreach $element (@list) {
print "list contains $element\n";
}
}
my @elements = ("Tom", "Dick", "Harry");
function( @elements);
|
However, this program generates the 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 $element (@list) {
print "list contains $element\n";
}
}
my @elements = ("Tom", "Dick", "Harry");
function( @elements);
|
With no prototype, the first element in the list "Tom"
is assigned to $item
, and the $list
gets the remaining elements: ("Dick", "Harry")
.
Code Block |
---|
item is Tom
List has 2 elements
list contains Dick
list contains Harry
|
...
[Conway 05] pg. 194 "Prototypes"
[CPAN] Elliot Shank, Perl-Critic-1.116 Subroutines::ProhibitSubroutinePrototypes
[Wall 2011] perlsub
...