简体   繁体   English

如何访问Perl中从对象方法返回的数组元素?

[英]How to access array element returned from a object method in perl?

I have been using Perl for a while but is bothered by one syntax problem. 我已经使用Perl一段时间了,但是却被一个语法问题困扰。 In some packages, a method can return an array. 在某些包中,方法可以返回数组。 For example 例如

$root->getlist();

Will return an array. 将返回一个数组。 Now I want to get the first element of the result. 现在,我想获得结果的第一个元素。 Of course I can do this: 我当然可以这样做:

my @results = $root->getlist();
if($results[0] =~ /wow/) {
    print "Qualified result";
}

However, this is very troublesome. 但是,这很麻烦。 Is there a way that I can combine the first line with second line? 有没有一种方法可以将第一行与第二行合并? I tried this but failed. 我试过了但是失败了。

if(${$root->getlist()}[0] =~ /wow/) {
    print "Qualified result";
}

Is there a way to do this quick? 有办法快速做到这一点吗?

A better example: Consider this following package: 一个更好的例子:考虑以下软件包:

package Try;

sub new {
    my $package = shift;
    return bless({}, $package);
}

sub getList {
    return (1,2,3,4,5);
}

1;

Now I have a user pl file like this: 现在,我有一个用户pl文件,如下所示:

use lib '.';
use Try;
use strict;

my $obj = Try->new();
print ($obj->getList())[0];

Trying to run this script will result in: 尝试运行此脚本将导致:

syntax error at perlarrayaccess.pl line 6, near ")[" perlarrayaccess.pl第6行中“)[”附近的语法错误

Execution of perlarrayaccess.pl aborted due to compilation errors. 由于编译错误,perlarrayaccess.pl的执行中止。

if ( ( $root->get_list() )[0] =~ /wow/ ) {
    print "Qualified result";
}

There's wantarray for that. wantarray的那个。 In your sub returning the array, do: 在返回数组的sub ,执行以下操作:

sub getlist()
{
    my $self = shift;

    #  caller wants the full list
    if (wantarray) {
        # fetch all
        return @all_results;
    } else {
        # fetch only first result here.
        return $one_result;
    }
}

This would save you the overhead of fetching all results, when only the one is required. 当只需要一个结果时,这将节省您获取所有结果的开销。 If it's another index you specifically need, write: 如果这是您特别需要的另一个索引,请输入:

if ([$root->getlist]->[5] =~ /wow/) {
...
}

I know, perl is not about easy reading, but this one's more legible than ${$root->get}[0] . 我知道,perl并非易读,但是它比${$root->get}[0]更清晰。

if($root->getlist()[0] =~ /wow/) {
    print "Qualified result";
}

Ought to work. 应该工作。 The second thing that you tried treats the returned value as an array reference and tries to dereference it. 您尝试的第二件事是将返回值视为数组引用,然后尝试取消引用它。 The method just returns an array (or rather, a list - there is a difference), so you need to just access the element you want. 该方法仅返回一个数组(或更确切地说,一个列表-有所不同),因此您只需要访问所需的元素即可。

Using Perl syntax you can just assign return value to a list of variables: 使用Perl语法,您可以将返回值分配给变量列表:

my ($result) = $root->getlist();
print "Qualified result" if $result =~ /wow/;

This is very basic Perl syntax that often used when you need to get few parameters in sub: 这是非常基本的Perl语法,经常需要在sub中获取少量参数时使用:

sub get_three_params {
    my ($foo, $bar, $baz) = @_;
}

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

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