But is this guaranteed?
No, not from the perspective of the sub's caller. It's dependent on the implementation of the sub.
For example, consider the following two functions that behave identically in list context.
>perl -le"sub f { 'a','b','c' } my @v=f(); print for @v" a b c >perl -le"sub f { my @a = ('a','b','c'); @a } my @v=f(); print for @v" a b c
In scalar context, however, they return drastically different results.
>perl -le"sub f { 'a','b','c' } my $v=f(); print $v" c >perl -le"sub f { my @a = ('a','b','c'); @a } my $v=f(); print $v" 3
If the sub doesn't define how it behaves in scalar context (which is the case for many subs that return lists), you have two options.
You can assign to a list with the undesired elements undefined.
(my $first) = f(); (my $first, undef, my $third) = f(); (undef, my $second) = f();
The above can also be written as
my ($first) = f(); my ($first, undef, $third) = f(); my (undef, $second) = f();
You can filter out the undesired returned values. This is usually done using a list slice (see perldata).
my $first = ( f() )[0]; my ($first, $third) = ( f() )[1,3]; my $second = ( f() )[2];
In reply to Re: Ignore certain return values
by ikegami
in thread Ignore certain return values
by elTriberium
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |