Skip to content

Commit

Permalink
update and revamp documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
xsawyerx committed Dec 28, 2012
1 parent 364bcea commit 049ee80
Showing 1 changed file with 66 additions and 34 deletions.
100 changes: 66 additions & 34 deletions lib/Algorithm/Diff/Callback.pm
Expand Up @@ -85,8 +85,8 @@ Use callbacks in your diff process to get better control over what will happen.
diff_arrays( diff_arrays(
\@old_family_members, \@old_family_members,
\@new_family_members, \@new_family_members,
sub { print 'Happy to hear about ', shift }, added => sub { say 'Happy to hear about ', shift },
sub { print 'Sorry to hear about ', shift }, deleted => sub { say 'Sorry to hear about ', shift },
); );
Or using hashes: Or using hashes:
Expand All @@ -96,77 +96,109 @@ Or using hashes:
diff_hashes( diff_hashes(
\%old_details, \%old_details,
\%new_details, \%new_details,
sub { print 'Lost ', shift }, added => sub { say 'Gained ', shift },
sub { print 'Gained ', shift }, deleted => sub { say 'Lost ', shift },
sub { changed => sub {
my ( $key, $before, $after ) = @_; my ( $key, $before, $after ) = @_;
print "$key changed from $before to $after\n"; say "$key changed from $before to $after";
}, },
); );
=head1 DESCRIPTION
One of the difficulties when using diff modules is that they assume they know 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 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 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 (such as L<Algorithm::Diff>) give you way too much information that you now have
to skim over and write long complex loops for. to skim over and write long complex loops for.
L<Algorithm::Diff::Callback> let's you pick what you're going to diff (Arrays, L<Algorithm::Diff::Callback> let's you pick what you're going to diff (Arrays or
Hashes) and set callbacks for the diff process. Hashes) and set callbacks for the diff process.
=head1 EXPORT =head1 EXPORT
You'll need to declare to explicitly export these functions.
=head2 diff_arrays =head2 diff_arrays
=head2 diff_hashes =head2 diff_hashes
use Algorithm::Diff::Callback qw<diff_arrays diff_hashes>;
=head1 SUBROUTINES/METHODS =head1 SUBROUTINES/METHODS
=head2 diff_arrays(\@old, \@new, \&removed, \&added) =head2 diff_arrays(\@old, \@new, %callbacks)
The first two parameters are array references to compare. The first two parameters are array references to compare.
The second two parameters are subroutine references which will be called and The rest of the parameters are keys for the type of callback you want and the
given the value that was either removed or added during the diff process. corresponding callback. You can provide multiple callbacks. Supported keys are:
The comparison is explicitly the second one B<against> the first one. =over 4
That means that if you give a I<removed> subroutine, it really means that a =item * added
value that existed in the first arrayref does not exist in the second arrayref.
diff_arrays(
\@old, \@new,
added => sub {
my $value = shift;
say "$value was added to the array";
}
);
If you gave a I<added> subroutine, it really means that a value that did B<not> =item * deleted
exist in the first arrayref now exists in the second one.
B<Note:> if you do not wish to give a certain subroutine, you can simply provide diff_arrays(
undef: \@old, \@new,
deleted => sub {
my $value = shift;
say "$value was deleted from the array";
}
);
diff_arrays( \@old, \@new, undef, sub { 'added: ', $_[0], "\n" } ); =back
=head2 diff_hashes(\%old, \%new, \&removed, \&added, \&change) =head2 diff_hashes(\%old, \%new, %callbacks)
The first two parameters are hash references to compare. The first two parameters are hash references to compare.
The second two paramters are the subroutine references which will be called and The rest of the parameters are keys for the type of callback you want and the
given the key and value that was either removed or added during the diff corresponding callback. You can provide multiple callbacks. Supported keys are:
process.
The third parameter is a subroutine reference of information that changed =over 4
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.
B<Note:> if you do not wish to give a certain subroutine, you can simply provide =item * added
undef:
diff_hashes( diff_hashes(
\%old, \%old, \%new,
\%new, added => sub {
undef, my ( $key, $value ) = @_;
undef, say "$key ($value) was added to the hash";
sub { }
);
=item * deleted
diff_hashes(
\%old, \%new,
deleted => sub {
my ( $key, $value ) = @_;
say "$key ($value) was deleted from the hash";
}
);
=item * changed
diff_hashes(
\%old, \%new,
changed => sub {
my ( $key, $before, $after ) = @_; my ( $key, $before, $after ) = @_;
print "$key changed from $before to $after\n"; say "$key in the hash was changed from $before to $after";
}, }
); );
=back
=head1 BUGS =head1 BUGS
Please report bugs on the Github issues page at Please report bugs on the Github issues page at
Expand Down

0 comments on commit 049ee80

Please sign in to comment.