如何将两个数组和一个字符串传递给 Perl 子例程? [英] How can I pass two arrays and a string to a Perl subroutine?

查看:30
本文介绍了如何将两个数组和一个字符串传递给 Perl 子例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将两个数组和一个字符串传递给子对象?

How can I pass two arrays and a string to a sub?

这是我想要做的:

use strict;
use warnings;

my @x = qw(AAAA BBBB CCCC DDDD EEEE);
my @y = qw(1111 2222 3333 4444 5555);

my $z = "hello";

Hello(@x,@y,$z);

exit(0);

sub Hello {

    my (@x,@y,$z) = @_;

    print "$_\n" for @x;
    print "$_\n";
    print "$_\n" for @y;
    print "$_\n";
    print "$z\n";
}

<小时>

输出:

AAA
BBBB
CCCC
DDDD
EEEE
1111
2222
3333
4444
5555
hello
Use of uninitialized value $_ in concatenation (.) or string at test.pl line 19.

Use of uninitialized value $_ in concatenation (.) or string at test.pl line 21.

Use of uninitialized value $z in concatenation (.) or string at test.pl line 22.

<小时>

推荐答案

你需要传递每个数组作为一个引用,否则你在 sub 中的 @x 会吞噬整个数组参数,留下 @y 一个空数组和 $z 一个 undef 值.

You need to pass each of the arrays as a reference, otherwise your @x in the sub will gobble up the ENTIRE array of arguments, leaving @y an empty arraay and $z an undef value.

发生这种情况是因为 逗号运算符 - 在列表上下文中 - 将转向一个 @x, @y, $z 到一个由 @x 的所有元素和 @y 的所有元素组成的数组然后是 $z 的值;你在 sub 中的 @x 将吞噬整个组合的参数数组,留下 @y 一个空数组和 $z 一个 undef 值.

This happens because the comma operator - in a list context - will turn a @x, @y, $z into a single array consisting of all the elements of @x followed by all elements of @y and then a value of $z; your @x in the sub will gobble up the ENTIRE combined array of arguments, leaving @y an empty array and $z an undef value.

另一个可能造成混淆的原因是您将两个变量命名为 @x,尽管由于作用域规则,它们彼此完全独立.好的做法是将它们命名为不同的名称,以避免猜测您打算使用哪个,例如调用子程序的第一个数组@x2.

Another possible source of confusion is the fact that you named both variables @x, despite the fact that they are completely independent of each other due to scoping rules. Good practice would be to name them something distinct to avoid having to guess which one you meant to use, e.g. call the subroutine's first array @x2.

请注意,您可以通过以下两种方式之一将数组作为引用传递 - 对原始数组的引用(真正的按引用传递方法)以及对数组的 COPY 的引用 - 其行为类似于您希望您的原始代码能够正常运行并按值传递.

Please note that you can pass the array as a reference in one of two ways - the reference to the original array (real pass-by-reference approach) as well as reference to a COPY of the array - which will behave like you wanted your origainal code to behave and pass by value.

use strict; use warnings;

my @x = qw(AAAA BBBB CCCC DDDD EEEE); 
my @y = qw(1111 2222 3333 4444 5555);
my $z = "hello";

Hello(\@x,\@y,$z);

# If you wish to pass a reference of a COPY of the array, 
# so that you can modify it inside the subroutine without modifying the original,
# instead call Hello([@x], [@y], $z);

exit(0);

sub Hello {

    my ($x2,$y2,$z2) = @_;
    # Now, you de-reference array reference $x2 via @$x2 or $x2->[$i]
    # where previously you used @x2 or $x2[$i]
    print "$_\n" for @$x2;
    print "$_\n";
    print "$_\n" for @$y2;
    print "$_\n";
    print "$z2\n";

}

这篇关于如何将两个数组和一个字符串传递给 Perl 子例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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