Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

function w complex args

by pileofrogs (Priest)
on Mar 13, 2007 at 22:43 UTC ( [id://604703]=perlquestion: print w/replies, xml ) Need Help??

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

I've run across several functions that handle complex args in a way that I like. They will take a few args as just args or they can take a hash ref (and maybe a hash) or maybe they can handle a combination. Like so:

blargh('/tmp/foo'); # or blargh('/tmp/foo','pileofrogs@example.com'); # or blargh('/tmp/foo','pileofrogs@example.com', { enable_giant_squid => 1, squid_name => 'Bob' }); # or even blargh( file => '/tmp/foo', email => 'pileofrogs@example.com', enable_giant_squid => 1, squid_name => 'Bob');

The args that you always or almost always set can be set by normal positional args, but more detailed tweaks can be passed in a hash ref or you can pass everything as a hash.

Anyway, I've seen this (or something like this) in several places and I'd like to do it too. Is there a recommended way to handle this? A best practice?

Thanks!
--Pileofrogs

Replies are listed 'Best First'.
Re: function w complex args
by fmerges (Chaplain) on Mar 13, 2007 at 23:29 UTC

    Hi,

    I like the way that is also recommended in the book Perl Best Practices by Damian Conway (O'Reilly).

    It's passing a hashref to the subroutine, like:

    blargh({ file => '/tmp/foo', email => 'pileofrogs@example.com', enable_squid => 1, squid_name => 'Bob', }); sub blargh { my $args = shift; ... }

    And then you test for the existence or the required contraints on the arguments passed.

    Regards,

    fmerges at irc.freenode.net
Re: function w complex args
by ferreira (Chaplain) on Mar 14, 2007 at 02:31 UTC

    I do believe named arguments could be considered a best practice. (I wish "they" used it more in those awful PL/SQL code that hurts my eyes and soul, but many don't even know Oracle supports it. But I am digressing.)

    As fmerge said, Damian Conway included it in his book. They read as:

    Use a hash of named arguments for any subroutine that has more than three parameters.

    And you can read more about the rationale of using named parameters and living better at a perl.com article by dragonchild.

Re: function w complex args
by GrandFather (Saint) on Mar 13, 2007 at 23:32 UTC
Re: function w complex args
by jdtoronto (Prior) on Mar 14, 2007 at 02:47 UTC
    It's a perfectly valid, and very useful technique. My suggestion is that you do things one way or another - don't mix the named parameters and the positional ones in the same place. Although I have seen it done and in fact it is done in Perl/Tk in a few places.

    jdtoronto

Re: function w complex args
by izut (Chaplain) on Mar 14, 2007 at 18:04 UTC

    My two cents about it is: choose one and use it with consistency. I think the developer that uses it must use it the way you intended your piece of code to be used. If he/she uses it in an unappropriate way, it is not your fault because you wrote on your docs the way you thought they would use it.

    Igor 'izut' Sutton
    your code, your rules.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://604703]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-19 08:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found