Hey. In Python I can do this:
def fnuh():
a = "foo"
b = "bar"
return a,b
Can I return a list in a similarly elegant way in perl, especially when the return type of the subroutine should be a reference to an array?
I know I can do
sub fnuh {
my $a = "foo";
my $b = "bar";
my $return = [];
push (@{$return}, $a);
push (@{$return}, $b);
return $return;
}
But I bet there is a better way to do that in Perl. Do you know it?
[$a, $b]is doing. First we have the anonymous array constructor[ EXPRESSION ], it evaluates the expression in list context and uses the results to populate an array ref. Next is the expression$a, $b. In scalar context, the expression evaluates to $b (the rightmost element-see the comma operator in perlop). In list context, both values are returned. So the anonymous array ref is populated with two values,$aand$b. Which is almost too much pedantry for the comment system here to handle.