develooper Front page | perl.beginners | Postings from May 2003

return multiple values from subroutine

Thread Next
From:
Joseph Paish
Date:
May 14, 2003 12:40
Subject:
return multiple values from subroutine
Message ID:
03051413343700.12705@localhost.localdomain
i want to return multiple values from a subroutine.  all the examples i have 
seen show just a single value (sometimes an array) being returned, and until 
now i have been creating an array just for this purpose.  what i would like 
to do is what i have done in the simple code below (that obviously doesn't 
work).  

i have a couple of questions :

1. can "return" be used to return multiple values or do they all have to be 
collected into one structure like an array?

2. is there any way i can do a multiple assignment like i tried to do in the 
last line below?

i am in the process of breaking up a large script/program into modules and at 
the same time trying to make my code more readable ... even i don't recognize 
what some of it does, and i wrote it only a few months ago :-)  .

thanks

joe

----------
#!/usr/bin/perl -w
use strict ;


test_sub {
    my ( $a, $b, $c ) = @_ ;

    $a = $b * $c ;

    return $a, $b, $c ; # can i even do this??
}

my $d = 7 ;
my $e = 8 ;
my $f = 9 ;

my ($g, $h, $i ) = test_sub ($d, $e, $f) ; # hopefully g = 8 * 9 = 72






Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About