Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Accept user options with defaults and report unknowns.

by Aristotle (Chancellor)
on May 30, 2003 at 00:15 UTC ( [id://261731]=note: print w/replies, xml ) Need Help??


in reply to Accept user options with defaults and report unknowns.

I would generalize that a bit unless I only need it once.
sub get_unknown_keys (\%;) { my $hash = shift; my %allow; @allow{keys %$hash} = (); delete @allow{@_}; return keys %allow; } sub func { my %arg = ( foo => 1, bar => 'baz', quux => 42 ); my @allow = keys %arg; %arg = (%arg, @_); if(my @unknown = get_unknown_keys %arg, @allow) { carp "Ignoring unkown parameters: @unknown"; delete @arg{@unknown}; } # do stuff. }
I have to say I quite like the way delete @arg{@unknown} reads.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Accept user options with defaults and report unknowns.
by BrowserUk (Patriarch) on May 30, 2003 at 03:37 UTC

    Part of me agree's, and enevitably, part doesn't.

    Three lines repeated twice becomes a sub? Use the sub in two modules, it becomes a module in its own right? Then it need pod, and a name and hell, I might as well throw in some validation of the values, so I need an api. Might as well make it OO. Oops! I re-invented Params::Validate.

    Now why did I reject Params::Validate above? Oh. because I couldn't see the point in replacing 3 lines with a module and 3 lines.

    use My::Params::Validate; my $validator = My::Params::Validate->new( Foo=>[ qw[foo|bar|qux] ], Bar=>[ 100 .. 1000 ], Qux=>[ 2e9, 3e8 ], ); sub myfunction{ my %opts = $validator %defs, @_; ... }

    Ah! But now I need some code to validate the arguements to My::Params::Validate.

    Should I subclass it? Use a virtual base class? What about exception handling?

    OMG - I just re-invented Java:)

    I do like delete @args{@unknown} (Note the S. I'll have to see if I can work that in somewhere:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found