home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book Home Programming PerlSearch this book

32.19. File::Compare

use File::Compare;

printf "fileA and fileB are %s.\n",
    compare("fileA","fileB") ? "different" : "identical";

use File::Compare 'cmp';
sub munge($) {
    my $line = $_[0];
    for ($line) {
        s/^\s+//;   # Trim leading whitespace.
        s/\s+$//;   # Trim trailing whitespace.
    }
    return uc($line);
}

if (not cmp("fileA", "fileB", sub {munge $_[0] eq munge $_[1]} ) {
    print "fileA and fileB are kinda the same.\n";
}
The File::Compare module provides one function, compare, which compares the contents of the two files passed to it. It returns 0 if the files contain the same data, 1 if they contain different data, and -1 if an error was encountered in accessing the named files. If you pass a subroutine reference as the third argument, that function is repeatedly called to determine whether any two lines are equivalent. For compatibility with the cmp(1) program, you may explicitly import the function as cmp. (This does not affect the binary cmp operator.)



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.