perlquestion
mt2k
<p>I am continually working on a set of modules for a system I am planning to use in the (hopefully) near future. I have come across something new that I have never given much thought to. This is not a problem I have come across, but more of a question of style and perhaps preference. In these modules, I have several subroutines that give a hash as a return value. My question to you wise and wonderful monks is whether I should return a hash or a hash reference.</p>
<readmore>
To explain perfectly well, this is an example of one of my subroutines:<p>
<code>
package Module::Name;
# require CGI, strict, constructor, all other code...
# A sub to return a [hash|hash reference] of CGI params.
sub get_params {
my $self = shift;
my %input;
$input{$_} = $self->{$cgi_handle}->param($_)
foreach $self->{$cgi_handle}->param());
# The two possible ways of returning the hash.
# Is there a preference (style or personal)
# as to which one I should use?
return %input; # return hash
# OR
return \%input; # return hash reference
}
</code>
<p>The first method of returning the hash (<small>return %input;</small>), would be handled as such:</p>
<code>
my $CGI = new Module::Name;
my %INPUT = $CGI->get_params();
print $INPUT{'node'};
</code>
<p>The second method of returning the has (<small>return \%input;</small>), would be handled as such:</p>
<code>
my $CGI = new Module::Name;
my $INPUT = $CGI->get_params();
print $INPUT->{'node'};
</code>
My guess is that this is probably a completely personal preference rather than style, but I might be wrong. If it is personal preference, I'd like to know which method other perl users prefer and perhaps any reason(s) as to why. :)
</readmore>