[Linux-disciples] perl question: why do modules return
a reference to a hash and not a hash?
Stephen R Laniel
steve at laniels.org
Thu Dec 22 11:13:52 EST 2005
On Thu, Dec 22, 2005 at 10:24:45AM -0500, Adam Rosi-Kessel wrote:
> 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?
I just thought of a better answer, based on my own
experiences. The trouble is that Perl collapses multiple
lists into one single list. So then if you call
&someFunc( func1(), func2() );
and func1() and func2() both return lists, Perl will treat
the arguments as a single long list. Then in the
implementation of someFunc, you want to treat them as two
separate lists, and you can't: by the time someFunc sees the
arguments, they're collapsed into one list.
So if you're writing a function for a library, I think the
smarter idea is to write everything to return references.
Then you can do
sub someFunc {
my @inArgs = @_;
my @thisList = ();
foreach my $arg ( @inArgs ) {
@thisList = @{$arg};
foreach my $insideArg (@thisList) {
doSomething;
}
}
}
If the DBI functions returned raw objects, you'd have to
make references out of them anyway to use them inside other
functions, then dereference them inside the other functions.
This is my latest guess. I still have no idea what the
official answer is, but that seems plausible.
--
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
PGP key: http://laniels.org/slaniel.key
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20051222/4c57cb4f/attachment.pgp
More information about the Linux-disciples
mailing list