VOOZH about

URL: https://www.geeksforgeeks.org/perl/comparing-content-of-files-using-perl/

⇱ Comparing content of files using Perl - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Comparing content of files using Perl

Last Updated : 12 Jul, 2025

In Perl, we can easily compare the content of two files by using the File::Compare module. This module provides a function called compare, which helps in comparing the content of two files specified to it as arguments. If the data present in both the files comes out to be same, the function returns 0 as the output, if the data in the passed files is different, the return value is 1 and if there is any error occurred while accessing the specified/passed files, it returns the value as -1
Syntax:

use File::Compare;
$compare = compare('FILE_NAME_1', 'FILE_NAME_2');


Note:

  • Same Content: Return Value [0]
  • Different Content: Return Value [1]
  • Error in Accessing Files: Return Value [-1]


Example: 
Files Present in the Folder. πŸ‘ Image
  When the content of the Files is same: 

Output:

 πŸ‘ Image
  

When the content of the Files is different: 

Output:

 πŸ‘ Image
  

When the file is not accessible: 

Output:

 πŸ‘ Image

Comment
Article Tags:
Article Tags:

Explore