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

How do I pass hash in subroutine with multiple parameters
my %modHash #It holds values getHash({"pdtName"=>"$pdtName","modHash"=>\%modHash}); sub getHash{ my $pdtName= $_[0]{pdtName}; my %moduleHash= $_[0]{modHash}; while (($key, $value) = each(%moduleHash)){ print "key +:".$key.",Value:".$value."\n"; } }

Let me know if anyone knows to retrieve the hash value in subroutine

Replies are listed 'Best First'.
Re: How do I pass hash in subroutine with multiple parameters
by moritz (Cardinal) on Aug 18, 2009 at 12:17 UTC
    my %moduleHash= $_[0]{modHash};

    That should be  ... = %{ $_[0]{modHash} };

    See perlreftut for an explanation

    Perl 6 projects - links to (nearly) everything that is Perl 6.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How do I pass hash in subroutine with multiple parameters
by cdarke (Prior) on Aug 18, 2009 at 12:57 UTC
    Here is an alternative way of faking named parameters. This uses a hash inside the subroutine (%params):
    use strict; use warnings; my %modHash = qw (Ford Mundaneo Honda Civet Rolls Rice); my $pdtName = 'Fred Bloggs'; getHash(pdtName => $pdtName, modHash => \%modHash); sub getHash{ my %params = @_; my $pdtName= $params{pdtName}; my $moduleHashref= $params{modHash}; while (my ($key, $value) = each(%$moduleHashref)){ print "key: $key, Value: $value\n"; } }
Re: How do I pass hash in subroutine with multiple parameters
by Anonymous Monk on Aug 18, 2009 at 12:17 UTC