Struct::Diff - Recursive diff tools for nested perl structures
Version 0.87
use Struct::Diff qw(diff dsplit list_diff patch); $a = {x => [7,{y => 4}]}; $b = {x => [7,{y => 9}],z => 33}; $diff = diff($a, $b, noO => 1, noU => 1); # omit unchanged items and old values # $diff == {D => {x => {D => [{I => 1,N => {y => 9}}]},z => {A => 33}}} $splitted = dsplit($diff); # $splitted->{a} # not exists # $splitted->{b} == {x => [{y => 9}],z => 33} @list_diff = list_diff($diff); # list (path and ref pairs) all diff entries # $list_diff == [[{keys => ['z']}],\{A => 33},[{keys => ['x']},[0]],\{I => 1,N => {y => 9}}] patch($a, $diff); # $a now equal to $b by structure and data
Nothing is exported by default.
Diff is simply a HASH whose keys shows status for each item in passed structures.
Stands for 'added' (exists only in second structure), it's value - added item.
Means 'different' and contains subdiff.
Index for changed array item.
Is a new value for changed item.
Alike N, O is a changed item's old value.
N
O
Similar for A, but for removed items.
A
Represent unchanged items.
Returns hashref to recursive diff between two passed things. Beware when changing diff: some of it's substructures are links to original structures.
$diff = diff($a, $b, %opts); $patch = diff($a, $b, noU => 1, noO => 1, trimR => 1); # smallest possible diff
Where X is a status (A, N, O, R, U); such status will be omitted.
R
U
Drop removed item's data.
List pairs (path, ref_to_subdiff) for provided diff. See "ADDRESSING SCHEME" in Struct::Path for path format specification.
@list = list_diff(diff($frst, $scnd);
Don't dive deeper than defined number of levels.
Defines how to traverse hash subdiffs. Keys will be picked randomely (keys behavior, default), sorted by provided subroutine (if value is a coderef) or lexically sorted if set to some other true value.
keys
Divide diff to pseudo original structures
$structs = dsplit($diff); # $structs->{a} - contains items originated from $a # $structs->{b} - same for $b
Deprecated. "list_diff" should be used instead.
Traverse through diff invoking callback function for subdiff statuses.
my $opts = { callback => sub { print "added value:", $_[0], "depth:", @{$_[1]}, "status:", $_[2]; return 1}, sortkeys => sub { sort { $a <=> $b } @_ } # numeric sort for keys under diff }; dtraverse($diff, $opts);
Don't dive deeper than defined number of levels
Mandatory option, must contain coderef to callback fuction. Four arguments will be passed to provided subroutine: value, path, status and ref to subdiff. Function must return some true value on success. Important: path (second argument) is actual for callback lifetime and will be immedeately changed afterwards.
Defines how will be traversed subdiffs for hashes. Keys will be picked randomely (depends on keys behavior, default), sorted by provided subroutine (if value is a coderef) or lexically sorted if set to some other true value.
Exact list of statuses. Sequence defines invocation priority.
Apply diff
patch($a, $diff);
Struct::Diff fails on structures with loops in references. has_circular_ref from Data::Structure::Util can help to detect such structures.
has_circular_ref
Only scalars, refs to scalars, ref to arrays and ref to hashes correctly traversed. All other data types compared by their references.
No object oriented interface provided.
Michael Samoglyadov, <mixas at cpan.org>
<mixas at cpan.org>
Please report any bugs or feature requests to bug-struct-diff at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Struct-Diff. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-struct-diff at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Struct::Diff
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Struct-Diff
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Struct-Diff
CPAN Ratings
http://cpanratings.perl.org/d/Struct-Diff
Search CPAN
http://search.cpan.org/dist/Struct-Diff/
Algorithm::Diff, Data::Deep, Data::Diff, Data::Difference, JSON::MergePatch
Data::Structure::Util, Struct::Path, Struct::Path::PerlStyle
Copyright 2015-2017 Michael Samoglyadov.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Struct::Diff, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Struct::Diff
CPAN shell
perl -MCPAN -e shell install Struct::Diff
For more information on module installation, please visit the detailed CPAN module installation guide.