#!/usr/bin/perl
# http://perlmonks.org/?node_id=1207184
use strict;
use warnings;
use Algorithm::Diff qw(traverse_sequences);
use Term::ANSIColor;
my $file1 = <<'END';
#!/usr/bin/perl
use Algorithm::Diff qw(traverse_sequences);
use Term::ANSIColor;
use strict;
use warnings;
# line in both files
# line only in file1
my @from = split //, shift // 'this is the left string';
my @to = split //, shift // 'this is the right string';
traverse_sequences( \@from, \@to,
{
MATCH => sub {print $from[shift()]},
DISCARD_A => sub {print color('red'), $from[shift()], color 'reset'}
+,
DISCARD_B => sub {print color('green'), $to[pop()], color 'reset'},
} );
print "\n";
END
my $file2 = <<'END';
#!/usr/bin/perl
use Algorithm::Diff qw(traverse_sequences);
use Term::ANSIColor;
use warnings;
use strict;
# line in both files
my @from = split //, shift // 'this is the source string';
my @to = split //, shift // 'this is the target string';
# line only in file2
traverse_sequences( \@from, \@to,
{
MATCH => sub {print $from[shift()]},
DISCARD_A => sub {print color('darkred'), $from[shift()], color 'res
+et'},
DISCARD_B => sub {print color('cyan'), $to[pop()], color 'reset'},
} );
print "\n";
END
my @from = split //, $file1;
my @to = split //, $file2;
traverse_sequences( \@from, \@to,
{
MATCH => sub {print $from[shift()]},
DISCARD_A => sub {print color('red'), $from[shift()], color 'reset'}
+,
DISCARD_B => sub {print color('green'), $to[pop()], color 'reset'},
} );
print "\n";
Output not shown because it contains ANSI color sequences. Run in a terminal (like linux console or xterm)
that can handle them.
|