Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Text::Diff help

by whittick (Acolyte)
on Feb 23, 2012 at 10:27 UTC ( [id://955692]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a bit of script that's supposed to compare two files which are fairly identical, at the moment it's outputting the whole contents of the second file though and I really have no idea why.

use warnings; use strict; use File::Compare; use Text::Diff; use lib "E:\\Program Files (x86)\\Traverse\\lib\\perl"; use lib "C:\\Perl"; my $expect = `expect.expect`; chdir $ARGV[0]; rename "output.new","output.old"; open DIFF, ">alarm.log" or die $!; open OUTPUT, ">output.new" or die $!; print OUTPUT "$expect\n"; if (compare("1.log","2.log") == 0) { print "They're equal\n"; } else { print "They are NOT equal\n"; print DIFF "------------------------------------------------------ +-------------------------------------------------------------\n"; + print DIFF "The script has identified the following changes:\n\n"; + my $diff = diff "output.new", "output.old", { STYLE => "Unified" , + OUTPUT => \*DIFF }; print DIFF "------------------------------------------------------ +-------------------------------------------------------------\n\n"; + close(DIFF); }

If I've missed anything glaringly obvious then please let me know, I can add the files to compare if needed but like I said, they're identical apart from 2 or three lines.

Thanks

Replies are listed 'Best First'.
Re: Text::Diff help
by moritz (Cardinal) on Feb 23, 2012 at 10:39 UTC
    rename "output.new","output.old"; ... diff "output.new", "output.old", ...

    If the rename call succeeded (you should really check that, or use autodie), then output.new doesn't exist anymore. I guess that diff just treats is as empty, which is why it shows you that the whole contents of the second file as output (the diff is really the whole file).

    Also it seems odd to me that once you compare 1.log and 2.log, and then based on the result of that comparison, you report the difference between two different files. But you might have your reasons for that.

      It exists, because a pair of lines later,
      open OUTPUT, ">output.new" or die $!;
      But it is empty.

      Update: Heh, I should have read the whole code :-) The file is even being written to, but as jethro noticed, its handle is not closed, so the "expect" line is probably still in the buffer.

Re: Text::Diff help
by jethro (Monsignor) on Feb 23, 2012 at 10:54 UTC

    If you change the line to

    my $diff = diff "output.new", "output.new", { STYLE => "Unified" , OUT +PUT => \*DIFF };

    what is the output then? If you see the whole file the diff module is implicated. If not, it could mean there are white space differences in your files. Did you check the files with the diff command line tool?

    UPDATE: You never close the file output.new after you write to it. The data is probably still in the buffer waiting to be written to disk when you do the diff (disk operations are buffered so that not every write of a single character rewrites a whole disk block). Only after the script finishes, close is called automatically and the contents written

      Thanks for pointing out the 1.log and 2.log moritz, that was from a previous attempt so cretainly won't help!

      Thanks for all the pointers about closing my filehandle, I'll give that a go!

        working perfectly now - thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://955692]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 08:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found