1. Perl
  2. Module
  3. Text::Diff

Text::Diff - Check text differences

Use the Text::Diff module to see the diffs in the text. A Perl implementation of the Unix diff program.

# Loading modules and importing functions
use Text::Diff 'diff';

Use the diff function to see the diffs in the text. You can specify a file name or a string as an argument.

# Check file differences
$diff = diff ($filename1, $filename2);
$diff = diff (\$string1, $string2);

This is an example that outputs the difference in text between files.

# Check the difference between files
use Text::Diff 'diff';

my @files = @ARGV;

my $diff = diff ($files[0], $files[1]);

print $diff;

Difference result style

You can optionally specify the style of the diff result. The styles are:

Unified

The Unified style is the following difference display method.

- - A Mon Nov 12 23:49:30 2001
+++ B Mon Nov 12 23:49:30 2001
@@-2,13 +2,13 @@ 2
 3
 Four
-5d
+ 5a
 6
 7
 8
 9
+ 9a
 Ten
 11
-11d
 12
 13

Context

Context is the following difference display method.

*** A Mon Nov 12 23:49:30 2001
- - B Mon Nov 12 23:49:30 2001
***************
*** 2,14 ****
  2
  3
  Four
! 5d
  6
  7
  8
  9
  Ten
  11
- 11d
  12
  13
- - 2,14 - --
  2
  3
  Four
! 5a
  6
  7
  8
  9
+ 9a
  Ten
  11
  12
  13

OldStyle

OldStyle is the following difference display method.

5c5
<5d
- -
> 5a
9a10
> 9a
12d12
<11d

Related Informatrion