gpt4 book ai didi

perl - 为什么 Perl 压缩我的数组?

转载 作者:行者123 更新时间:2023-12-04 13:01:14 24 4
gpt4 key购买 nike

这是一个简短的测试程序:

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";

为什么输出是这样的:
sql: 3
params:

而不是这个?
sql: select * from blah where x = ? and y = ?
params: 2,3

最佳答案

这是因为当您调用 foo(@_) , foo() 的原型(prototype)强制将第一个参数(它是一个数组)转换为一个标量(它是 @_ 中的元素数)。

查看我上一个问题“Why are Perl Function Prototypes Bad”的答案?

FWIW,如果您更改 bar,您可以保留原型(prototype)。因此:

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

关于perl - 为什么 Perl 压缩我的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/335133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com