How do I return multiple values from a Perl function call?
my ($value1, $value2, $value3) = getValues(@parts)
sub getValues
{
foreach(@_)
{
$_ =~ m{/test1_name (.*) test2_name (.*) test3_name (.*)/};
$test1_value = $1;
$test2_value = $2;
$test3_value = $3;
}
}
This code is not working.
You can also assign an array to hold the multiple return values from a Perl function. You do that like this: sub foo { return ('aaa', 'bbb', 'ccc'); } (@arr) = &foo(); print "@arr\n"; As you can see, most of the code is the same, except I now assign an array ( @arr ) to contain the three return values from my function.
You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values.
Returning a an array from a function in PerlCollect them in an array inside the function and then return the array. Behind the scenes Perl sees the content of this array and returns the elements as a list. Usually the caller will assign the result to an array as we did in the first call of fibonacci(8).
The Perl idiom would be to return the multiple values in a list and then assign the result of the function to the list of variables that you want to receive values. You've already assigned the result of the function correctly, so the only change you need is return ($1, $2, $3);
as everyone has suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With