Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Subroutines: Returning a hash vs. hash reference

by BrowserUk (Patriarch)
on Nov 28, 2002 at 04:29 UTC ( #216236=note: print w/replies, xml ) Need Help??


in reply to Subroutines: Returning a hash vs. hash reference

Personally, the decision as to whether to return a hash or a hash reference would be made on the basis of the size of the hash, and the frequency with which the subroutine is called. If the hash is small and the subroutine is only called a few times, I might return the whole thing simply because it makes the notation for using the hash much simpler in the calling code.

Of course, that can be overcome by passing a reference to a hash declared in the calling code to the sub and having it populate it. The return then becomes (almost)irrelevant, serving only as a marker that the hash may have been modified. Though you might opt to return undef if the sub was unable to complete its task, and the passed hashref if successful.

It's worth pointing out that your example sub could be replaced by my %INPUT = $CGI->Vars; or $INPUT = $CGI-Vars;.

See section "FETCHING THE PARAMETER LIST AS A HASH:" in the CGI.pm pod for more info.


Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Replies are listed 'Best First'.
Re(^2): CGI::Vars vs. CGI::param loop
by mt2k (Hermit) on Nov 28, 2002 at 04:58 UTC
    It's worth pointing out that your example sub could be replaced by my %INPUT = $CGI->Vars; or $INPUT = $CGI->Vars;
    Okay, time to bring something up here... I was earlier (a year ago or so here at perlmonks) in a discussion over using a for() loop vs. using CGI::Vars. IIRC, the general decision was to stick with a loop to copy the values into a hash, because of some compatability issues with the fact that CGI::Vars looks back on the old cgi-lib.pl library. Perhaps CGI.pm has been updated since then? Anyone have any input on this (again, after such a long time?)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://216236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-09-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?