Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am writing some routines to handle data passed to me by users over the web via our webserver. I want to write a function that takes a hash for the output results. Does anyone know how to handle hashes being passed to a Function? THNX -- LiteStar

Replies are listed 'Best First'.
The solution is to pass-by-reference
by Olecram (Initiate) on Apr 22, 2002 at 09:03 UTC
    Here you are.
    The example should explain you quite clearly how to solve your prob.

    cheers!
    #!usr/bin/perl sub meteathash() { #the parameter is passed by reference via the \ my $varhashref= shift; #we need to CAST the reference as a hash #Same method would be used for arrays too %varlocalhash= %$varhashref; foreach $varkey (keys (%varlocalhash)) { print STDERR "\nKey local: $varkey Value: " . $varlocalhash{$v +arkey}; } } @varkeyz= ('a','b','c','d'); @varkeyztwo= ('aa','bb','cc','dd'); @varvaluez=(1,2,3,4); =comment print STDERR "\nkeyz: @varkeyz"; print STDERR "\nvalz: @varvaluez"; =cut @varhash{@varkeyz}= @varvaluez; @varhashtwo{@varkeyztwo}= @varvaluez; =comment foreach $varkey (keys (%varhash)) { print STDERR "\nKey: $varkey Value: " . $varhash{$varkey}; } =cut &meteathash(\%varhash); print STDERR "\nherecomesthesecondhash"; &meteathash(\%varhashtwo);
Re: Passing Hashes to Functions
by straywalrus (Friar) on Apr 22, 2002 at 22:37 UTC
    This worked fine on my Slakware 7.1 box w/ perl 5.6.0
    #!/usr/bin/perl some(TK=>OK,PERL=>COOL,LISP=>AWESOME); print "Done!\n"; sub some { %localhash = @_; foreach $localkey (keys (%localhash)) { print "Key: $localkey \n Value: $localhash{$localkey}\n"; } }
Re: Passing Hashes to Functions
by Anonymous Monk on Apr 22, 2002 at 02:03 UTC
    I answered my own stupid question! I am rather stupid sometimes.