for my $k (keys %$init_hash) { $new_cmd->{$k} = $init_hash->{$k}; }
Is there a more idiomatic way to say that?

Several, but it seems to me you're just copying over every value from %$init_hash into %$new_cmd, so it seems easiest to write:

my %new_cmd = %$init_hash;

This creates the same shallow copy of the hash that your current code is creating. You can lock_keys afterwards and incorrect keys will still cause corresponding errors. In the interest of TIMTOWTDI, here's a couple other solutions, most of which would be better applicable if you only wanted to copy over a subset of the keys. Note that the latter three clobber the entire contents of the hash, which should be fine in your case:

sub ops_new_cmd { my ($init_hash) = @_; my %new_cmd; # -OR- #my %new_cmd = %$init_hash; lock_keys(%new_cmd, qw/name user host command/); my @keyset = keys %$init_hash; #for my $k (@keyset) { $new_cmd{$k} = $init_hash->{$k}; } # -OR- #$new_cmd{$_} = $init_hash->{$_} for @keyset; # -OR- %new_cmd = map { $_=>$init_hash->{$_} } @keyset; # -OR- #use 5.020; # For Key/Value Slices #%new_cmd = %$init_hash{ @keyset }; # -OR- #use experimental 'postderef'; # For Postfix Deref + Key/Value Sli +ces #%new_cmd = $init_hash->%{ @keyset }; return \%new_cmd }

As for locked hashes, there was some discussion on P5P recently about them, but even if something were to happen to this feature, tied hashes would be an easy replacement. For example, a quick search on CPAN shows Tie::Hash::FixedKeys. On the other hand, if you start taking locked hashes that seriously, it's probably better to start moving to OO.

Update in response to your reply, since I should have included it here in the first place: Personally what I currently use locked hashes for is mostly typo prevention, which is helpful during development, but could also be removed later without really affecting the code.


In reply to Re^4: How best to validate the keys of hashref arguments? (updated) by haukex
in thread How best to validate the keys of hashref arguments? by cbeckley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.