NAME
Data::Hash::Diff::Smart::Cookbook - Practical examples for Data::Hash::Diff::Smart
SYNOPSIS
perldoc Data::Hash::Diff::Smart::Cookbook
BASIC DIFFING
use Data::Hash::Diff::Smart qw(diff_text);
my $old = { name => 'Nigel', age => 40 };
my $new = { name => 'N. Horne', age => 40 };
print diff_text($old, $new);
IGNORING PATHS
Ignore specific fields:
diff($old, $new, ignore => ['/timestamp']);
Ignore all debug fields:
diff($old, $new, ignore => [ qr{^/debug} ]);
Wildcard ignore:
diff($old, $new, ignore => ['/users/*/password']);
CUSTOM COMPARATORS
Numeric tolerance:
diff($old, $new,
compare => {
'/price' => sub { abs($_[0] - $_[1]) < 0.01 },
}
);
Case-insensitive string comparison:
compare => {
'/name' => sub { lc($_[0]) eq lc($_[1]) },
}
ARRAY MODES
Index mode (default)
diff($old, $new, array_mode => 'index');
LCS mode (minimal diffs)
diff($old, $new, array_mode => 'lcs');
Unordered mode (treat arrays as sets)
diff($old, $new, array_mode => 'unordered');
RENDERING OUTPUT
Text
print diff_text($old, $new);
JSON
my $json = diff_json($old, $new);
YAML
my $yaml = diff_yaml($old, $new);
Test2 diagnostics
diag diff_test2($old, $new);
ADVANCED: CYCLE DETECTION
Self-referential structures are handled safely:
my $a = {};
$a->{self} = $a;
my $b = {};
$b->{self} = $b;
diff($a, $b); # no infinite recursion
AUTHOR
Nigel Horne