Algorithm-Diff-Apply

 view release on metacpan or  search on metacpan

lib/Algorithm/Diff/Apply.pod  view on Meta::CPAN


Which is probably the right thing to do if your array is going to be
printed out one item per line.

=item optimise_remove_duplicates HASH

Conflict optimiser: detects identical diff hunks in the conflict
block, and removes all but one of each duplicated hunk.

=back

=head1 CALLBACK INTERFACES

A lot of the dirty work in C<apply_diffs()> is done by callback
subroutines, and users of C<Algorithm::Diff::Apply> can override its
default behaviour if they wish by passing appropriate options to
C<apply_diffs()> (see above).

=head2 Conflict Optimiser Callbacks

Optimiser callbacks are a way of making conflict cases easier for
humans to deal with. This can be done by combining diff hunks from
different sources that do the same thing, factoring out common
changes, and other devious bits of monkeywork. Doing this can even
factor away a conflict situation entirely.

These callbacks are called by C<apply_diffs()> when it detects a group
of hunks from different diff sequences that can't all be applied at
once without conflicting with each other.

For each block of conflicting diffs, the callback will be called with
every diff in the block in the following format:

    @ret = optimiser_callback (
          "conflict_block" => {
               "name_1" => [$hunk_1_1, $hunk_1_2, ..., $hunk_1_N],
               "name_2" => [$hunk_2_1, $hunk_2_2, ..., $hunk_2_N],
                :
               "name_M" => [$hunk_M_1, $hunk_M_2, ..., $hunk_M_N],
          },
    );

The tag names are whatever you labelled the diff sequences you passed
to C<apply_diffs()>. By definition, a conflict block contains a subset
of at least two separate diffs.

Each of the C<$hunk_X_X> scalars in the arguments above is a hash
reference with the following structure:

    {
        "start"   => N,
        "changes" => [[OP1, DATA1], ..., [OPn, DATAn]],
    }

Where "start" is a line number in the I<target> array, indicating
where this hunk is intended to be applied, and "changes" contains the
changes to apply.

Optimiser callbacks should return a I<permuted copy> of what they were
passed. Empty diffs will be discarded automatically. If only one diff
remains after processing, the conflict will have been optimised away
completely.

=head2 Conflict Resolver Callbacks

Resolver callbacks are invoked when conflicts have been detected, and
the optimisers weren't able to completely factor away the conflict
block.

The job of a resolver callback is to return some kind of resolution of
the conflicting sub-arrays. These subs are called a little like this:

     @ret = resolver_callback (
	   "alt_txts" => {
                "diff1_name" => ['m', 'n', 'o'],
	        "diff3_name" => [],
           }
     );

The C<alt_txts> parameter is a hash ref keyed by (some of the) names
of the diffs being applied in the main C<apply_diffs()> call, whose
values are arrays containing alternative generated subsequences. Each
of these subsequences is the result of applying a set of hunks from
the corresponding diff to a copy of the slice of the source array
where the conflict happened.

A resolver callback should return an array which will be spliced into
the array that C<apply_diffs()> will return.

(Other options are passed to resolver subs too, but these are as yet
undocumented because they're still liable to change)

=head2 Key Generation Callbacks

When C<Algorithm::Diff::Apply> needs to compare two lines from
different hunks for equality during the optimisation phase, it uses
the "eq" operator by default. This is meaningless for many Perl
variables; for example object references stringify to a form that
doesn't capture the internals of the object, e.g.
"SomeClass=HASH(0x813aa8c)".

This also applies to plain ARRAY, HASH, SCALAR references, or other
types of ref. Ideally we'd like the internal state of any such object
pointed at by a scalar variable to be represented in a form that can
be compared against other varibales of the same type.

This is where key generation callbacks come in. A C<key_generator>
subroutine is a hashing routine; it takes as argument a scalar
variable, and returns a string that can be compared against other
strings returned by other calls to the key_generator. The return
values are cached inside a call to C<apply_diffs()> for efficiency.

The key generators used by C<Algorithm::Diff::Apply> have the same
semantics as the ones used by C<Algorithm::Diff>. See
L<Algorithm::Diff/KEY GENERATION FUNCTIONS>.

When you're dealing with objects, a simple wrapper around one of the
object class's methods often does the trick:

	# Book objects representing titles produced by a publishing
	# house are considered unique if their ISBNs are



( run in 0.709 second using v1.01-cache-2.11-cpan-fa01517f264 )