String::Diff(3pm)
π String::Diff π
okay so bestie, welcome to the User Contributed Perl Documentation page for String::Diff(3pm), and let me tell you, this module is a whole vibe. it diffs strings. that's it, that's the girl. let's get into it π
β¨ NAME β¨
String::Diff
π SYNOPSIS π
here's the whole flex, in code, exactly as the module wants you to write it:
use String::Diff;
use String::Diff qw( diff_fully diff diff_merge diff_regexp );# export functions
# simple diff
my($old, $new) = String::Diff::diff('this is Perl', 'this is Ruby');
print "$old\n";# this is [Perl]
print "$new\n";# this is {Ruby}
my $diff = String::Diff::diff('this is Perl', 'this is Ruby');
print "$diff->[0]\n";# this is [Perl]
print "$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>',
);
print "$diff->[0]\n";# this is <del>Perl</del>
print "$diff->[1]\n";# this is <ins>Ruby</ins>
# merged
my $diff = String::Diff::diff_merge('this is Perl', 'this is Ruby');
print "$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>',
);
print "$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');
print "$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] }) {
print "$line->[0]: '$line->[1]'\n";
}
# u: 'this is '
# -: 'Perl'
for my $line (@{ $diff->[1] }) {
print "$line->[0]: '$line->[1]'\n";
}
# u: 'this is '
# +: 'Ruby'
like, it's giving utility. the exports are right there: diff_fully, diff, diff_merge, diff_regexp. π«Ά
πΈ DESCRIPTION πΈ
String::Diff makes the difference of a consecutive string. after a 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, and you can use it for HTML display too. π
after the line is divided, diff is taken when the 'linebreak' option is specified. look:
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. obsessed, honestly:
my $diff = String::Diff::diff('this is Perl', 'this is Ruby',
remove_open => '<del>',
remove_close => '</del>',
append_open => '<ins>',
append_close => '</ins>',
);
and you can escape with a callback set to the diff function and the diff_merge function. that's the glow-up:
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 π (yes, again π)
okay bestie the page literally has two NAME sections, it's that iconic, we're not skipping it:
String::Diff - Simple diff to String
π¦ METHODS π¦
diff_fully
the list that divides diff according to the mark is returned. π
my($old_string, $new_string) = String::Diff::diff_fully('this is Perl', 'this is Ruby');
diff
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 π§
Algorithm::Diff
π 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 π (yep, twice, keep up π )
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.
and that's the whole page, bestie π«Ά this one was generated by perl v5.40.0 on 2025-02-15 β no cap, that's the timestamp, slay πβ¨π
π¦ where this page comes from
- debian trixie: libstring-diff-perl 0.11-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libstring-diff-perl 0.11-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms