在 perl 中分配多个值,undef 出现问题

发布于 2024-09-10 04:07:29 字数 772 浏览 5 评论 0原文

我想从 perl 子例程返回几个值并批量分配它们。

这在某些时候有效,但当其中一个值为 undef 时则无效:

sub return_many {
    my $val = 'hmm';
    my $otherval = 'zap';
    #$otherval = undef;
    my @arr = ( 'a1', 'a2' );
    return ( $val, $otherval, @arr );
}

my ($val, $otherval, @arr) = return_many();

Perl 似乎连接了这些值,忽略了 undef 元素。我期待像 Python 或 OCaml 中那样解构赋值。

有没有一种简单的方法可以将返回值分配给多个变量?

编辑:这是我现在用来传递结构化数据的方式。正如 MkV 建议的那样,@a 数组需要通过引用传递。

use warnings;
use strict;

use Data::Dumper;

sub ret_hash {
        my @a = (1, 2);
        return (
                's' => 5,
                'a' => \@a,
        );
}

my %h = ret_hash();
my ($s, $a_ref) = @h{'s', 'a'};
my @a = @$a_ref;

print STDERR Dumper([$s, \@a]);

I want to return several values from a perl subroutine and assign them in bulk.

This works some of the time, but not when one of the values is undef:

sub return_many {
    my $val = 'hmm';
    my $otherval = 'zap';
    #$otherval = undef;
    my @arr = ( 'a1', 'a2' );
    return ( $val, $otherval, @arr );
}

my ($val, $otherval, @arr) = return_many();

Perl seems to concatenate the values, ignoring undef elements. Destructuring assignment like in Python or OCaml is what I'm expecting.

Is there a simple way to assign a return value to several variables?

Edit: here is the way I now use to pass structured data around. The @a array needs to be passed by reference, as MkV suggested.

use warnings;
use strict;

use Data::Dumper;

sub ret_hash {
        my @a = (1, 2);
        return (
                's' => 5,
                'a' => \@a,
        );
}

my %h = ret_hash();
my ($s, $a_ref) = @h{'s', 'a'};
my @a = @$a_ref;

print STDERR Dumper([$s, \@a]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

差↓一点笑了 2024-09-17 04:07:29

不确定这里的串联是什么意思:

use Data::Dumper;
sub return_many {
    my $val = 'hmm';
    my $otherval = 'zap';
    #$otherval = undef;
    my @arr = ( 'a1', 'a2' );
    return ( $val, $otherval, @arr );
}

my ($val, $otherval, @arr) = return_many();
print Dumper([$val, $otherval, \@arr]);

prints

$VAR1 = [
          'hmm',
          'zap',
          [
            'a1',
            'a2'
          ]
        ];

while:

use Data::Dumper;
sub return_many {
    my $val = 'hmm';
    my $otherval = 'zap';
    $otherval = undef;
    my @arr = ( 'a1', 'a2' );
    return ( $val, $otherval, @arr );
}

my ($val, $otherval, @arr) = return_many();
print Dumper([$val, $otherval, \@arr]);

prints:

$VAR1 = [
          'hmm',
          undef,
          [
            'a1',
            'a2'
          ]
        ];

唯一的区别是 $otherval 现在是 undef 而不是 'zap'。

Not sure what you mean by concatenation here:

use Data::Dumper;
sub return_many {
    my $val = 'hmm';
    my $otherval = 'zap';
    #$otherval = undef;
    my @arr = ( 'a1', 'a2' );
    return ( $val, $otherval, @arr );
}

my ($val, $otherval, @arr) = return_many();
print Dumper([$val, $otherval, \@arr]);

prints

$VAR1 = [
          'hmm',
          'zap',
          [
            'a1',
            'a2'
          ]
        ];

while:

use Data::Dumper;
sub return_many {
    my $val = 'hmm';
    my $otherval = 'zap';
    $otherval = undef;
    my @arr = ( 'a1', 'a2' );
    return ( $val, $otherval, @arr );
}

my ($val, $otherval, @arr) = return_many();
print Dumper([$val, $otherval, \@arr]);

prints:

$VAR1 = [
          'hmm',
          undef,
          [
            'a1',
            'a2'
          ]
        ];

The single difference being that $otherval is now undef instead of 'zap'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文