[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 10:12:15 EST 2005


Does anyone (particularly dpt, ken, slaniel) know why so many perl database
functions return references to hashes rather than hashes?

E.g., an example select query in DBIx::Abstract looks like this:

my $result = $db->select_one_to_hashref({
                                        field =>'*',
                                        table =>'users',
                                        where => { uid => $u }
                                       })


$result is then a reference to a hash containing the results; in this case
just the first matching record is requested, and the hash needs to be
dereferenced to get to the data, e.g.:

$$result{'uid'}

Alternatively, it can be dereferenced at the time of assignment:

my %result = %{$db->select_one_to_hashref({...})};

(I think this syntax causes an error, however, when the query returns no
results).

In my experience, I *always* want to dereference the hash right away, since
I want the result of the query.

I've noticed other perl modules similar return references to hashes rather
than the hash itself.

Is there a good reason for that?

-------------- 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/c411f308/signature.pgp


More information about the Linux-disciples mailing list