Algorithm-Diff

 view release on metacpan or  search on metacpan

htmldiff.pl  view on Meta::CPAN

#!/usr/bin/perl -w
# diffs two files and writes an HTML output file.
use strict;
use CGI qw(:standard :html3);
use Algorithm::Diff 'traverse_sequences';
use Text::Tabs;

my ( @a, @b );

# Take care of whitespace.
sub preprocess
{
	my $arrayRef = shift;
	chomp(@$arrayRef);
	@$arrayRef = expand(@$arrayRef);
}

# This will be called with both lines are the same
sub match
{
	my ( $ia, $ib ) = @_;

htmldiff.pl  view on Meta::CPAN

# Read each file into an array.
open FH, $ARGV[0];
@a = <FH>;
close FH;

open FH, $ARGV[1];
@b = <FH>;
close FH;

# Expand whitespace
preprocess( \@a );
preprocess( \@b );

# inline style
my $style = <<EOS;
	PRE {
		margin-left: 24pt; 
		font-size: 12pt;
	    font-family: Courier, monospaced;
		white-space: pre
    }
	PRE.onlyA { color: red }

lib/Algorithm/Diff.pm  view on Meta::CPAN

        {
            &$discardBCallback( $ai, $bi++, @_ ) while $bi < $bLine;
            &$matchCallback( $ai,    $bi++, @_ );
        }
        else
        {
            &$discardACallback( $ai, $bi, @_ );
        }
    }

    # The last entry (if any) processed was a match.
    # $ai and $bi point just past the last matching lines in their sequences.

    while ( $ai <= $lastA or $bi <= $lastB )
    {

        # last A?
        if ( $ai == $lastA + 1 and $bi <= $lastB )
        {
            if ( defined($finishedACallback) )
            {

lib/Algorithm/Diff.pm  view on Meta::CPAN

        @hunk3a = @b[ 0 .. 2-1 ];
    # or
        @hunk3a = @a[ 1 .. 2 ];
        @hunk3a = @b[ 0 .. 1 ];
    # or
        @hunk3a = qw( b c );
        @hunk3a = qw( b c );

Note that this third hunk contains unchanged items as our convention demands.

You can continue this process until you reach the last two indices,
which will always be the number of items in each sequence.  This is
required so that subtracting one from each will give you the indices to
the last items in each sequence.

=head2 C<traverse_sequences>

C<traverse_sequences> used to be the most general facility provided by
this module (the new OO interface is more powerful and much easier to
use).

lib/Algorithm/Diff.pm  view on Meta::CPAN

            CHANGE    => $callback_4,
        }
    );

If no C<CHANGE> callback is specified, C<traverse_balanced>
will map C<CHANGE> events to C<DISCARD_A> and C<DISCARD_B> actions,
therefore resulting in a similar behaviour as C<traverse_sequences>
with different order of events.

C<traverse_balanced> might be a bit slower than C<traverse_sequences>,
noticeable only while processing huge amounts of data.

The C<sdiff> function of this module 
is implemented as call to C<traverse_balanced>.

C<traverse_balanced> does not have a useful return value; you are expected to
plug in the appropriate behavior with the callback functions.

=head1 KEY GENERATION FUNCTIONS

Most of the functions accept an optional extra parameter.  This is a

lib/Algorithm/DiffOld.pm  view on Meta::CPAN

		if ( defined( $bLine ) )	# matched
		{
			&$discardBCallback( $ai, $bi++, @_ ) while $bi < $bLine;
			&$matchCallback( $ai, $bi++, @_ );
		}
		else
		{
			&$discardACallback( $ai, $bi, @_ );
		}
	}
	# the last entry (if any) processed was a match.

	if ( defined( $finishedBCallback ) && $ai <= $lastA )
	{
		&$finishedBCallback( $bi, @_ );
	}
	else
	{
		&$discardACallback( $ai++, $bi, @_ ) while ( $ai <= $lastA );
	}



( run in 0.283 second using v1.01-cache-2.11-cpan-8d75d55dd25 )