简体   繁体   English

为什么在此Perl函数中出现严格的refs错误?

[英]Why strict refs error in this Perl function?

Minimum code 最小码

#!/usr/local/bin/perl
use strict;
use warnings;
use Math::Geometry::Planar qw(SegmentLineIntersection);

sub x_intercepts {
    my $xa = @{ $_[0] };
    my @xcross = ();
    my @x_axis = ( [0, 2000], [1, 2000] );

    foreach my $i (0 .. 1) {
        my $xc = SegmentLineIntersection([ @$xa[$i, $i + 1], @x_axis ]);
        push @xcross, $xc;
    }        
    return \@xcross;
}

my @xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 4.619254 4.526920 4.418416 4.321419 4.219890 4.123336 4.009777 3.912648 3.804183 3.705847 3.597756 3.512301 3.393413 3.301963 3.196725 3.098560 3.007482 2.899825 2.801002 2.688680 2.598862 2.496139 2.393526 2.282183 2.190449 2.084530 1.987778 1.877562 1.788788 1.678473 1.578123 1.467071 1.373372 1.283629 1.176670 1.071805 0.975422 0.877622 0.767820 0.667409 0.562480 0.469068 0.354589 0.264291 0.152522 0.063765 -0.045323 -0.136783 -0.248559 -0.343694 -0.459178 -0.551917 -0.640803 -0.755502 -0.845535 -0.955227 -1.045879 -1.155600 -1.254556 -1.365163 -1.461669 -1.571370 -1.658043 -1.772672 -1.865942 -1.981037 -2.073702 -2.176205 -2.276184 -2.367371 -2.476278 -2.567385 -2.686326 -2.777433 -2.884357 -2.980067 -3.087754 -3.183607 -3.291003 -3.386942 -3.495822 -3.586759 -3.702955 -3.793550 -3.900680 -3.999672 -4.093094 -4.200047 -4.301026 -4.399742 -4.493190 -4.602013 -4.705124 -4.812383 -4.907510 -5.022904 -5.109829 -5.214304 -5.317662 -7.703172 -10.350131 -12.921361 -15.431203 -18.188013 -20.544248 -22.822808 -25.679854 -22.999092 -20.540434 -17.964916 -15.398857 -12.990375 -10.402209 -7.888263 -5.504909 -5.217892 -5.109841 -5.014187 -4.908558 -4.811807 -4.704282 -4.605555 -4.504613 -4.406546 -4.292540 -4.204043 -4.088770 -3.995524 -3.905669 -3.796544 -3.707958 -3.596449 -3.490966 -3.382869 -3.293054 -3.185821 -3.088417 -2.971214 -2.880314 -2.772518 -2.677986 -2.569794 -2.473668 -2.365096 -2.276422 -2.179232 -2.068195 -1.973110 -1.859565 -1.771873 -1.669422 -1.569953 -1.462626 -1.364911 -1.258100 -1.159318 -1.050486 -0.959979 -0.849149 -0.749421 -0.640950 -0.547122 -0.451754 -0.344463 -0.252269 -0.134625 -0.051640 0.052970 0.154112 0.266505 0.353926 0.468739 0.561666 0.673810 0.759169 0.881697 0.973207 1.082409 1.170424 1.282163 1.378096 1.472728 1.586454 1.678473 1.785688 1.873862 1.984090 2.086021 2.196799 2.292400 2.386097 2.493190 2.601726 2.694346 2.803450 2.901878 3.011959 3.103050 3.196979 3.294507 3.397563 3.504076 3.600163 3.712539 3.809573 3.919799 4.012314 4.120694 4.216406 4.322895 4.416466 4.522871 4.623917 4.735925 4.826929 7.361253 9.647208 12.337984 14.870260 17.439730 19.921717 22.524080 25.125903/;

foreach my $i ( x_intercepts(@xs) ) {
    print "$i \n";
}

which gives 这使

Can't use string ("22.595451") as an ARRAY ref while "strict refs" in use at test5.pl line 8. 在test5.pl第8行使用“ strict refs”时,不能将字符串(“ 22.595451”)用作ARRAY ref。

Something basic here wrong in declaring the variables. 这里的一些基本的错误声明变量。 Declaring my main::$xa = main::$xa; 声明my main::$xa = main::$xa; in initialization is not the way to go. 在初始化不是要走的路。

Why is this error coming? 为什么会出现此错误?

You're reading the first argument you pass to the function (22.595451), which is a string, and are trying to treat it as an array (which it isn't). 您正在读取传递给函数(22.595451)的第一个参数,该参数是一个字符串,并试图将其视为数组(不是)。

Presumably you intended to pass an array reference as a single argument instead of passing each member of the array as a separate argument. 大概您打算将数组引用作为单个参数传递,而不是将数组的每个成员作为单独的参数传递。

x_intercepts(\@xs)

Having converted the arrayref to a regular array, you then seem to try to treat it as a scalar: 将arrayref转换为常规数组后,您似乎尝试将其视为标量:

my $xa = @{ $_[0] };

That doesn't make sense. 那没有道理。

Based on what you do later, it looks like you want $ax to be an arrayref, so don't dereference it: 根据稍后的操作,您似乎希望$ax成为arrayref,所以不要取消引用它:

my $xa = $_[0];

Your example boils down to: 您的示例可以归结为:

#!/usr/bin/env perl

use strict;
use warnings;

run();

sub run {
    print_x('x');
}

sub print_x {
    my @x = @{ $_[0] };
    print "@x\n";
}

You pass to print_x a list of strings (in this case, a list with just one element), but in print_x , you are treating the first argument as a reference to an array. 您将字符串列表(在这种情况下,仅包含一个元素的列表)传递给print_x ,但是在print_x ,您将第一个参数视为对数组的引用。

Note that if that were the case, you would be creating a second (shallow) copy of the array by copying all of its elements to the lexically scoped @x . 请注意,如果是这种情况,您将通过将数组的所有元素复制到按词法作用域的@x来创建该数组的第二个(浅)副本。 In most cases, this is not necessary or desired. 在大多数情况下,这不是必需的或不需要的。

Reduce the problems you encounter to the shortest complete and runnable script that still exhibits the problem. 将遇到的问题减少到最短的完整且可运行的脚本,该脚本仍会显示该问题。 That will help you learn. 这将帮助您学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM