Hugo Cornelis

NAME

Data::Merger - merge nested Perl data structures.

SYNOPSIS

    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');
    }

DESCRIPTION

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:

target and source 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

Options is a hash reference. There is currently one option:

{arrays}->{overwrite}

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.

{hashes}->{overwrite}

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.

BUGS

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.

AUTHOR

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.

SEE ALSO

Data::Transformator(3), Data::Comparator(3), Clone(3)