String::ShowHTMLDiff - Perl extension to help visualize (in a browser) differences between strings.
use String::ShowDiff qw/html_colored_diff/; print html_colored_diff("abcehjlmnp", "bcdefjklmrst"); # or a bit more detailed: my %options = ('u' => 'reset', '+' => 'on_green', '-' => 'on_red'); print html_colored_diff($oldstring, $newstring, \%options); # or let's see only the changed words print html_colored_diff($old, $new, {context => qr/\w*/, gap => ' '});
This module is a slight spin on the String::ShowASCIIDiff module. It marks up a diff between two strings using HTML. YOU supply the style sheet and make it look cool. A sample style sheet is included. Basically, you just have to define the style for the <SPAN> tags that are in the output. The classes are
unchanged diff_minus diff_plus
See a CSS tutorial if you still don't know what's going on.
This module is a wrapper around the diff algorithm from the module Algorithm::Diff.
Algorithm::Diff
Compared to the many other Diff modules, the output is neither in diff-style nor are the recognised differences on line or word boundaries, they are at character level.
diff
This method compares $string with $changed_string and returns an HTML encoded string.
$string
$changed_string
print html_colored_diff($s1, $s2, {context => qr/.*/, gap => ''}); # default # will print the complete combined string with the marked removings and # additions print html_colored_diff($s1, $s2, {context => qr/.{0,3}/, gap => ' ... '}); # will print all changings with a context of the left and right 3 chars # and will join each of them with a space, 3 dots and a space # Note that it is important to use qr/.{0,3}/ instead of qr/.../ to also # show only a context of 0,1 or 2 chars at the beginning or end of the # strings print html_colored_diff($s1, $s2, {context => qr/\w*/, gap => ' '}) # will print all changed words and seperates them with a blank
None by default.
Algorithm::Diff, String::ShowASCIIDiff, Text::Diff, Text::ParagraphDiff, Test::Differences
Janek Schleicher, <bigj@kamelfreund.de>
Jim Garvin <jg.perl@thegarvin.com>
Copyright 2003 by Jim Garvin
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install String::ShowHTMLDiff, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::ShowHTMLDiff
CPAN shell
perl -MCPAN -e shell install String::ShowHTMLDiff
For more information on module installation, please visit the detailed CPAN module installation guide.