错误 - “全局符号需要明确的包名" [英] Error - 'global symbol requires explicit package name'
问题描述
我正在尝试为矩阵乘法编写脚本.它只是一个基本程序,但我无法弄清楚以下错误:
I am trying to write a script for matrix multiplication. Its just a basic program but I am not able to figure it out about the following error :
全局符号@ref_mat1"需要在 multiplication.pl 第 49 行显式包名称.
Global symbol "@ref_mat1" requires explicit package name at multiplication.pl line 49.
全局符号@ref_mat2"需要在 multiplication.pl 第 49 行显式包名称.
Global symbol "@ref_mat2" requires explicit package name at multiplication.pl line 49.
下面是我的脚本:
#!/usr/bin/perl -w
use strict;
my @mat1=(
[2,3,4],
[1,2,3],
[3,4,5]
);
my @mat2=(
[2],
[3],
[4]
);
my ($i, $j, $k);
my $r_product=[];
$r_product= mat_multiplication(\@mat1,\@mat2);
sub mat_multiplication
{
my ($ref_mat1,$ref_mat2)=@_;
my($mat1_row,$mat1_col)=total_rows_column($ref_mat1);
my($mat2_row,$mat2_col)=total_rows_column($ref_mat2);
for($i=0;$i<$mat1_row;$i++)
{
for($j=0;$j<$mat2_col;$j++)
{
my $sum=0;
for($k=0;$k<$mat1_col;$k++)
{
$sum=$sum+$ref_mat1[$i][$k]*$ref_mat2[$k][$j];
}
$r_product->[$1][$j]=$sum;
}
}
return $r_product;
}
sub total_rows_column
{
my($r_mat) =@_;
my $num_row=@{$r_mat};
my $num_col=@{$r_mat->[0]};
return($num_row,$num_col);
}
我搜索了这个问题并找到了一个链接
I searched for this problem and found one link
但是还是无法解决.请看看让我知道我在哪里做错了.
But still not able to resolve it. Please just have a look an let me know where am i doing mistake.
谢谢
推荐答案
$ref_mat1
和 $ref_mat2
是对数组的引用.在 Perl 中,如果您想访问对数组的引用,则不能直接使用 $reference[$idx]
—— 必须在引用后使用 ->
运算符像这样:$ref_mat1->[0]
.
$ref_mat1
and $ref_mat2
are references to arrays. In Perl if you want to access a reference to an array you cannot use $reference[$idx]
directly -- you have to use the ->
operator after the reference like this: $ref_mat1->[0]
.
没有它 Perl 认为 $ref_mat1[0]
指的是一个不存在的数组 @ref_mat1
.是的,$var
和 @var
可以同时存在,但内容不同,请参见此示例:
Without it Perl thinks that $ref_mat1[0]
refers to an array @ref_mat1
which doesn't exist. Yes, both $var
and @var
can exist at the same time with differing content, see this example:
use strict;
use Data::Dumper;
my $abc = 42;
my @abc = (1, 2, 3);
print Dumper($abc), Dumper(\@abc);
这篇关于错误 - “全局符号需要明确的包名"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!