1String::Diff(3) User Contributed Perl Documentation String::Diff(3)
2
3
4
6 String::Diff - Simple diff to String
7
9 use String::Diff;
10 use String::Diff qw( diff_fully diff diff_merge diff_regexp );# export functions
11
12 # simple diff
13 my($old, $new) = String::Diff::diff('this is Perl', 'this is Ruby');
14 print "$old\n";# this is [Perl]
15 print "$new\n";# this is {Ruby}
16
17 my $diff = String::Diff::diff('this is Perl', 'this is Ruby');
18 print "$diff->[0]\n";# this is [Perl]
19 print "$diff->[1]\n";# this is {Ruby}
20
21 my $diff = String::Diff::diff('this is Perl', 'this is Ruby',
22 remove_open => '<del>',
23 remove_close => '</del>',
24 append_open => '<ins>',
25 append_close => '</ins>',
26 );
27 print "$diff->[0]\n";# this is <del>Perl</del>
28 print "$diff->[1]\n";# this is <ins>Ruby</ins>
29
30 # merged
31 my $diff = String::Diff::diff_merge('this is Perl', 'this is Ruby');
32 print "$diff\n";# this is [Perl]{Ruby}
33
34 my $diff = String::Diff::diff_merge('this is Perl', 'this is Ruby',
35 remove_open => '<del>',
36 remove_close => '</del>',
37 append_open => '<ins>',
38 append_close => '</ins>',
39 );
40 print "$diff\n";# this is <del>Perl</del><ins>Ruby</ins>
41
42 # change to default marks
43 %String::Diff::DEFAULT_MARKS = (
44 remove_open => '<del>',
45 remove_close => '</del>',
46 append_open => '<ins>',
47 append_close => '</ins>',
48 separator => '<-OLD|NEW->', # for diff_merge
49 );
50
51 # generated for regexp
52 my $diff = String::Diff::diff_regexp('this is Perl', 'this is Ruby');
53 print "$diff\n";# this\ is\ (?:Perl|Ruby)
54
55 # detailed list
56 my $diff = String::Diff::diff_fully('this is Perl', 'this is Ruby');
57 for my $line (@{ $diff->[0] }) {
58 print "$line->[0]: '$line->[1]'\n";
59 }
60 # u: 'this is '
61 # -: 'Perl'
62
63 for my $line (@{ $diff->[1] }) {
64 print "$line->[0]: '$line->[1]'\n";
65 }
66 # u: 'this is '
67 # +: 'Ruby'
68
70 String::Diff is the difference of a consecutive string is made. after
71 general diff is done, the difference in the line is searchable.
72
73 the mark of the addition and the deletion can be freely changed. the
74 color is colored to the terminal with ANSI, using the HTML display it.
75
76 after the line is divided, diff is taken when 'linebreak' option is
77 specified.
78
79 my($old_string, $new_string) = String::Diff::diff_fully('this is Perl', 'this is Ruby', linebreak => 1);
80 my($old_string, $new_string) = String::Diff::diff('this is Perl', 'this is Ruby', linebreak => 1);
81 my $string = String::Diff::diff_merge('this is Perl', 'this is Ruby', linebreak => 1);
82 my $string = String::Diff::diff_regexp('this is Perl', 'this is Ruby', linebreak => 1);
83
84 In diff and diff_merge methods the mark of the difference can be
85 changed.
86
87 my $diff = String::Diff::diff('this is Perl', 'this is Ruby',
88 remove_open => '<del>',
89 remove_close => '</del>',
90 append_open => '<ins>',
91 append_close => '</ins>',
92 );
93
94 You can escape callback set to diff function and diff_merge function.
95
96 use HTML::Entities
97 my($diff_old, $diff_new) = String::Diff::diff(
98 'this is <b>Perl</b>',
99 'this is <b><BIG>R</BIG>uby</b>',
100 remove_open => '<del>',
101 remove_close => '</del>',
102 append_open => '<ins>',
103 append_close => '</ins>',
104 escape => sub { encode_entities($_[0]) },
105 });
106 is($diff_old, 'this is <b><del>Perl</del></b>');
107 is($diff_new, 'this is <b><ins><BIG>R</BIG>uby</ins></b>');
108
110 diff_fully
111 the list that divides diff according to the mark is returnd.
112
113 my($old_string, $new_string) = String::Diff::diff_fully('this is Perl', 'this is Ruby');
114
115 diff
116 abd the mark of the deletion and the addition is given to the string.
117
118 diff_merge
119 old and new string is merged with diff.
120
121 diff_regexp
122 the regular expression to which old string and new string are matched with regexp is returned.
123
125 Kazuhiro Osawa <yappo {@} shibuya {dot} pl>
126
128 Algorithm::Diff
129
131 Copyright 2008 (C) Kazuhiro Osawa
132
133 This library is free software; you can redistribute it and/or modify it
134 under the same terms as Perl itself.
135
136
137
138perl v5.34.0 2021-07-22 String::Diff(3)