Complex return value

Jason White jasona at inetarena.com
Tue Jun 4 18:04:21 CDT 2002


Thank you Todd, the pass by reference model worked perfectly well.
Since I heard a few other suggestions along with a request for a snippet,
I'm supplying the snippet of orignial code here.

This segment of code is reading a list of rules from a configuration file.
It is returning a numerically indexed hash since the order of the rules is
significant.

my %rules = GetArrayFileData($configfile);

#############################################
# GetArrayFileData retrieves array data from a file
# accepts $(filename)
# returns a numerically keyed hash of arrays with file line contents
# requires available filehandle <INFILE>
#############################################
sub GetArrayFileData($){
    my (%TmpHash,$i,$tmp,$value, at Value,$key);
    my ($filename)=@_;
    open(INFILE, "$filename");
    blah blah blah - code bringing the data in from the file and assigning
it to %TmpHash
    close(INFILE);
    if($opt_v){
        Blah Blah - code showing that %TmpHash contains valid data at this
point
    }#end verbose code

return %TmpHash;
####################################
}#end GetArrayFileData
####################################

My hash contained junk.
returning a pointer to the private hash and %{}ing that pointer worked fine.

Jason White

----- Original Message -----
From: "Todd Caine" <todd_caine at eli.net>
To: "Jason White" <jasona at inetarena.com>
Cc: "Pdx-Pm-List at Pm. Org" <pdx-pm-list at pm.org>
Sent: Tuesday, June 04, 2002 3:11 PM
Subject: Re: Complex return value


> Hi Jason,
>
> You can use "return by value".
>
> my %h = &foo();
>
> sub foo {
>   my %hol;
>   return %hol;
> }
>
> or you can use "return by reference".
>
> my $href = &foo();
>
> sub foo {
>   my %hol;
>   return \%hol;
> }
>
> or you can use a closure.
>
> my %h;
>
> &foo();
>
> sub foo {
>   %h = ();
> }
>
> The return by value stuff is a bit slow if you are returning
> a huge data structure.  In most cases it is probably
> sufficient.
>
> Regards,
> Todd
>
> > Jason White wrote:
> >
> > I have a happy and well functioning hash of arrays in a
> > subroutine.
> > If I return that hash to the main program, there's nothing
> > useful inside.
> >
> > I've also tried passing a pointer to the global hash I
> > want the data in and filling the data into that which
> > didn't seem to work, although I don't usually use
> > pointers, so I could have been doing that wrong.
> >
> > What's the best way to get a hash of arrays back into a
> > hash in my main function?
> >
> > Jason White
>

TIMTOWTDI



More information about the Pdx-pm-list mailing list