File: compare.pl

package info (click to toggle)
libhtml-gentoc-perl 3.20-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436 kB
  • ctags: 60
  • sloc: perl: 1,173; php: 51; makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,176 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# compare two files
sub compare {
    my $file1 = shift;
    my $file2 = shift;

    return 0 unless (-f $file1);
    return 0 unless (-f $file2);

    my $fh1 = undef;
    my $fh2 = undef;
    open($fh1, $file1) || return 0;
    open($fh2, $file2) || return 0;

    my $res = 1;
    my $count = 0;
    while (<$fh1>)
    {
	$count++;
	my $comp1 = $_;
	# remove newline/carriage return (in case these aren't both Unix)
	$comp1 =~ s/\n//;
	$comp1 =~ s/\r//;

	my $comp2 = <$fh2>;

	# check if $fh2 has less lines than $fh1
	if (!defined $comp2)
	{
	    print "error - line $count does not exist in $file2\n  $file1 : $comp1\n";
	    close($fh1);
	    close($fh2);
	    return 0;
	}

	# remove newline/carriage return
	$comp2 =~ s/\n//;
	$comp2 =~ s/\r//;
	if ($comp1 ne $comp2)
	{
	    print "error - line $count not equal\n  $file1 : $comp1\n  $file2 : $comp2\n";
	    close($fh1);
	    close($fh2);
	    return 0;
	}
    }
    close($fh1);

    # check if $fh2 has more lines than $fh1
    if (defined($comp2 = <$fh2>))
    {
	$comp2 =~ s/\n//;
	$comp2 =~ s/\r//;
	print "error - extra line in $file2 : '$comp2'\n";
	$res = 0;
    }

    close($fh2);

    return $res;
}

1;