[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:24:45 EST 2005


Stephen R Laniel wrote:
> On Thu, Dec 22, 2005 at 10:12:15AM -0500, Adam Rosi-Kessel wrote:
>> Does anyone (particularly dpt, ken, slaniel) know why so many perl database
>> functions return references to hashes rather than hashes?
> My first inclination is to suggest that it's the same reason
> why everyone recommends passing arguments by reference in C
> and C++: because it uses much less memory. When you pass
> around a raw object as an argument, rather than a reference
> to that raw object, C and C++ make a copy of that object
> onto the stack for that function. So you pass a reference
> around, which is always a fixed size (equal to the size of a
> word in that machine's memory). Then when you need to play
> with the object itself, you dereference it.

That sounds plausible, but I also would have expected perl to automatically
optimize on this level. In my limited experience in this area, it's actually
hard to know what effect on memory usage a particular approach will have in
perl--usually you just leave it up to the interpreter to make the best
decisions.

Also, since this is a value being returned, does it really matter if it
makes a copy since the function is now over and its stack would be destroyed?

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


More information about the Linux-disciples mailing list