[Linux-disciples] perl question: why do modules return a reference
to a hash and not a hash?
Adam Rosi-Kessel
adam at rosi-kessel.org
Thu Dec 22 13:46:41 EST 2005
Stephen R Laniel wrote:
> On Thu, Dec 22, 2005 at 01:33:55PM -0500, Adam Rosi-Kessel wrote:
>> If someOtherFunc() returns a hash, however, then it will be a unitary item,
>> regardless of whether it is a hash, or a reference to hash.
>> No?
> I don't believe so, no. A hash is just an array. That's why
> you can do
I guess you're right, at least sort of. Viz:
--------
sub HashFunction {
my %hash = { 'a' => 1, 'b' => 2};
return %hash
}
sub ScalarFunction {
my $scalar = "bob";
return $scalar;
}
sub CheckFunction {
my $x = 1;
foreach (@_) {
print "Parameter $x = " . $_ . "\n";
$x++;
}
}
&CheckFunction(&HashFunction(), &ScalarFunction());
--------
The output is:
Parameter 1 = HASH(0x814cd38)
Parameter 2 =
Parameter 3 = bob
versus:
----------
sub HashFunction {
my %hash = { 'a' => 1, 'b' => 2};
return \%hash
}
sub ScalarFunction {
my $scalar = "bob";
return \$scalar;
}
sub CheckFunction {
my $x = 1;
foreach (@_) {
print "Parameter $x = " . $_ . "\n";
$x++;
}
}
&CheckFunction(&HashFunction(), &ScalarFunction());
--------
Where the output is:
Parameter 1 = HASH(0x8160654)
Parameter 2 = SCALAR(0x8163d68)
So apparently in the first example (where dereferenced variables are
returned), the function called gets *three* parameters, although the second
one is empty (?). In the second example (where references are returned),
only two variables are passed to CheckFunction, which is probably the
expected/desired behavior.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 250 bytes
Desc: OpenPGP digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20051222/17413162/signature-0001.pgp
More information about the Linux-disciples
mailing list