9. Compare structures

The module Data::Compare compares two perl data structures recursively. The returning value is 0 if the structures differ, else 1.

Example 1

use Data::Compare;

@arr_1 = qw( 1 2 3 4 );# normal array
@arr_2 = sort( qw(4 3 2 1) );
$result = Compare(  \@arr_1, \@arr_2 ); # pass arguments as reference

($result) ? print("true\n") : print("false\n");# output: true
Example 2

use Data::Compare;

$anon_array_ref1 = ['Sebastian', 'Daniel', 'Florence'];# reference to anonymous array
$anon_array_ref2 = ['Max', 'Cornelius', 'Gustav'];

$result = Compare(  $anon_array_ref1, $anon_array_ref2 ); # arguments are references by definition

($result) ? print("true\n") : print("false\n");# output: false
Example 3

use Data::Compare;

$anon_hash_ref1 = { name => 'John', age => 25, city => 'New York' };# reference to anonymous hash
$anon_hash_ref2 = { name => 'John', age => 25, city => 'New York' };

$result = Compare( $anon_hash_ref1, $anon_hash_ref2 ); # arguments are references by definition

($result) ? print("true\n") : print("false\n");# output: true

Example 4

use Data::Compare;

@AoA_1 = ( # array of arrays
  ['red', 'orange', 'yellow'], ['green', 'blue'], 
);

@AoA_2 = (
  ['red', 'orange', 'yellow'], ['green', 'blue'], 
);

$result = Compare( \@AoA_1, \@AoA_2 ); # pass arguments as reference

($result) ? print("true\n") : print("false\n");# output: true
Example 5

use Data::Compare;

my @AoH_1 = ( # array of hashes
    { name => 'John', age => 25, city => 'New York' },
    { name => 'Alice', age => 30, city => 'London' },
    { name => 'Bob', age => 22, city => 'Paris' }
);

my @AoH_2 = (
    { name => 'John', age => 25, city => 'New York' },
    { name => 'Alice', age => 30, city => 'London' },
    { name => 'Bob', age => 22, city => 'Parijs' } # Parijs instead of Paris
);

$result = Compare(\@AoH_1, \@AoH_2); # pass arguments as reference

($result) ? print("true\n") : print("false\n");# output: false
Example 6

use Data::Compare;

my %HoA_1 = ( # hash of arrays
    key1 => [1, 2, 3],
    key2 => ['a', 'b', 'c'],
    key3 => [10, 20, 30]
);

my %HoA_2 = (
    key1 => [1, 2, 3],
    key2 => ['a', 'b', 'c'],
    key3 => [10, 20, 31] # 31 instead of 30
);

$result = (Compare(\%HoA_1, \%HoA_2)); # pass arguments as reference

($result) ? print("true\n") : print("false\n");# output: false
Example 7

use Data::Compare;

my %HoH_1 = (
    key1 => { name => 'John', age => 25, city => 'New York' },
    key2 => { name => 'Alice', age => 30, city => 'London' },
    key3 => { name => 'Bob', age => 22, city => 'Paris' }
);

my %HoH_2 = (
    key1 => { name => 'John', age => 25, city => 'New York' },
    key2 => { name => 'Alice', age => 30, city => 'London' },
    key3 => { name => 'Bob', age => 22, city => 'Amsterdam' } # Amsterdam instead of Paris
);

$result = Compare(\%HoH_1, \%HoH_2); # pass arguments as reference

($result) ? print("true\n") : print("false\n");# output: false