Why is Perl squashing my array?

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

Answers


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, @_);
}

Need Your Help

How to properly use anonymous type with Netflix OData API

linq odata netflix

I am trying to use the query below in LINQPad. It isnt working. I am getting this exception:

Setting background color of root window

background xlib

I am trying to learn XLib. And I searched throught source of Feh program to understand how to modify root window, but I had no results.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.