NAME
String::Diff
SYNOPSIS
use
String::Diff;
# simple diff
my
(
$old
,
$new
) = String::Diff::diff(
'this is Perl'
,
'this is Ruby'
);
"$old\n"
;
# this is [Perl]
"$new\n"
;
# this is {Ruby}
my
$diff
= String::Diff::diff(
'this is Perl'
,
'this is Ruby'
);
"$diff->[0]\n"
;
# this is [Perl]
"$diff->[1]\n"
;
# this is {Ruby}
my
$diff
= String::Diff::diff(
'this is Perl'
,
'this is Ruby'
,
remove_open
=>
'<del>'
,
remove_close
=>
'</del>'
,
append_open
=>
'<ins>'
,
append_close
=>
'</ins>'
,
);
"$diff->[0]\n"
;
# this is <del>Perl</del>
"$diff->[1]\n"
;
# this is <ins>Ruby</ins>
# merged
my
$diff
= String::Diff::diff_merge(
'this is Perl'
,
'this is Ruby'
);
"$diff\n"
;
# this is [Perl]{Ruby}
my
$diff
= String::Diff::diff_merge(
'this is Perl'
,
'this is Ruby'
,
remove_open
=>
'<del>'
,
remove_close
=>
'</del>'
,
append_open
=>
'<ins>'
,
append_close
=>
'</ins>'
,
);
"$diff\n"
;
# this is <del>Perl</del><ins>Ruby</ins>
# change to default marks
%String::Diff::DEFAULT_MARKS
= (
remove_open
=>
'<del>'
,
remove_close
=>
'</del>'
,
append_open
=>
'<ins>'
,
append_close
=>
'</ins>'
,
separator
=>
'<-OLD|NEW->'
,
# for diff_merge
);
# generated for regexp
my
$diff
= String::Diff::diff_regexp(
'this is Perl'
,
'this is Ruby'
);
"$diff\n"
;
# this\ is\ (?:Perl|Ruby)
# detailed list
my
$diff
= String::Diff::diff_fully(
'this is Perl'
,
'this is Ruby'
);
for
my
$line
(@{
$diff
->[0] }) {
"$line->[0]: '$line->[1]'\n"
;
}
# u: 'this is '
# -: 'Perl'
for
my
$line
(@{
$diff
->[1] }) {
"$line->[0]: '$line->[1]'\n"
;
}
# u: 'this is '
# +: 'Ruby'
DESCRIPTION
String::Diff is the difference of a consecutive string is made. after general diff is done, the difference in the line is searchable.
the mark of the addition and the deletion can be freely changed. the color is colored to the terminal with ANSI, using the HTML display it.
after the line is divided, diff is taken when 'linebreak' option is specified.
my
(
$old_string
,
$new_string
) = String::Diff::diff_fully(
'this is Perl'
,
'this is Ruby'
,
linebreak
=> 1);
my
(
$old_string
,
$new_string
) = String::Diff::diff(
'this is Perl'
,
'this is Ruby'
,
linebreak
=> 1);
my
$string
= String::Diff::diff_merge(
'this is Perl'
,
'this is Ruby'
,
linebreak
=> 1);
my
$string
= String::Diff::diff_regexp(
'this is Perl'
,
'this is Ruby'
,
linebreak
=> 1);
In diff and diff_merge methods the mark of the difference can be changed.
my
$diff
= String::Diff::diff(
'this is Perl'
,
'this is Ruby'
,
remove_open
=>
'<del>'
,
remove_close
=>
'</del>'
,
append_open
=>
'<ins>'
,
append_close
=>
'</ins>'
,
);
You can escape callback set to diff function and diff_merge function.
use
HTML::Entities
my
(
$diff_old
,
$diff_new
) = String::Diff::diff(
'this is <b>Perl</b>'
,
'this is <b><BIG>R</BIG>uby</b>'
,
remove_open
=>
'<del>'
,
remove_close
=>
'</del>'
,
append_open
=>
'<ins>'
,
append_close
=>
'</ins>'
,
escape
=>
sub
{ encode_entities(
$_
[0]) },
});
is(
$diff_old
,
'this is <b><del>Perl</del></b>'
);
is(
$diff_new
,
'this is <b><ins><BIG>R</BIG>uby</ins></b>'
);
NAME
String::Diff - Simple diff to String
METHODS
- diff_fully
-
the list that divides diff according to the mark is returnd.
my
(
$old_string
,
$new_string
) = String::Diff::diff_fully(
'this is Perl'
,
'this is Ruby'
);
- diff
-
abd the mark of the deletion and the addition is
given
to the string.
- diff_merge
-
old and new string is merged
with
diff.
- diff_regexp
-
the regular expression to which old string and new string are matched
with
regexp is returned.
AUTHOR
Kazuhiro Osawa <yappo {@} shibuya {dot} pl>
SEE ALSO
LICENSE
Copyright 2008 (C) Kazuhiro Osawa
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Kazuhiro Osawa <yappo {@} shibuya {dot} pl>
COPYRIGHT AND LICENSE
This software is copyright (c) 2025 by Kazuhiro Osawa <yappo {@} shibuya {dot} pl>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.