|The stupid question is the question not asked|
Re: Subroutines: Returning a hash vs. hash referenceby demerphq (Chancellor)
|on Nov 28, 2002 at 09:39 UTC||Need Help??|
My question to you wise and wonderful monks is whether I should return a hash or a hash reference.
Why lock your users into only one option? Thats not the perl way to do things.
If their code makes more sense with a ref then give them one, if it makes more sense to get a list of key=>value pairs then give it to them.
I avoid the term "to get a hash" as there is no way to return a hash from a subroutine. Instead the hash must be listified into key=>value pairs and then returned that way. Its up to the user to shove that list into an actual hash (or not).
And since this approach involves returning a list then good old perlfunc:wantarray comes to the rescue:
Incidentally this is a standard idiom that can come in very useful. Consider the behaviour of localtime(). You could even insert the following before the return statement
to provide a debugging behaviour for when the sub is called in void context.