Arrays 为什么;不是数组引用“;错误?
我有这个剧本Arrays 为什么;不是数组引用“;错误?,arrays,perl,Arrays,Perl,我有这个剧本 #!/usr/bin/perl use strict; use warnings; use yy; my $data = [ ["aax", "ert", "ddd"], ["asx", "eer", "kkk"], ["xkk", "fff", "lll"], ["xxj", "vtt", "lle"], ]; use Test::More tests => 4; is(yy::type1_to_type2(\$data,
#!/usr/bin/perl
use strict;
use warnings;
use yy;
my $data = [
["aax", "ert", "ddd"],
["asx", "eer", "kkk"],
["xkk", "fff", "lll"],
["xxj", "vtt", "lle"],
];
use Test::More tests => 4;
is(yy::type1_to_type2(\$data, 'aax'), 'ert');
is(yy::type1_to_type3(\$data, 'asx'), 'kkk');
is(yy::type2_to_type3(\$data, 'fff'), 'lll');
is(yy::type3_to_type1(\$data, 'lle'), 'xxj');
哪个使用这个模块
package yy;
sub typeX_to_typeY {
my ($x, $y, $data, $str) = @_;
foreach (@$data) {
if ($_->[$x - 1] eq $str) {
return $_->[$y - 1];
}
}
return;
}
sub type1_to_type2 { typeX_to_typeY(1, 2, @_) }
sub type1_to_type3 { typeX_to_typeY(1, 3, @_) }
sub type2_to_type1 { typeX_to_typeY(2, 1, @_) }
sub type2_to_type3 { typeX_to_typeY(2, 3, @_) }
sub type3_to_type1 { typeX_to_typeY(3, 1, @_) }
sub type3_to_type2 { typeX_to_typeY(3, 2, @_) }
1;
并且给出了这个错误
Not an ARRAY reference at yy.pm line 6.
# Looks like your test died before it could output anything.
它抱怨的路线是
foreach (@$data) {
这不是传递数组引用的方法吗
我做错了什么?您正在创建一个引用,因为
$data
已经是一个数组引用-首先,它是一个标量,其次,您使用方括号初始化它的值。因此,将调用更改为使用$data
而不是\$data
$data=[]
是对数组的引用。通过使用\$data
可以创建对标量的引用。将代码更改为:
is(yy::type1_to_type2($data, 'aax'), 'ert');
...