While
impossiblerobot and
tstock provided excellent solutions, it may help to understand why these solutions are needed.
When you
return() an array, it is sent back (so to speak) as a list. The variable
@array provides a nice little grouping for all its elements. If we had
@array = ('a', 'b'); and
return()'ed it, the portion catching this value will get
('a', 'b') and that variable container,
@array, is no more.
What does this lead to? Well, given the following:
sub foo { my @a = qw(a b); my @b = qw(c d); return (@a, @b); }<br>
@return_val = foo();
You might expect
@return_val to contain
('a', 'b')... But because the return value is put into a list,
('a', 'b', 'c', 'd') is returned from foo(). There aren't any
@a or
@b anymore to "draw the line" and distinguish one array from the other... it's just one big list.
So anyhow, by returning scalars (strings, hash refs, array refs, etc), you always ensure that the elements of each of the return values (or the elements pointed to by them) stay in their container variables. =)