/
Callback.pm
212 lines (139 loc) · 5.21 KB
/
Callback.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package Algorithm::Diff::Callback;
use strict;
use warnings;
use Carp;
use Exporter 'import';
use List::MoreUtils 'uniq';
use Algorithm::Diff 'diff';
our $VERSION = '0.01';
our @EXPORT_OK = qw(diff_hashes diff_arrays);
sub diff_hashes {
my ( $old, $new, $del_cb, $add_cb, $changed_cb ) = @_;
my @changed = ();
foreach my $cell ( keys %{$new} ) {
if ( ! exists $old->{$cell} ) {
$add_cb->( $cell, $new->{$cell} );
} else {
push @changed, $cell;
}
}
foreach my $cell ( keys %{$old} ) {
if ( ! exists $new->{$cell} ) {
$del_cb->( $cell, $old->{$cell} );
}
}
foreach my $changed (@changed) {
my $before = $old->{$changed} || '';
my $after = $new->{$changed} || '';
if ( $before ne $after ) {
$changed_cb->( $changed, $before, $after );
}
}
}
sub diff_arrays {
my ( $old, $new, $del_cb, $add_cb ) = @_;
# normalize arrays
my @old = uniq sort @{$old};
my @new = uniq sort @{$new};
my @diffs = diff( \@old, \@new );
foreach my $diff (@diffs) {
foreach my $changeset ( @{$diff} ) {
my ( $change, undef, $value ) = @{$changeset};
if ( $change eq '+' ) {
$add_cb->($value);
} elsif ( $change eq '-' ) {
$del_cb->($value);
} else {
croak "Can't recognize change in changeset: '$change'";
}
}
}
}
1;
__END__
=head1 NAME
Algorithm::Diff::Callback - Use callbacks on computed differences
=head1 VERSION
Version 0.01
=head1 SYNOPSIS
Use callbacks in your diff process to get better control over what will happen.
use Algorithm::Diff::Callback 'diff_arrays';
diff_arrays(
\@old_family_members,
\@new_family_members,
sub { print 'Happy to hear about ', shift },
sub { print 'Sorry to hear about ', shift },
);
Or using hashes:
use Algorithm::Diff::Callback 'diff_hashes';
diff_hashes(
\%old_details,
\%new_details,
sub { print 'Lost ', shift },
sub { print 'Gained ', shift },
sub {
my ( $key, $before, $after ) = @_;
print "$key changed from $before to $after\n";
},
);
One of the difficulties when using diff modules is that they assume they know
what you want the information for. Some give you formatted output, some give you
just the values that changes (but neglect to mention how each changed) and some
(such as L<Algorithm::Diff>) give you way too much information that you now have
to skim over and write long complex loops for.
L<Algorithm::Diff::Callback> let's you pick what you're going to diff (Array,
Hashes) and set callbacks for the diff process.
=head1 EXPORT
=head2 diff_arrays
=head2 diff_hashes
=head1 SUBROUTINES/METHODS
=head2 diff_arrays(\@old, \@new, \&removed, \&added)
The first two parameters are array references to compare.
The second two parameters are subroutine references which will be called and
given the value that was either removed or added during the diff process.
The comparison is explicitly the second one B<against> the first one.
That means that if you give a I<removed> subroutine, it really means that a
value that existed in the first arrayref does not exist in the second arrayref.
If you gave a I<added> subroutine, it really means that a value that did B<not>
exist in the first arrayref now exists in the second one.
=head2 diff_hashes(\%old, \%new, \&removed, \&added, \&change)
The first two parameters are hash references to compare.
The second two paramters are the subroutine references which will be called and
given the key and value that was either removed or added during the diff
process.
The third parameter is a subroutine reference of information that changed
between the first and second hashes. It will be given the key that was changed,
the value it had before and the value it now has in the new reference.
=head1 AUTHOR
Sawyer X, C<< <xsawyerx at cpan.org> >>
=head1 BUGS
Please report bugs on the Github issues page at
L<http://github.com/xsawyerx/algorithm-diff-callback/issues>.
=head1 SUPPORT
This module sports 100% test coverage, but in case you have more issues...
You can find documentation for this module with the perldoc command.
perldoc Algorithm::Diff::Callback
You can also look for information at:
=over 4
=item * Github issues tracker
L<http://github.com/xsawyerx/algorithm-diff-callback/issues>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Algorithm-Diff-Callback>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Algorithm-Diff-Callback>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Algorithm-Diff-Callback>
=item * Search CPAN
L<http://search.cpan.org/dist/Algorithm-Diff-Callback/>
=back
=head1 DEPENDENCIES
L<Algorithm::Diff>
L<List::MoreUtils>
L<Carp>
L<Exporter>
=head1 LICENSE AND COPYRIGHT
Copyright 2010 Sawyer X.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.