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