To save 2 values from a list returned by a sub and throw the third away, one can;
(my $first, my $second) = (1, 2, 3);
print $first, "\n";
print $second, "\n";
exit 0;
and it works as expected (in both perl5 and perl6). If you want just the first however;
(my $first) = (1, 2, 3);
print $first, "\n";
exit 0;
... you get the whole list. This seems counter-intuitive - why the inconsistency?
This should be due to the single argument rule. You get the expected behaviour by adding a trailing ,
:
(my $first,) = (1, 2, 3);
Note that while this works as declarations return containers, which are first-class objects that can be put in lists, you're nevertheless doing it 'wrong':
The assignments should read
my ($first, $second) = (1, 2, 3);
and
my ($first) = (1, 2, 3);
Also note that the parens on the right-hand side are superfluous as well (it's the comma that does list construction); the more idiomatic versions would be
my ($first, $second) = 1, 2, 3;
and
my ($first) = 1, 2, 3;
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