6

Here's a short test program:

sub foo($;@) {
  my $sql = shift;
  my @params = @_;

  print "sql: $sql\n";
  print "params: " . join(",", @params);
}

sub bar($;@) {
  foo(@_);
}

bar("select * from blah where x = ? and y = ?",2,3);
print "\n";

Why is the output this:

sql: 3
params: 

Rather than this?

sql: select * from blah where x = ? and y = ?
params: 2,3
Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
mike
  • 46,876
  • 44
  • 102
  • 112

3 Answers3

20

It's because when you call foo(@_), the prototype for foo() forces the first parameter (which is an array) to be converted into a scalar (which is the number of elements in @_).

See answers to my previous question asking "Why are Perl Function Prototypes Bad"?

FWIW, you can keep the prototypes in place if you change bar thus:

sub bar($;@) {
    foo(shift, @_);
}
Community
  • 1
  • 1
Alnitak
  • 334,560
  • 70
  • 407
  • 495
10

You're misunderstanding Perl prototypes. Remove them and it'll work fine.

See: Why are Perl 5's function prototypes bad?

Community
  • 1
  • 1
nobody
  • 19,814
  • 17
  • 56
  • 77
5

get rid of ($;@) after your function names and it works fine.

Tarski
  • 5,360
  • 4
  • 38
  • 47