加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何在perl中接受输出参数数组和标量?

发布时间:2020-12-16 06:19:31 所属栏目:大数据 来源:网络整理
导读:我有一个函数,我想检查路径是否存在,如果存在,将其添加到数组.这是我试过的: # If a path exists,adds the canonical version of that path to an arraysub AddPathCandidate { my (@$target,$path) = $_; die ('path needed') unless defined($path); $pat
我有一个函数,我想检查路径是否存在,如果存在,将其添加到数组.这是我试过的:

# If a path exists,adds the canonical version of that path to an array
sub AddPathCandidate {
    my (@$target,$path) = $_;
    die ('path needed') unless defined($path);
    $path = File::Spec->canonpath($path);
    if (-e $path) {
        push(@{$target},$path);
    }
}

调用者看起来像:

my @exampleDirs = ();
AddPathCandidate(@exampleDirs,$inDir . 'a');
AddPathCandidate(@exampleDirs,$inDir . "../b/a/$arch");
AddPathCandidate(@exampleDirs,$inDir . "../../b/a/$arch");

但是“死亡”声明总是会执行; AddPathCandidate的第二个参数没有通过某种方式.

我想在这里做什么甚至可能,还是有更多的“perl-ish方式”来实现这一目标?

解决方法

在解压缩参数时将变量声明为$target,并从@_解压缩:

my ($target,$path) = @_;
    ^                 ^^

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读