perl module to compare two files? 
Author Message
 perl module to compare two files?

Hi,

Does any one have a perl module which compares the
two files, like the unix command "cmp" ? or anything
equivalent to that?

Thanks in advance,
-Balaji

--
-------------------------------------------------------

-------------------------------------------------------



Sat, 30 Dec 2000 03:00:00 GMT  
 perl module to compare two files?
Here is a program I wrote & use. Check my web site
        http://www.ozemail.com/~rpsavage/perl
if you really want Getopt::Simple, which processes command line switches.
Otherwise just ignore it.

#!/usr/gnu/bin/perl -w
#
# Name:
#       diff.pl.
#
# Purpose:
#       Vaguely emulate diff, ignoring comments (ie /^\s*#/).
#
# Switches:
#       -help

use integer;
use strict;

use Getopt::Simple qw($switch);

# ----------------------------------------------------------------------

sub init
{
        my($default) =
        {
        'help' =>
                {
                'type'          => '',
                'env'           => '-',
                'default'       => '',
                'order'         => 1,
                },
        'file1' =>
                {
                'type'          => '=s',
                'env',          => '-',
                'default'       => '',
                'order'         => 3,
                },
        'file2' =>
                {
                'type'          => '=s',
                'env',          => '-',
                'default'       => '',
                'order'         => 4,
                },
        };

        my($option) = new Getopt::Simple;

        if (! $option -> getOptions($default, "Usage: diff.pl [options] file1
file2") )
        {
                # Failure.
                exit(-1);
        }

Quote:
}       # End of init.

# ----------------------------------------------------------------------

&init();

$$switch{'file1'} = shift;
$$switch{'file2'} = shift;

open(INX1, $$switch{'file1'}) || die("Can't open($$switch{'file1'}):
\nFailure: $!");
open(INX2, $$switch{'file2'}) || die("Can't open($$switch{'file2'}):
\nFailure: $!");

# Report no more than this many lines.
my($limit) = 20;


$line = 0;

while (! eof(INX1) )
{
        $line++;
        $one = <INX1>;
        chomp($one);

        if (eof(INX2) )
        {

                next;
        }

        $two = <INX2>;
        chomp($two);

        # Ignore differences in comments.
        next if ( ($one =~ /^\s*#/) && ($two =~ /^\s*#/) );

        if ($one ne $two)
        {

        }

Quote:
}

while (! eof(INX2) )
{
        $line++;
        $two = <INX2>;
        chomp($two);


Quote:
}

close(INX1);
close(INX2);

if ($#log >= 0)
{
        print "diff.pl found $#log mismatches. $limit will be printed. \n";

        my($i);

        for ($i = 0; $i < $limit; $i++)
        {
                print "$log[$i]\n";
        }

        # Failure.
        exit(-1);

Quote:
}

# Success.
exit(0);

----------

Quote:

> Newsgroups: comp.lang.perl.modules
> Subject: perl module to compare two files?
> Date: Wednesday, 15 July 1998 4:29

> Hi,

> Does any one have a perl module which compares the
> two files, like the unix command "cmp" ? or anything
> equivalent to that?

> Thanks in advance,
> -Balaji

> --
> -------------------------------------------------------

> -------------------------------------------------------



Sat, 30 Dec 2000 03:00:00 GMT  
 perl module to compare two files?
A quick check at the CPAN search engine would have directed you to
File::Compare, which is included in Perl, at least in 5.004_04.

Quote:

> Does any one have a perl module which compares the
> two files, like the unix command "cmp" ? or anything
> equivalent to that?

--

Internet Development                                http://www.dannet.dk
Dan Net A/S                                       phone: +45 45 82 16 00
Blokken 9 - DK-3460 Birkeroed - Denmark             fax: +45 45 82 16 44


Sun, 31 Dec 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. To compare two files and search for a word in both files

2. compare two files

3. compare two files - help!!

4. Help comparing two files using hashes

5. comparing the time stamp of two files

6. How to compare two files?

7. To compare the content in two files..

8. compare two files

9. File I/O: want to compare two or more lines @ once

10. Compare two Paths in a file impossible

11. Compare Two text files ?!?!?!

12. Comparing two files

 

 
Powered by phpBB® Forum Software