| 1 | package Array::Diff; | ||||||||||||||||||||||||||||
| 2 | 4 | $Array::Diff::VERSION = '0.09'; | |||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Algorithm::Diff 1.19; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 0 | ▶ | eval q{ use Algorithm::Diff::XS; }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | __PACKAGE__->mk_accessors(qw/added deleted count diff_class/); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Array::Diff - Find the differences between two arrays | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my @old = ( 'a', 'b', 'c' ); | ||||||||||||||||||||||||||||
| 20 | my @new = ( 'b', 'c', 'd' ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $diff = Array::Diff->diff( \@old, \@new ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $diff->count # 2 | ||||||||||||||||||||||||||||
| 25 | $diff->added # [ 'd' ]; | ||||||||||||||||||||||||||||
| 26 | $diff->deleted # [ 'a' ]; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | This module compares two B<pre-sorted> arrays | ||||||||||||||||||||||||||||
| 31 | and returns the added or deleted elements in two separate arrays. | ||||||||||||||||||||||||||||
| 32 | It's a simple wrapper around L<Algorithm::Diff>. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | B<Note>: the arrays must be sorted before you call C<diff>. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | And if you need more complex array tools, check L<Array::Compare>. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 METHODS | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =over 4 | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item new () | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Create a new C<Array::Diff> object. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 2 | my $self = shift->SUPER::new(@_); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | $self->{diff_class} ||= $INC{'Algorithm/Diff/XS.pm'} ? 'Algorithm::Diff::XS' : 'Algorithm::Diff'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"diff_class"} ||= $INC{"Algorithm/Diff/XS.pm"} ? "Algorithm::Diff::XS" : "Algorithm::Diff"
MC/DC0%0/2
$self->{"diff_class"} ||= $INC{"Algorithm/Diff/XS.pm"} ? "Algorithm::Diff::XS" : "Algorithm::Diff"
$self->{"diff_class"}
$INC{"Algorithm/Diff/XS.pm"} ? "Algorithm::Diff::XS" : "Algorithm::Diff"
Truth table33%1/3
A ||= B
A= $self->{"diff_class"}B= $INC{"Algorithm/Diff/XS.pm"} ? "Algorithm::Diff::XS" : "Algorithm::Diff"
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item diff ( OLD, NEW ) | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Compute the differences between two arrays. The results are stored | ||||||||||||||||||||||||||||
| 59 | in the C<added>, C<deleted>, and C<count> properties that may be | ||||||||||||||||||||||||||||
| 60 | examined using the corresponding methods. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | This method may be invoked as an object method, in which case it will | ||||||||||||||||||||||||||||
| 63 | recalculate the differences and repopulate the C<count>, C<added>, and | ||||||||||||||||||||||||||||
| 64 | C<removed> properties, or as a static method, in which case it will | ||||||||||||||||||||||||||||
| 65 | return a newly-created C<Array::Diff> object with the properties | ||||||||||||||||||||||||||||
| 66 | set appropriately. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | sub diff { | ||||||||||||||||||||||||||
|
Subroutine
diff: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 2 | my ( $self, $old, $new ) = @_; | |||||||||||||||||||||||||||
| 72 | 2 | ▶ | $self = $self->new unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | $self->added( [] ); | |||||||||||||||||||||||||||
| 75 | 2 | $self->deleted( [] ); | |||||||||||||||||||||||||||
| 76 | 2 | $self->count( 0 ); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | my $diff = $self->diff_class->new( $old, $new ); | |||||||||||||||||||||||||||
| 79 | 2 | ▶ | while ( $diff->Next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | next if $diff->Same; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 3 | my @deleted = $diff->Items(1); | |||||||||||||||||||||||||||
| 83 | 3 | my @added = $diff->Items(2); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | $self->{count} += @added + @deleted; | |||||||||||||||||||||||||||
| 86 | 3 | ▶ | push @{$self->{deleted}}, @deleted if @deleted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | push @{$self->{added}}, @added if @added; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | $self; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item added ( [VALUES ] ) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Get or set the elements present in the C<NEW> array and absent in | ||||||||||||||||||||||||||||
| 96 | the C<OLD> one at the comparison performed by the last C<diff()> | ||||||||||||||||||||||||||||
| 97 | invocation. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =item deleted ( [VALUES] ) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Get or set the elements present in the C<OLD> array and absent in | ||||||||||||||||||||||||||||
| 102 | the C<NEW> one at the comparison performed by the last C<diff()> | ||||||||||||||||||||||||||||
| 103 | invocation. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item count ( [VALUE] ) | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Get or set the total number of added or deleted elements at | ||||||||||||||||||||||||||||
| 108 | the comparison performed by the last C<diff()> invocation. | ||||||||||||||||||||||||||||
| 109 | This count should be equal to the sum of the number of elements in | ||||||||||||||||||||||||||||
| 110 | the C<added> and C<deleted> properties. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =back | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | L<Array::Compare> - performs the same function as this module, | ||||||||||||||||||||||||||||
| 117 | but has options for controlling how it works. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | L<List::Compare> - similar functionality, but again with more options. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | L<Algorithm::Diff> - the underlying implementation of the diff algorithm. | ||||||||||||||||||||||||||||
| 122 | If you've got L<Algorithm::Diff::XS> installed, that will be used. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | L<YAML::Diff> - find difference between two YAML documents. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | L<HTML::Differences> - find difference between two HTML documents. | ||||||||||||||||||||||||||||
| 127 | This uses a more sane approach than L<HTML::Diff>. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | L<XML::Diff> - find difference between two XML documents. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | L<Hash::Diff> - find the differences between two Perl hashes. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | L<Data::Diff> - find difference between two arbitrary data structures. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | L<Text::Diff> - can find difference between two inputs, which can be | ||||||||||||||||||||||||||||
| 136 | data structures or file names. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Daisuke Murase <typester@cpan.org> | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Copyright (c) 2009 by Daisuke Murase. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 147 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 150 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 4 | 1; | |||||||||||||||||||||||||||