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

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

by poj (Abbot)
on Mar 16, 2017 at 19:03 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184916]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found