in reply to Assigning list with duplicate keys to hash

In Perl Best Practices, TheDamian recommends using a hash for default parameters like so:

my %DEFAULTS = ( a => 1, b => 2 ); sub foo { my $arg_ref = shift; my %args = ref $arg_ref eq ref {} ? ( %DEFAULTS, %{$arg_ref} ) : %DEFAULTS; }

He clearly expects the second hash to overwrite the duplicated contents of the first, so I think it's safe to say this is defined behavior that you can rely on.

Replies are listed 'Best First'.
Re^2: Assigning list with duplicate keys to hash
by Your Mother (Archbishop) on Apr 22, 2008 at 17:23 UTC

    In his OOP book he gives a much clearer, to me, way to do (almost) the same thing. I use it often. Dovetails nicely with Params::Validate.

    sub bar { my %args = ( mi => "default", mo => "default", mu => "default", @_ # list of args in key/val pairs ); # ... }