PerlDancer/Dancer2 — Issue #31: Full config merging support
Author: bigpresh
Created: 2012-01-06 22:20:10+00:00
State: open
Labels: Wishlist

I think Dancer2 would be a good time to introduce full-on config merging, rather than just the top-level.

For instance, given:

```yaml
#config.yml:
foo:
bar:
wibble: 1
beer: 2
baz:
test: 3

# environments/development.yml:
foo:
bar:
beer: 4
```

... after they got merged, the end result currently would be:

```yaml
foo:
bar: beer
baz:
test: 3
```

It would be nice to walk through and merge fully.

Catalyst has some code in `Catalyst::Utils` we could use:
https://metacpan.org/module/Catalyst::Utils#merge_hashes-hashref-hashref-

```perl
sub merge_hashes {
my ( $lefthash, $righthash ) = @_;

return $lefthash unless defined $righthash;

my %merged = %$lefthash;
for my $key ( keys %$righthash ) {
my $right_ref = ( ref $righthash->{ $key } || '' ) eq 'HASH';
my $left_ref = ( ( exists $lefthash->{ $key } && ref $lefthash->{ $key } ) || '' ) eq 'HASH';
if( $right_ref and $left_ref ) {
$merged{ $key } = merge_hashes(
$lefthash->{ $key }, $righthash->{ $key }
);
}
else {
$merged{ $key } = $righthash->{ $key };
}
}

return \%merged;
}
```

There's also e.g. `Hash::Merge`, `Hash::Merge::Simple` and others on CPAN, if we were willing to add a dependency for it (it's simple enough that it's probably best to just use the above Catalyst code (with attribution, of course) assuming licences permit.