Passing a reference as a reference

P

pamelamishra

Hi,

I have this question about Perl references.
Suppose i have this %hash which has been populated with key -
values.Subroutine check1 gets the %hash as a refrence from the main
and this refrence has to be passed to another subroutine check2. How
can this be done.Any suggestions would be helpful.
##############################
my %hash;
check1(\%hash);

sub check1
{
my ($hash)=shift;
foreach my $keys (keys $hash)
{
print"$hash{$keys}\n";
}
check2 (I want to send the reference of %hash--which is already a
reference);
}

sub check2
{
my ($hash)=shift;
}

######################
Thanks
 
P

Paul Lalli

I have this question about Perl references.
Suppose i have this %hash which has been populated with key -
values.Subroutine check1 gets the %hash as a refrence from the main
and this refrence has to be passed to another subroutine check2. How
can this be done.Any suggestions would be helpful.
##############################
my %hash;
check1(\%hash);

sub check1
{
my ($hash)=shift;

All fine so far. Only minor problem you have is that you've named
your hash reference $hash. I would call it something like $h_ref or
$hash_ref or something similar.
foreach my $keys (keys $hash)

Problem here. $hash is not a hash. It's a reference to a hash. If
you want to get at the hash that it references, you have to
dereference the reference:

foreach my $keys (keys %{$hash})
{
print"$hash{$keys}\n";

Again. $hash is not a hash. You have to dereference:

print "$hash->{$keys}\n";
}

check2 (I want to send the reference of %hash--which is already a
reference);

No. %hash is the hash that you created in the "main" part of the
script. $hash is a reference to %hash.

There's nothing magical here. Just pass the reference directly:

check2($hash);
}

sub check2
{
my ($hash)=shift;

}

Again, though, I rather strongly recommend you choose better names for
your variables. Specifically, name references with some form of
"_ref" on them, to remind you what they actually are.

Paul Lalli
 
Q

QoS

Hi,

I have this question about Perl references.
Suppose i have this %hash which has been populated with key -
values.Subroutine check1 gets the %hash as a refrence from the main
and this refrence has to be passed to another subroutine check2. How
can this be done.Any suggestions would be helpful.
##############################
my %hash;
check1(\%hash);

sub check1
{
my ($hash)=shift;
foreach my $keys (keys $hash)
{
print"$hash{$keys}\n";
}
check2 (I want to send the reference of %hash--which is already a
reference);
}

sub check2
{
my ($hash)=shift;
}

######################
Thanks

If im not mistaken check2($hashRef) should work fine, is there a particular
reason why you want to pass a reference to a reference rather than just pass
the reference?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top