Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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.


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-09-17 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (55 votes), past polls