Elijah has asked for the wisdom of the Perl Monks concerning the following question:

I am having issues with comparing 2 files using File::Compare. My problem is I open the file make no changes and compare the file to itself and it still returns a boolean 1 saying the files are different. Here is the code in question:
sub close { open (TEMP, ">compare"); print TEMP $t->get("1.0", "end"); close(TEMP); print "\$filename equals ",$filename,"\n"; if (compare($filename,"compare") == 0) { print "They are the same\n"; exit; }elsif (compare($filename,"compare") == -1) { $t->insert("end", "There was an error while comparing!\n"); }else{ print "They are different\n"; my $sw = MainWindow->new(-title=>"Content Has Changed"); my $frame = $sw->Frame->pack(-side => 'top', -fill => 'x'); $frame->Label(-text => "The file has changed since last save. Wo +uld you like to save before exiting?")-> pack(-side => 'left', -anchor => 'w'); $frame->Button(-text => "No", -background => 'navy blue', -foreg +round => 'white', -command => sub {exit;})-> pack(-side => 'right'); $frame->Button(-text => "Yes", -background => 'navy blue', -fore +ground => 'white', -command =>\&save_and_exit)-> pack(-side => 'right'); } }
The scalar "$filename" is the file that was opened in it's original form and "compare" is a temp file I create to load the changed text into from the text editor before saving. I then compare the original file to the edited file and if there have been changes I want the program to prompt to save and if there have been no changes I want the program just to save. Now as I said it prompt every time even when I am sure the files are the same. Is there an issue with "$filename" being a full file path and "compare" being a local file in this subdirectory?

Replies are listed 'Best First'.
Re: File comparison problem!
by Roy Johnson (Monsignor) on Dec 01, 2003 at 18:20 UTC
    I notice that you call compare twice, if the first one doesn't register that they're equal. You should assign the result of the compare to a variable and test that.

    When I compare a file to itself, I get the expected result of equality:

    >perl -MFile::Compare -e "print compare('cookies.txt', 'cookies.txt')" 0
    Is $filename possibly still open at the time you compare?

    The PerlMonk tr/// Advocate
      Yeah when I compare a file to itself it returns the proper indication. But what I meant by comparing it to itself is that I make a temp file of the file itself along with any changes and then compare it to the file as it was when opened without any changes. Now if I open the file and then close it right away the 2 files should match exactly seeing how there were no changes made but they do not match.
Re: File comparison problem!
by duff (Parson) on Dec 01, 2003 at 18:24 UTC

    What happens when you run diff or cmp on the two files?

      Ok I have figured out why it is not passing the comparison. For some reason the following code is inputting one new line to many at the end of the file.
      open (TEMP, ">compare") || "Cannot open compare!\n"; print TEMP $t->get("1.0", "end"); close(TEMP);
      Why would this be doing that and how can I fix the issue or at least work around it?
        Either print() is adding an extra newline (because you have the output record separator $\ set to "\n", e.g. if you are using perl's -l switch), or the extra newline is added by $t->get(), in which case you need to tell us more about what $t and get() are.