Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: How best to validate the keys of hashref arguments?

by poj (Abbot)
on Mar 16, 2017 at 19:03 UTC ( #1184916=note: print w/replies, xml ) Need Help??


in reply to Re^3: How best to validate the keys of hashref arguments?
in thread How best to validate the keys of hashref arguments?

Like this perhaps ?

#!perl use strict; use Hash::Util 'lock_keys'; use Data::Dumper ; # valid my $cmd = ops_new_cmd({ name=>1, user=>2, host=>3}); print Dumper $cmd; # not valid $cmd = ops_new_cmd({ name=>1, user=>2, hostt=>3}); sub ops_new_cmd { my ($init_hash) = @_; my @valid = qw( name user host key command status ssh_retcode ssh_retmsg output std_err cmd_ret_code cmd_ret_msg); + lock_keys(my %new_cmd,@valid); %new_cmd = %$init_hash; return \%new_cmd; }
poj

Replies are listed 'Best First'.
Re^5: How best to validate the keys of hashref arguments?
by cbeckley (Curate) on Mar 16, 2017 at 19:36 UTC

    Yes it does, so does this:

    %$new_cmd = %$init_hash;

    Of course both assume I'm never gonna want to nest other hashrefs in my commands and initializers. I'm not, right? No. Never. Of course not ....

    Right. Ok, I'm going to use Clone.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-07-09 21:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?