I agree with saintmike. What you want to do is better using OO. To use the approach you are now, you can do something like this. See my deepmerge function. It is not complete, but works for your sample data. To complete it you need to handle all data types.
#!/usr/bin/perl
use strict;
use Data::Dumper;
my %europe = (
skin => "euro-white",
language => {
default => "euro-englisch",
instrument => "euro-clarinet",
},
politics => "euro-democratic",
);
my %german = (
skin => "de-white",
language => {
default => "de-german"
}
);
my %bavarian = (
skin => "bav-white",
language => {
default => "bav-bavarian"
}
);
# this is not enough, german/language overwrites
# the complete hash of euro/language
my %woman = %europe;
deepmerge(\%woman, \%german);
deepmerge(\%woman, \%bavarian);
print Dumper(\%woman);
# result is this:
# $VAR1 = {
# 'skin' => 'bav-white',
# 'politics' => 'euro-democratic',
# 'language' => {
# 'default' => 'bav-bavarian'
# ** this is not here: instrument => "euro-clarinet",
# }
# };
unless ($woman{language}->{instrument}) {
die "Error: Language instrument is not there";
} else {
print "It worked\n";
}
sub deepmerge {
my ($into, $this) = @_;
for my $k (keys %$this) {
if (defined $k and
exists $into->{$k} and
defined $into->{$k} and
ref($into->{$k}) eq 'HASH' and
ref($this->{$k}) eq 'HASH') {
deepmerge($into->{$k}, $this->{$k});
} elsif (defined $k) {
$into->{$k} = $this->{$k};
}
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.