in reply to Re: Re: Re: array or scalar, that's the question
in thread array or scalar, that's the question
When you say "return @bar" Perl flattens @bar into a list.That's what happens in _list_ context. If the sub is called in scalar context, @bar will also be in scalar context and thus return the number of elements in @bar.
sub foo { my @foo = qw(a b c d); return @foo; } sub bar { return qw(15 4 3 12); } print foo(), "\n"; # list context => abcd\n print scalar foo(), "\n"; # scalar context => 4\n print bar(), "\n"; # list context => 154312\n print scalar bar(), "\n"; # scalar context => 12\n
Explanation: a list returns its last element in scalar context, an array returns the number of elements in scalar context.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Re: array or scalar, that's the question
by Fastolfe (Vicar) on Jan 03, 2002 at 00:01 UTC |