String::Diff(3pm)

πŸ“¦
version 0.11 Β· 5 versions
, in debian trixie, ubuntu resolute

πŸ’– 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    => '&lt;-OLD|NEW-&gt;', # 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 &lt;b&gt;<del>Perl</del>&lt;/b&gt;');
is($diff_new, 'this is &lt;b&gt;<ins>&lt;BIG&gt;R&lt;/BIG&gt;uby</ins>&lt;/b&gt;');

πŸ‘‘ 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>


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