File Coverage

blib/lib/Algorithm/Diff/Callback.pm
Criterion Covered Total %
statement 53 53 100.0
branch 19 28 67.8
condition 4 4 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 86 95 90.5


line stmt bran cond sub pod time code
1             package Algorithm::Diff::Callback;
2             {
3             $Algorithm::Diff::Callback::VERSION = '0.100';
4             }
5             # ABSTRACT: Use callbacks on computed differences
6              
7 3     3   70496 use strict;
  3         8  
  3         110  
8 3     3   17 use warnings;
  3         6  
  3         81  
9              
10 3     3   14 use Carp;
  3         5  
  3         283  
11 3     3   2448 use parent 'Exporter';
  3         960  
  3         16  
12 3     3   2612 use List::MoreUtils 'uniq';
  3         3785  
  3         299  
13 3     3   2936 use Algorithm::Diff 'diff';
  3         17262  
  3         1674  
14              
15             our @EXPORT_OK = qw(diff_hashes diff_arrays);
16              
17             sub diff_hashes {
18 4     4 1 1071 my ( $old, $new, %cbs ) = @_;
19              
20 4 50       15 ref $old eq 'HASH' or croak 'Arg 1 must be hashref';
21 4 50       11 ref $new eq 'HASH' or croak 'Arg 2 must be hashref';
22              
23 4         5 my @changed = ();
24 4         6 foreach my $key ( keys %{$new} ) {
  4         12  
25 12 100       27 if ( ! exists $old->{$key} ) {
26 5 50       19 exists $cbs{'added'}
27             and $cbs{'added'}->( $key, $new->{$key} );
28             } else {
29 7         10 push @changed, $key;
30             }
31             }
32              
33 4         1283 foreach my $key ( keys %{$old} ) {
  4         9  
34 8 100       935 if ( ! exists $new->{$key} ) {
35 1 50       6 exists $cbs{'deleted'}
36             and $cbs{'deleted'}->( $key, $old->{$key} );
37             }
38             }
39              
40 4         12 foreach my $key (@changed) {
41 7   100     1338 my $before = $old->{$key} || '';
42 7   100     34 my $after = $new->{$key} || '';
43              
44 7 100       19 if ( $before ne $after ) {
45 1 50       9 exists $cbs{'changed'}
46             and $cbs{'changed'}->( $key, $before, $after );
47             }
48             }
49             }
50              
51             sub diff_arrays {
52 5     5 1 1721 my ( $old, $new, %cbs ) = @_;
53              
54 5 50       19 ref $old eq 'ARRAY' or croak 'Arg 1 must be arrayref';
55 5 50       15 ref $new eq 'ARRAY' or croak 'Arg 2 must be arrayref';
56              
57             # normalize arrays
58 5         9 my @old = uniq sort @{$old};
  5         65  
59 5         15 my @new = uniq sort @{$new};
  5         32  
60              
61 5         30 my @diffs = diff( \@old, \@new );
62              
63 5         555 foreach my $diff (@diffs) {
64 4         1002 foreach my $changeset ( @{$diff} ) {
  4         8  
65 8         16 my ( $change, undef, $value ) = @{$changeset};
  8         15  
66              
67 8 100       22 if ( $change eq '+' ) {
    100          
68 6 50       21 exists $cbs{'added'} and $cbs{'added'}->($value);
69             } elsif ( $change eq '-' ) {
70 1 50       10 exists $cbs{'deleted'} and $cbs{'deleted'}->($value);
71             } else {
72 1         249 croak "Can't recognize change in changeset: '$change'";
73             }
74             }
75             }
76             }
77              
78             1;
79              
80             __END__