zss_text.gif

Struct::Compare


Download | Products

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.

  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:

  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:

  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:

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