Data::Merger - merge nested Perl data structures.
use Data::Merger qw(merger); my $target = { a => 2, e => { e1 => { }, }, }; my $source = { a => 1, e => { e2 => { }, e3 => { }, }, }; my $expected_data = { a => 1, e => { e1 => { }, e2 => { }, e3 => { }, }, }; my $merged_data = merger($target, $source); use Data::Comparator qw(data_comparator); my $differences = data_comparator($merged_data, $expected_data); if ($differences->is_empty()) { print "$0: 3: success\n"; ok(1, '3: success'); } else { print "$0: 3: failed\n"; ok(0, '3: failed'); }
Data::Merger contains subs to merge two nested perl data structures, overwriting values where appropriate. For scalars, default is to overwrite values. The two data structure can contain perl hashes, arrays and scalars, and should have the same overall structure (unless otherwise specified using options, see below). They should not be self-referential.
This module implements the functions merger(), merger_any(), merger_array() and merger_hash(). The main entry point is merger().
The merger() function is called with three arguments:
are the two data structures to be merged. The target data structure will be overwritten, and results are copied by reference. If you need plain copies, first Clone(3) your original data.
Options is a hash reference. There is currently one option:
If this value evals to 1, array entries are always overwritten, regardless of type / structure mismatches of the content of the entries in the arrays.
If this value evals to 1, hash entries are always overwritten, regardless of type / structure mismatches of the values of the tuples in the hashes.
Does only work with scalars, hashes and arrays. Support for self-referential structures seems broken at the moment.
This works for me to overwrite configuration defaults with specific values. Yet, is certainly incomplete.
Hugo Cornelis, hugo.cornelis@gmail.com
Copyright 2007 Hugo Cornelis.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Data::Transformator(3), Data::Comparator(3), Clone(3)
To install Data::Utilities, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Data::Utilities
CPAN shell
perl -MCPAN -e shell install Data::Utilities
For more information on module installation, please visit the detailed CPAN module installation guide.