http://www.perlmonks.org?node_id=410391


in reply to Re: existing file is modified or not
in thread existing file is modified or not

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.