in reply to returning arrays from a sub - how clever is Perl?
Is Perl clever enough? I think Perl is stupid enough to return the entire array first and then scalar it. Don't underestimate Perl and the guys who created it ;-P
Is it neccessary? This is really a question for you, not others. Is it not all depends on your reqirement?
By the way, to improve performance, you better return array ref instead of array. (I trust you that by doing scalar @array, what you want is number of elements, not array ref)
Some demo:use strict; use warnings; { my @a = sub1(); print join(",", @a), "\n"; my $a = sub1(); print $a, "\n";; } { my @a = sub2(); print join(",", @a), "\n"; my $a = sub2(); print $a, "\n"; print join(",", @$a), "\n"; } sub sub1 { my @my_array = (1,11,111,1111); return wantarray ? @my_array : scalar @my_array; } sub sub2 { my @my_array = (2,22,222,2222); return wantarray ? @my_array : \@my_array; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: returning arrays from a sub - how clever is Perl?
by Roger (Parson) on Oct 22, 2003 at 00:15 UTC | |
by davido (Cardinal) on Oct 22, 2003 at 04:55 UTC |