Below is one way to do it.

#!/usr/bin/perl
use strict;

sub func {
        my %list;
        $list{"map"} = "key";
        $list{"l"}="j";

        my @arr;
        push @arr,"egg";
        push @arr,"hell";
        return (\%list, \@arr);
}

my ($hashref, $arrref) = &func();

print "keys\n";
foreach my $k(keys %{$hashref}) 
{ 
   print "$hashref->{$k}\n"; 
} 

print "array\n"; 
for my $i (0 .. $#{$arrref}) 
{ 
   print "$arrref->[$i]\n"; 
}

Hope this clarifies.

Thanks,
Sachin Malesha
-----Original Message-----
From: K4 Monk [mailto:[email protected]] 
Sent: Tuesday, February 22, 2011 9:40 PM
To: [email protected]
Subject: baby code

I just realized even after years of dabbling in perl I don't
understand how function calls work. Can someone please be kind enough
to help me understand how to return multiple objects from a function
and use them? Here I'm interested in returning two objects: a hash,
and a list respectively. I'm sure there is more than one way of doing
this in perl

#!/usr/bin/perl
use strict;

sub func {
        my %list;
        $list{"map"} = "key";
        $list{"l"}="j";

        my @arr;
        push (@arr, "egg");
        push (@arr, "hell");


        return \(%list, @arr);

}

 my $arrref = &func();
 my %l = $arrref->[0];
 my @r = $arrref->[1];

 print "keys\n";
foreach my $k(keys %l) { print "$k\n"; }
print "array\n";
foreach my $rr(@r) { print "$rr\n"; }

This prints:

vidura@localhost/tmp $ ./v.pl
keys
egg
array
hell


-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/



--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to