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