在Perl中多次赋值给以前定义的变量

n53p2ov0  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(182)

如何在Perl中为先前声明的变量分配返回值?是否有类似这样的方法:

use strict;
use warnings;
my ($a, $b, $c) = first_assign();
 # let's say $a = "a", $b = "b", $c = "c"

($a, $b, $c) = second_assign();

# let's say we expect them to be "aa", "bb" and "cc" correspondingly

在这种情况下,所有这些变量都将等于“”。那么,是否有某种特殊的方法可以一次赋值给许多先前声明的变量呢?

pn9klfpd

pn9klfpd1#

只需从赋值方法返回一个元组:

use strict;
use warnings;

my ($a, $b, $c) = first_assign();
print "a = $a, b = $b, c = $c\n";

($a, $b, $c) = second_assign();
print "a = $a, b = $b, c = $c\n";

sub first_assign {
  return ('a', 'b', 'c');
}

sub second_assign {
  return ('aa', 'bb', 'cc');
}

这会产生下列输出:

a = a, b = b, c = c
a = aa, b = bb, c = cc

相关问题