http://www.perlmonks.org?node_id=920743


in reply to Re^3: get solution as short as possible
in thread get solution as short as possible

A(...) problem is that your last solution truncates hash if it has data. It's not appropriate for me. Though I'm not sure if moritz's version does this.

If you are objecting to destruction of %s, then mortiz's version does preserve the previous elements.

With a slight modification destructive construction changes to ...

%map = map { m/^ default_ (option_.+) $/x ? ( $1 => $def{ $_ } ) : () } keys %def ; @s{ keys %map } = values %map;

In any case ...

As there is no getting around search for interesting keys and generation of second key list, so just use ...

sub simple_fill { my ( $save , %def ) = @_; for ( keys %def ) { my ( $opt ) = ( $_ =~ m/^ default_ (option_.+) $/x ) or next; $save->{ $opt } = $def{ $_ }; } return; }

And then there is use of map|grep in void context for side effects, which some may not regard as "a nice looking, straightforward (...) version for everyday use".