Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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]
[Lady_Aleena]: Lax, that would take it down to a little less than 100 characters. The module name is nearly 80 characters long.
Lady_Aleena has fumble fingers today.
[Lady_Aleena]: Sorry for the name typo LanX.
[LanX]: never lad_ena!
[LanX]: ;)
[Lady_Aleena]: IMO, Fancy::Join:: Grammatical isn't bad, but RolePlaying:: WorldBuilding:: WorldBuildersGuide book::ClimateTerra inSubsistance is on the long side.
[Lady_Aleena]: I'm still in reorganizing/ renaming mode.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2017-05-24 22:02 GMT
Find Nodes?
    Voting Booth?