The purpose is to only ammend the hashref argument being passed to the function, but not modify the original hashref.
{
my $hr = { foo => q{bunnyfoofoo},
bar => q{barbiebarbar} };
foo( { %$hr, baz => q{bazkethound} } );
}
sub foo {
my $args = shift // {};
}
I think the above would work, but am not sure there is a better way (other than modifying the orginal ref before passing).
Your code is as simple as it can be. You need to pass a hash and you don't want to modify the existing hash, so you need to create a new hash. That's what
{ }
does.The underlying problem is
foo
takes a hash ref instead of a key-value list.You can still do everything you did before:
There are two advantages.
You don't have to use
{ }
all over the place.You can modify the argument safely.
This is buggy:
This is ok: