Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Passing a hash into a subroutine

by hmerrill (Friar)
on Jan 08, 2004 at 13:46 UTC ( #319801=note: print w/ replies, xml ) Need Help??


in reply to Passing a hash into a subroutine

Others have pointed this out already, but I'll try to explain a little more. The safest way to pass hashes is to pass-by-reference, or pass a reference to the hash instead of passing the hash itself.

You could do this:

use Converter; my $document = new Converter; my %convert_args_hash = ( 'MIME-Type' => 'application/ms-word', 'Input' => '[In file]', 'Output' => '[Out file]' ); my $convert_args_hashref = \%convert_args_hash; my $status = $document->convert($convert_args_hashref);
Or you could do as someone already suggested and use the anonymous hash constructor curly braces (which creates a reference to an anonymous hash), like this:
my $status = $document->convert( { 'MIME-Type' => 'application/ms-word', 'Input' => '[In file]', 'Output' => '[Out file]' } );
and then in the "convert" method (subroutine) you would just accept the hashref as the one and only argument, something like this:
sub convert { my $args_hashref = shift; print "The MIME-Type parameter = $args_hashref->{'MIME-Type'}\n" +; ### blah blah ### } # end sub convert
Hopefully you get the idea.

HTH.


Comment on Re: Passing a hash into a subroutine
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2015-07-30 04:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls