Greetings;

This isn't technically an ActivePerl question, but I'm hoping someone
here can point me to some more information.

I'm puzzled about methods for passing hashes (or arrays) by reference to
a subroutine and updating the actual data in the hashes. 

I know I can make a subroutine like this:

sub add2hash3 {
        # refer to data indirectly but return reference to new hash
        my %th = %{ shift @_ };
        $th{'praying'} = 'mantis';
        return \%th;
}

and call it like this:

%hash = %{ &add2hash3(\%hash) };

But that strikes me as kind of kludgy, and it can also be awkward if you
want to go in later and add additional input/return values to the
subroutine. 

Another alternative that works is to write a function like:

sub add2hash {
        # refer to data directly using hash reference
        my $thRef = shift @_;
        $thRef->{'worker'} = 'ant';
}

and call this like so:

&add2hash(\%hash);

All very well and good. But for some reason, the first thing I tried
didn't work. That was to write a function like this:

sub add2hash2 {
        # refer to data indirectly using local (my) hash
        my $thRef = shift @_;
        my %th = %$thRef;
        $th{'bee'} = 'hive';
}

It seems to me like this should be doing the same thing as add2hash
above, just defining a local hash name for convenience. But it doesn't
work the same and I'm not sure why. Is this explained anywhere in the
perl docs? 

Thanks,

--Art
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to