Versions Compared

Key

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

...

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.  Prototypes do not affect functions defined using the  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.

...

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

...

 

 

...

Image Modified Image Modified Image Modified