in reply to Re: Pimp My RegEx
in thread Pimp My RegEx

Thanks for sharing your wisdom! I can't believe how quickly you guys came up w/ replies. The PerlMonks community is a great resource!

I wanted to use file slurp mode because so that could MOVE old log file entries from the current log into a seperate monthly log file. (and then tar/gzip 'em)

I thought that by using the slurp mode, I could essentially edit the file in place without having to create a temporary file. The file system these logs live in is often constrained for space.

Thanks again!

Replies are listed 'Best First'.
Re^3: Pimp My RegEx
by eyepopslikeamosquito (Archbishop) on May 31, 2005 at 21:20 UTC

    I thought that by using the slurp mode, I could essentially edit the file in place without having to create a temporary file.
    No, that is unsound. You can corrupt your log files. Consider:
    1. Slurp the log file.
    2. Apply regex to file contents.
    3. Write the log file with new file contents.
    Now, if the write fails due to "disk full" (a distinct possibility in your environment) or because you lose power at that instant, or for any old reason, you have just corrupted your log file. Generally, you should write the modified file to a temporary file and rename the temporary file over the original only after checking that the temporary file was created without error.

Re^3: Pimp My RegEx
by demerphq (Chancellor) on May 31, 2005 at 19:13 UTC

    I could essentially edit the file in place without having to create a temporary file.

    Generally this isn't possible. You can only overwrite the existing parts of the log, not slice them out of the file. The normal way to do what you want is to process your input records as shown here by me and several of the other monks and then process them and output them to the new file, and then delete the old file. You can't really remove from the beginning of a file while something is writing to the end.

    Anyway, glad to help. :-)

    ---
    $world=~s/war/peace/g

      Generally this isn't possible. You can only overwrite the existing parts of the log, not slice them out of the file.

      Alas, it was a worthy effort!