Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Passing a hash into a subroutine

by Anonymous Monk
on Jan 08, 2004 at 08:36 UTC ( #319746=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I want to pass a associative array to the subroutine of the perl module as given below :
use Converter; my $document = new Converter; my $status = $document->convert( 'MIME-Type' => 'application/ms-word', 'Input' => '[In file]', 'Output' => '[Out file]' );
However , in the subroutine of the perl module Converter which i am making , I am unable to collect the associative array values.
How can I collect the values of MIME-TYPE,Input,Output in my subroutine .
Thankyou !

20040108 Edit by Corion: Fixed Unicode quotes
20040108 Edit by broquaint: title change (was Subroutine problem)

Replies are listed 'Best First'.
Re: Passing a hash into a subroutine
by davido (Cardinal) on Jan 08, 2004 at 09:06 UTC
    I'm not familiar with "". You probably need to pass the hash by reference. It could even be an anonymous hash, in which case, your syntax is very close. You just need the anonymous hash constructor:

    my $status = $document->convert( { 'MIME-Type' => 'application/ms-word', 'Input' => '[In file]', 'Output' => '[Out file] } );


Re: Passing a hash into a subroutine
by chimni (Pilgrim) on Jan 08, 2004 at 08:50 UTC

    Suggest you pass a reference to a hash and iterate over the keys to extract the value.
    &callsub(\%hash); sub callsub { my($ref)=@_; foreach my $key(keys %$ref){ print $ref->{$key}; } }

    Also suggest you get a copy of the "Perl cookbook" .Very handy for such day to day problems
Re: Passing a hash into a subroutine
by tune (Curate) on Jan 08, 2004 at 08:55 UTC
    You should provide the code where you are trying to collect the values of the hash. It is very hard to find one's error until the code is remaining hidden.

    Here is a tip until you do that:

    #!/usr/bin/perl use strict; use Data::Dumper; convert ( 'MIME-Type' => 'application/ms-word', 'Input' => '[In file]', 'Output' => '[Out file]' ); sub convert { my %hash = @_; # show the full thing print Dumper(\%hash); # or just one value print $hash{'Output'},"\n"; }
    I prefer passing hashrefs though...


Re: Passing a hash into a subroutine
by hmerrill (Friar) on Jan 08, 2004 at 13:46 UTC
    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.

Re: Passing a hash into a subroutine
by BUU (Prior) on Jan 08, 2004 at 10:05 UTC
    sub new { my $package = shift; # implicitly passed by the new package syntax my %opts = @_; print $opt{foo}; }
Re: Passing a hash into a subroutine
by ysth (Canon) on Jan 08, 2004 at 14:18 UTC
    There may be some confusion going on here. Will users of your Converter module actually have the parameters stored in a hash variable? Or do you just want to have your routine take "named parameters"? Most of the replies seem to have assumed the former, so for completeness I will assume the latter.

    Sample code:

    sub convert { # Since this is an object method, the object is passed as first arg +; my $self = shift; # now load the rest of the args into a hash my %arg = @_; if ($arg{"MIME-Type"} eq "application/ms-word") { install_openofficedotorg() ... }
    There are some problems with this. If the caller passes an odd number of parameters, you'll get a warning on that = @_ assignment statement. It also is hard to catch typos in the parameter names, e.g. MIME_Type => "application/ms-word" would get silently ignored, because it has _ instead of -.

    There are several modules, such as Params::Validate that help out when dealing with named parameters.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://319746]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-05-27 15:56 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (96 votes). Check out past polls.