Thanks for this. I wasn’t familiar with the Hash::Merge module, it’s a useful addition to the toolbox.

In case it helps anyone else, here’s how I got it to work with the OP’s script:

#! perl use strict; use warnings; use feature 'say'; use Data::Dump 'pp'; use Hash::Merge 'merge'; my %valid_inputs = ( one => 1, two => 2, ); validate( valid_inputs => \%valid_inputs, max_record_length => 72, ); sub validate { my %params = ( valid_inputs => { one => 'one', two => 'two', three => 'three', four => 'four', }, max_record_length => 24, ); Hash::Merge::set_behavior('RIGHT_PRECEDENT'); %params = %{ merge( \%params, { @_ } ) }; say "\%params =\n", pp(\%params); }

(The only slightly tricky part was turning @_ into a hash reference.) Output:

12:41 >perl 987_SoPW.pl %params = { max_record_length => 72, valid_inputs => { four => "four", one => 1, three => "three", t +wo => 2 }, } 12:42 >

I’d call that elegant.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: Default named params with an embedded hash by Athanasius
in thread Default named params with an embedded hash by neilwatson

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.