User0110101
User0110101

Reputation: 119

Perl - Declared variable with 'my', still get error

so I'm getting this error:

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

Code:

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;

After declaring the code with "my" for each of the respected errors:

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

Code:

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;

Any help would be great guys, really struggling to figure why these two are not working correctly.

Upvotes: 2

Views: 380

Answers (2)

TRSi
TRSi

Reputation: 1

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.

Upvotes: -2

Mike Holt
Mike Holt

Reputation: 4612

You're declaring @Z (using the keyword my) in two different scopes. One inside the for loop, and one afterwards. If you need @Z to still exist after the for loop completes, you need to declare it outside of (and prior to) that scope.

Also, only use my once:

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;

Upvotes: 3

Related Questions