Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Passing a hash into a subroutine

by ysth (Canon)
on Jan 08, 2004 at 14:18 UTC ( #319807=note: print w/replies, xml ) Need Help??

in reply to Passing a hash into a subroutine

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
Node Status?
node history
Node Type: note [id://319807]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-05-21 17:09 GMT
Find Nodes?
    Voting Booth?