in reply to Default named params with an embedded hash

While I currently favour hash slices a la Modern Perl, I couldn't quickly figure out how to get the valid_inputs and max_record_length in one line.
my (%hash) = @_; @{$params{valid_inputs}}{keys $hash{valid_inputs}} = values $hash{valid_inputs}; $params{max_record_length} = $hash{max_record_length} || $params{max_record_length};

Oh, and thanks again for the steak!

Sometimes I can think of 6 impossible LDAP attributes before breakfast.

Replies are listed 'Best First'.
Re^2: Default named params with an embedded hash
by Athanasius (Archbishop) on Aug 30, 2014 at 13:37 UTC

    Hello Ea,

    Using hash slices is a clever strategy here!, and the Modern Perl link is a useful one. I must admit, I’m still trying to get my head around the @{ ... }{ ... } syntax, which is perfectly correct, but looks wrong (to me).

    But this:

    $params{max_record_length} = $hash{max_record_length} || $params{max_record_length};

    makes it impossible to override $params{max_record_length} with a zero value. Easily fixed:

    $params{max_record_length} = $hash{max_record_length} // $params{max_record_length};

    See perlop#Logical-Defined-Or.

    Cheers,

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