在这个Perl函数为什么严格裁判错误? [英] Why strict refs error in this Perl function?

查看:143
本文介绍了在这个Perl函数为什么严格裁判错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最低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";
}

这使得

不能使用字符串(22.595451)作为一个数组引用,而严格裁判,在
  使用在test5.pl 8行。

Can't use string ("22.595451") as an ARRAY ref while "strict refs" in use at test5.pl line 8.

基本的东西在这里错在声明的变量。
声明我的主:: $ XA =主:: $ XA; 在初始化是不是要走的路。

Something basic here wrong in declaring the variables. Declaring my main::$xa = main::$xa; in initialization is not the way to go.

这是为什么错误来了吗?

推荐答案

您正在阅读的你传递给函数(22.595451),这是一个字符串的第一个参数,并试图把它作为一个数组(它是不是)。

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).

presumably你打算传递一个数组引用作为一个参数,而不是阵列中的每个成员传递作为一个单独的说法。

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:

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

这是没有意义的。

根据你以后做什么,它看起来像你想 $ AX 是一个数组引用,所以不要取消对它的引用:

Based on what you do later, it looks like you want $ax to be an arrayref, so don't dereference it:

my $xa = $_[0];

这篇关于在这个Perl函数为什么严格裁判错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆