Perl-声明变量为“ my”,仍然出现错误

时间:2019-03-18 20:21:01

标签: perl

所以我收到此错误:

Global symbol "@Z" requires explicit package name 
(did you forget to declare "my @Z"?) at 
C:/Users/owner/Documents/Slic3r/lib/Slic3r/Print/SupportMaterial.pm line 937. Global symbol "@Z" requires explicit package name (did you forget to declare "my @Z"?) at C:/Users/owner/Documents/Slic3r/lib/Slic3r/Print/SupportMaterial.pm line 945

代码:

my ($X_ref,$Y_ref)=grid($min_X,$max_X,$min_Y,$max_Y,$distance);my @X=@$X_ref;my @Y=@{$Y_ref};
for my $i (0..$#X){
    $Z[$i]=20;#The function that defined the height of each point. This setting wil give you a flat roof. For a more advanced tree, try:
    #$Z[$i]=-0.01*$X[$i]**2+0.2*$Y[$i]-0.005*$Y[$i]**2+20;
}

#End of input parameters.

my $min_radian = deg2rad($min_angle);
my $b = tan($min_radian);
my @Z=map{$_/$b} @Z;

为每个受关注的错误用“ my”声明代码后:

"my" variable @Z masks earlier declaration in same statement at C:/Users/owner/Documents/Slic3r/lib/Slic3r/Print/SupportMaterial.pm line 945.
"my" variable @Z masks earlier declaration in same statement at C:/Users/owner/Documents/Slic3r/lib/Slic3r/Print/SupportMaterial.pm line 945.
syntax error at C:/Users/owner/Documents/Slic3r/lib/Slic3r/Print/SupportMaterial.pm line 937, near "$Z["

代码:

my ($X_ref,$Y_ref)=grid($min_X,$max_X,$min_Y,$max_Y,$distance);my @X=@$X_ref;my @Y=@{$Y_ref};
for my $i (0..$#X){
    my $Z[$i]=20;#The function that defined the height of each point. This setting wil give you a flat roof. For a more advanced tree, try:
    #$Z[$i]=-0.01*$X[$i]**2+0.2*$Y[$i]-0.005*$Y[$i]**2+20;
}

#End of input parameters.

my $min_radian = deg2rad($min_angle);
my $b = tan($min_radian);
my @Z=map{$_/$b} my @Z;

任何帮助都是好人,真的很难弄清楚为什么这两个不能正常工作。

2 个答案:

答案 0 :(得分:3)

您要在两个不同的范围内声明@Z(使用关键字my)。在for循环中一个,然后一个。如果您需要@Z在循环for之后仍然存在,则需要在该作用域之外(并在此作用域之前)声明它。

此外,仅使用一次my

my @Z;
my ($X_ref,$Y_ref)=grid($min_X,$max_X,$min_Y,$max_Y,$distance);my @X=@$X_ref;my @Y=@{$Y_ref};
for my $i (0..$#X){
    $Z[$i]=20;#The function that defined the height of each point. This setting      wil give you a flat roof. For a more advanced tree, try:
    #$Z[$i]=-0.01*$X[$i]**2+0.2*$Y[$i]-0.005*$Y[$i]**2+20;
}

#End of input parameters.

my $min_radian = deg2rad($min_angle);
my $b = tan($min_radian);
@Z=map{$_/$b} @Z;

答案 1 :(得分:-2)

When you set my, so you have to define the base.

old school: my @Z =(); new school: my $Z ="";

This should be the missed one.