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

Hi,
I need to generate a file and before doing so,I have to check the whether the file exists and if so ,is it modified or not .I know in the recent past there have been lot of questions on this but my question is different. I am checking in a different way. i,e I am pushing the contents of the existing file "file1" into any array "@seq1" and the contents of the file to be generated is stored in an array "@seq2". I am using Perl module Algorithm::diff to find the difference of 2 arrays. I want to know is this the right way to find whether the existing file is modified or not ?
my $flag = 1; if ( -e $file1 ) { print "Script '$file1' already exists\n"; open(FILE1,"$file1"); my @seq1 = <FILE1>; close FILE1; # @seq2 contains the contents of the file to generated. my $diff = Algorithm::Diff->new( \@seq1, \@seq2 ); my $sep; my $flag = 1; $diff->Base( 1 ); # Return line numbers, not indices while( $diff->Next() ) { next if $diff->Same(); if( ! $diff->Items(2) || ! $diff->Items(1) ) { $flag = 0; print $diff->Get(qw( Min1 Max1 Max2 )),"\n"; } else { $flag = 0; $sep = "---\n"; print $diff->Get(qw( Min1 Max1 Min2 Max2 )),"\n"; } print "< $_" for $diff->Items(1); print $sep; print "> $_" for $diff->Items(2); } if($flag == 0) { print "User edits,do u want to overwrite the exisiting $file1?"; print "[INFO] Continue ? [no]"; my $answer = <STDIN> ; chomp $answer; if ($answer =~ /^n/i) { print"Not Generated the script '$file1'"; } ....

Replies are listed 'Best First'.
Re: existing file is modified or not
by tachyon (Chancellor) on Nov 25, 2004 at 08:37 UTC

    I want to know is this the right way to find whether the existing file is modified or not ?

    Only if the exact difference is of any use. Otherwise simply storing a 128 bit MD5 hash would be the typical approach as it is very economical on space.

    There is lots of software that monitor change in directories. Tripwire springs to mind but there are many others. However it appears to me that you might do well to look at CVS as this does all sorts of useful stuff like versioning, *diffs*, rollback, detecting and merging changes where possible, generating conflict files otherwise...... rsync might also be an option.

    It really depends on the problem you are trying to solve. But when it comes to syncronising directories of files there has been a *lot* of good work done that you may find very useful.

    cheers

    tachyon

Re: existing file is modified or not
by rupesh (Hermit) on Nov 25, 2004 at 09:44 UTC

    If you're on Windows, then you can also have a look at one of Microsoft Resource Kit's tools, robocopy.
    It has many features that come in handy, like MIRroring 2 directories, reporting changes etc.
    It can also synchronize the permissions between 2 directory structures.

    Rupesh.
Re: existing file is modified or not
by rev_1318 (Chaplain) on Nov 25, 2004 at 10:42 UTC
    I want to know is this the right way to find whether the existing file is modified or not ?

    IMHO there is no 'right way' or 'wrong way'. It all depends on the goal you wish to achive. If this works for you, go ahead.

    One thing you should be aware of, is the race condition you are introducing: what is someone edits the file after you read it, and before you stumble over it? You should introduce some kind of locking mechanism to be sure you get it right!

    Cheers,
    Paul

      Another approach would be to create your new file as a unique temporary file, then use something like Text::Diff to diff the two files. If they differ, you remove the old one and rename the new (temporary) one to the name of the old removed file. If they are the same, you just remove the temporary file.

      I agree about the locking: to do this without race conditions, you should consider locking the target file. You may also be able to use file modification times to see if the file has changed since you started looking at it.