[ABE.pm] returning an array from a subroutine

Jim Eshleman jce0 at Lehigh.EDU
Fri Mar 4 19:56:27 PST 2005


> How to do it?
> 
> 
> I've got a sub routine that creates an array (it could be sizeable but
> I'll worry about that later).  I've tried every permutation I can think
> of to return the array to the calling function:
> 
>     @my_array = foobar() and in the func do a "return @another_array"
>     foobar(@myarray) and populating it inside the func 
>     foobar(\@myArray) and populating it inside the func
> 
> The only thing I can think of doing now is to flatten the array into a
> string.  I can't believe that's how it's suppposed to be done.
> 

AFAIK you just do what seems natural:

$ cat test.pl
#!/usr/bin/perl -w
 

use strict;
 

my @my_array = foobar();
print "@my_array\n";
exit;
 

sub foobar {
     my @another_array = (0,1,2,3);
     return @another_array;
}
$ ./test.pl
0 1 2 3
$
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20050304/d958ef52/signature.bin


More information about the ABE-pm mailing list