zenspider.com by ryan davis

Compares two values of any type and structure and returns true if they are the same. It does a deep comparison of the structures, so a hash of a hash of a whatever will be compared correctly.

1
2
    use Struct::Compare;
    my $is_different = compare($ref1, $ref2);

This is especially useful for writing unit tests for your modules! For example, instead of writing:

1
2
3
    my $result = my_test_function($data);
    assert("Field 'a' must be correct", 1 == $result->{'a'});
    assert("Field 'b' must be correct", 2 == $result->{'b'});

Write this instead:

1
2
    assert("The data must be correct",
           compare({'a' => 1, 'b' => 2}, my_test_function($data)));

It becomes even more apparent when the structure is nested:

1
2
3
4
5
6
7
8
    assert("The data must be correct",
           compare({'a' => {
                            'b' => 1,
                            'c' => 2,
                            },
                    'b' => [1, 2, 3],
                    },
                   my_test_function($data)));