Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2015-11-30 08:40 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (765 votes), past polls