Struct::Diff - Recursive diff tools for nested perl structures
Version 0.55
use Struct::Diff qw(diff dselect dsplit patch); $a = {x => [7,{y => 4}]}; $b = {x => [7,{y => 9}],z => 33}; $diff = diff($a, $b, noO => 1, noU => 1); # omit unchanged and old values for changed items # $diff == {D => {x => {D => [{I => 1,N => {y => 9}}]},z => {A => 33}}}; @items = dselect($diff, fromD => ['z']); # get status for a particular key # @items == ({z => {A => 33}}); $href = dsplit($diff); # divide diff # $dsplit->{a} not exists # because unchanged omitted and all other items originated from $b # $dsplit->{b} == {x => [{y => 9}],z => 33}; patch($a, $diff); # $a now equal to $b
Nothing exports by default
Returns HASH reference to 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
Diff's keys shows status of each item in passed structures.
stands for 'added' (exists only in second structure), it's value - added item.
means 'different' and contains subdiff.
shows index for changed items (in arrays only).
is a new value for changed item.
Alike 'N', 'O' is a changed item's old value.
similar for 'A', but for removed items.
represent 'unchanged' items - common for both structures.
Where X is a status (A, N, O, R, U); such status will be omitted
Drop removed item's data
Returns items with desired status from diff's first level
@added = dselect($diff, states => { 'A' => 1 } # something added? @items = dselect($diff, states => { 'A' => 1, 'U' => 1 }, 'fromD' => [ 'a', 'b', 'c' ]) # from D hash @items = dselect($diff, states => { 'D' => 1, 'N' => 1 }, 'fromD' => [ 0, 1, 3, 5, 9 ]) # from D array
Select items from diff's 'D'. Expects list of positions (indexes for arrays and keys for hashes). All items with specified states will be returned if opt exists, but not defined or is an empty list.
Expects hash with desired states as keys with values in some true value. Items with all states will be returned if opt not defined
Divide diff to pseudo original structures.
$structs = dsplit($diff); # $structs->{'a'} - now contains items originated from original $a # $structs->{'b'} - same for $b
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.
Only scalars, refs to scalars, ref to arrays and ref to hashes correctly traversed. All other data types compared by their reference.
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/
Data::Diff
Array::Diff, Array::Compare, Algorithm::Diff, Data::Compare, Hash::Diff, Test::Struct, Struct::Compare Data::Structure::Util
Copyright 2015-2016 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.