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

I'm on a Win32 platform and I am performing an in-place edit of a file. I have to make sure though that the modified date/time on the file doesn't change if no changes are made.

The problem now is that even if no changes are made to the file,the modified time is changing. This wreaks havoc with a source control system we're using so I need to avoid it.

I think the reason this modified time might be changing is becuase an in-place edit as I understand it involves copying the file(not referring to teh backup).

Is there anyway to not change the modified date if no changes are made?

  • Comment on Open file without changing modified date/time

Replies are listed 'Best First'.
Re: Open file without changing modified date/time
by TedYoung (Deacon) on Dec 06, 2004 at 20:49 UTC

    Hi

    Yes, on a win32 system, the inplace edit has to do it on a temp file, than copy the temp file back to the original. The only solution that comes to mind is to do the implace yourself:

    #!perl use strict; use warnings; undef $/; for my $file (@ARGV) { open F, $file or die $!; $_ = <F>; close F; $changed = 0; # put your code here, mark $changed if you made changes # For example: # $changed = s/this/that/g; if ($changed) { open F, ">$file" or die $!; print F; close F; } }
    }

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
      You both have good suggestions but I am going to take up Ted's suggestion. The reason is that I'd rather *really* not change the modified time and change it back if I can avoid it.

      I wanted to make sure there wasn't some easy way I was missing but now it seems there is not.

      Thanks to both of you.

        Why don't you want to change the modified time? It's easily the simplest / easist option.
      Ted,

      I followed your approach but wrote my own code to do it. But just in case someone tries to use your code, I think it has a bug. I think the line $_ = <F>; since you will just read one line.

        You forgot to notice this line in Ted's code:
        $/ = undef;
        Look up $/ in perldoc perlvar -- in case you don't know what "slurp mode" is for file reads, that section of the docs will explain it. It's not a bug.
Re: Open file without changing modified date/time
by dragonchild (Archbishop) on Dec 06, 2004 at 20:47 UTC
    What about saving off the modified date/time then resetting it if you don't make any changes?

    Alternatively, can you determine if you need to make changes before you do your inplace edit?

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Open file without changing modified date/time
by strat (Canon) on Dec 07, 2004 at 07:29 UTC

    if you don't find another way, you can set the date/time with the perl function utime (see perldoc -f utime)

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"