File Coverage

blib/lib/Array/Diff.pm
Criterion Covered Total %
statement 32 34 94.1
branch 8 10 80.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 2 2 100.0
total 50 56 89.2


line stmt bran cond sub pod time code
1             package Array::Diff;
2             $Array::Diff::VERSION = '0.09';
3 2     2   139826 use strict;
  2         64  
  2         61  
4 2     2   11 use warnings;
  2         4  
  2         58  
5 2     2   9 use base qw/Class::Accessor::Fast/;
  2         4  
  2         1068  
6              
7 2     2   7055 use Algorithm::Diff 1.19;
  2         10599  
  2         538  
8 2     2   312 eval q{ use Algorithm::Diff::XS; };
  0         0  
  0         0  
9              
10              
11             __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 arrays
31             and returns the added or deleted elements in two separate arrays.
32             It's a simple wrapper around L.
33              
34             B: the arrays must be sorted before you call C.
35              
36             And if you need more complex array tools, check L.
37              
38             =head1 METHODS
39              
40             =over 4
41              
42             =item new ()
43              
44             Create a new C object.
45              
46             =cut
47              
48             sub new {
49 2     2 1 16 my $self = shift->SUPER::new(@_);
50              
51 2 50 33     28 $self->{diff_class} ||= $INC{'Algorithm/Diff/XS.pm'} ? 'Algorithm::Diff::XS' : 'Algorithm::Diff';
52              
53 2         6 $self;
54             }
55              
56             =item diff ( OLD, NEW )
57              
58             Compute the differences between two arrays. The results are stored
59             in the C, C, and C 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, C, and
64             C properties, or as a static method, in which case it will
65             return a newly-created C object with the properties
66             set appropriately.
67              
68             =cut
69              
70             sub diff {
71 2     2 1 3719 my ( $self, $old, $new ) = @_;
72 2 50       10 $self = $self->new unless ref $self;
73              
74 2         56 $self->added( [] );
75 2         53 $self->deleted( [] );
76 2         52 $self->count( 0 );
77              
78 2         48 my $diff = $self->diff_class->new( $old, $new );
79 2         461 while ( $diff->Next ) {
80 4 100       117 next if $diff->Same;
81              
82 3         76 my @deleted = $diff->Items(1);
83 3         59 my @added = $diff->Items(2);
84              
85 3         48 $self->{count} += @added + @deleted;
86 3 100       10 push @{$self->{deleted}}, @deleted if @deleted;
  2         5  
87 3 100       9 push @{$self->{added}}, @added if @added;
  2         6  
88             }
89              
90 2         51 $self;
91             }
92              
93             =item added ( [VALUES ] )
94              
95             Get or set the elements present in the C array and absent in
96             the C one at the comparison performed by the last C
97             invocation.
98              
99             =item deleted ( [VALUES] )
100              
101             Get or set the elements present in the C array and absent in
102             the C one at the comparison performed by the last C
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 invocation.
109             This count should be equal to the sum of the number of elements in
110             the C and C properties.
111              
112             =back
113              
114             =head1 SEE ALSO
115              
116             L - performs the same function as this module,
117             but has options for controlling how it works.
118              
119             L - similar functionality, but again with more options.
120              
121             L - the underlying implementation of the diff algorithm.
122             If you've got L installed, that will be used.
123              
124             L - find difference between two YAML documents.
125              
126             L - find difference between two HTML documents.
127             This uses a more sane approach than L.
128              
129             L - find difference between two XML documents.
130              
131             L - find the differences between two Perl hashes.
132              
133             L - find difference between two arbitrary data structures.
134              
135             L - can find difference between two inputs, which can be
136             data structures or file names.
137              
138             =head1 AUTHOR
139              
140             Daisuke Murase
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             1;