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

Monks I beseech thee,

I am doing major edits to sgml files through regex. Basically I am taking the original file as a string, editing the string and then printing the string to a new file. I want the new file to retain the modified and accessed dates of the original file. Here's my attempt so far:

$atime = (stat($found_file))[8]; $mtime = (stat($found_file))[9]; $time = ctime($date_string); $sgml_string = finalProcess($sgml_string); $new_str = substr($found_file, 0, index($found_file,'.new')) . '.new'; open (NEW_STR, ">$new_str"); print NEW_STR $sgml_string; close NEW_STR; unlink($found_file); rename ($new_str, $found_file); utime ($atime, $mtime, $new_str);

Replies are listed 'Best First'.
Re: Apply mtime and atime to new file
by gjb (Vicar) on Dec 08, 2003 at 15:53 UTC

    Don't you want

    utime($atime, $mtime, $found_file);
    rather than operating on $new_str which you've just renamed?

    Hope this helps, -gjb-

Re: Apply mtime and atime to new file
by shenme (Priest) on Dec 08, 2003 at 15:57 UTC
    Is this the code you are actually using?   It looks like the line below won't do what you expect.
    $new_str = substr($found_file, 0, index($found_file,'.new')) . '.new +';
    If the old filename already ends '.new' you will replace '.new' with '.new' to get the old filename again.   If the old filename ends in something else, you will chop off the last character and then add '.new'.   "test.it" will become "test.i.new"

    Otherwise isn't the problem just that you have renamed the file from the name $new_str to the name $found_file, and then try to change the times for the file named $new_str, which isn't there anymore?

      As usual I am humbled by my experience with perl. And by my more experienced colleagues.

      In fact it was as shenme states....I was trying to apply times to a variable that no longer existed.

      Thanks for the quick response and for not being too insulting regarding my ugly code and lack of knowledge.

Re: Apply mtime and atime to new file
by duff (Parson) on Dec 08, 2003 at 15:53 UTC
    I want the new file to retain the modified and accessed dates of the original file.

    Say that sentence out loud to yourself. Say it again and think about what the words mean. Does it sound ridiculous to you too? :-)

    Seriously though, why would you want this bizarre behavior? And what about your code doesn't work like you expect? This line looks suspicioius to me:

    $new_str = substr($found_file, 0, index($found_file,'.new')) . '.new';

    Try it with "foo" for $found_file and you'll see one aspect that looks suspicious. The other is that if $found_file is "foo.new", then $new_str is also "foo.new" If you wanted them to be different then your rename just made the $new_str file go away.

Re: Apply mtime and atime to new file
by Abigail-II (Bishop) on Dec 08, 2003 at 16:20 UTC
    I am doing major edits to sgml files through regex. Basically I am taking the original file as a string, editing the string and then printing the string to a new file. I want the new file to retain the modified and accessed dates of the original file. Here's my attempt so far:
    Yes? Is there a question that I am missing?

    One remark though, your code is doing 5 different system calls (open, close, unlink, rename, utime) of which you are not checking whether they succeed or not. Each of them might fail.

    Abigail

      |Yes? Is there a question that I am missing?

      Yeap, the question was "will this work" or "why doesn't this work"? I am really new (suprise) at Perl, programming, and Perl Monks forums. I'm sorry for asking questions strangely.

      I do mean well.

        Yeap, the question was "will this work" or "why doesn't this work"?
        Given that you have the code, why bother asking "will this work" if you can just run the code and see whether it works?

        As for "why doesn't this work", you get the most out of the question if you specify what happened, and what you expected it to happen. Otherwise, people just have to guess what went wrong, meaning that people who could be answering your question might waste time, or just move on and answer a question of someone who took the trouble in supplying that information.

        Abigail

Re: Apply mtime and atime to new file
by PodMaster (Abbot) on Dec 08, 2003 at 15:53 UTC
    What is ctime (it's not a perl built-in)? You code looks like it should work. You should check what finalProcess returns, and you should also check rename for failure.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.