Home > Archive > PERL Beginners > July 2005 > passing hash data from one subroutine to another using refs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
passing hash data from one subroutine to another using refs
|
|
| Brent Clark 2005-07-28, 9:07 am |
| Hi
Would someone please clear up / help me understand this.
I have a hash
my %allTariffData = {};
and in my sub
I have
my %tariffData = {};
do the processing etc
and then
return \%tariffData;
I now would like to use that data that in tariffData in another subroutine.
Would anyone be so kind as to share how I would go about this.
I can pass the whole hash, but that would not be right.
Kind Regards and thanks in advance
Brent Clark
| |
| Ankur Gupta 2005-07-28, 9:07 am |
| Brent Clark <mailto:bclark@eccotours.dyndns.org> wrote:
> Hi
Hi,
=20
> Would someone please clear up / help me understand this.
Sure.
=20
> I have a hash
>=20
> my %allTariffData =3D {};
I think you meant my %allTariffData =3D ();
> and in my sub
>=20
> I have
> my %tariffData =3D {};
Same here.. my %tariffData =3D ();
> do the processing etc
> and then
> return \%tariffData;
A reference of %tariffData is returned to the caller.
=20
> I now would like to use that data that in tariffData in another
> subroutine.=20
I guess your initial subroutine would be like this.
$ref_tariffData =3D func();
subroutine func returns refernce to a hash.
If you want to use it in another subroutine. simply pass the reference
of that hash to the subroutine.
func2($ref_tariffData);
sub func2 {
my ($ref_hash) =3D @_;
foreach my $key ( keys %$ref_hash ){
print "$key: $ref_hash->{$key}\n";
}
}
> Would anyone be so kind as to share how I would go about this.
>=20
> I can pass the whole hash, but that would not be right.
You can pass the whole hash but only if you are passing one hash.=20
return %hash; # OK
return %hash1, %hash2; # May not be OK
Please read the following perldocs:
perldoc perldsc
perldoc perlref
perldoc perllol
perldoc perldata
It will make the things a lot clearer to you.
--Ankur
I don't suffer from insanity, I enjoy every minute of it!
| |
| Tom Allison 2005-07-29, 10:00 pm |
|
> sub func2 {
> my ($ref_hash) = @_;
> foreach my $key ( keys %$ref_hash ){
> print "$key: $ref_hash->{$key}\n";
> }
> }
>
>
What you can't do here is:
sub func2 {
my %hash = @_;
....
}
You've broken the reference back to the original hash.
you will modify the elements in %hash, but not in your %tariffData
But you can declare a reference
my %tarrifData;
func1(\%tarrifData);
# %tarrifData has been changed
func2(\%tarrifData);
func3(\%tarrifData); # does not modify %tarrifData
sub func1 {
my $ref = shift; # points to %tarrifData
# Do Something
}
sub func2 {
my $ref = shift; # points again to %tarrifData
# Do something else
}
sub func3 {
my %hash = %{$_[0]}; # no longer points to %tarrifData
undef %hash; # who cares?
}
now go read the perldocs.. :)
|
|
|
|
|