Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
langperl

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
bgColor#ccccff
langperl

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

...

Image Modified Image Removed  01. Declarations and Initialization Image Modified