in reply to Re: Copying a file to a temporary file
in thread Copying a file to a temporary file
My idea was therefore to use the backup file as the temporary file, where a backup filename is given, or else use File::Temp to create one for me. I then copy to the temporary file, read from it and write back over the original, and then leave File::Temp to clean up the temporary file if one was created. (If a specified backup file was used instead, then it gets left afterwards, of course.)
I could start by renaming the original to the backup/temporary name instead, as you suggest, but where do I get the temporary name from? File::Temp returns an open filehandle - no good for renaming my original file to, hence I was looking to copy to it instead.
Actually, having read Re-runnably editing a file in place, I'm now thinking something along those lines would be better:
I could get a temporary filehandle, read from the original file, process the data and write to the temporary filehandle. Then I'd want to rename the temporary file to the original filename, but I don't know the temporary filename unless I ignore File::Temp's advice and pick up both the handle and the name. Maybe that's safe enough since I wouldn't be doing anything with the temporary filename except renaming it (and I therefore wouldn't want File::Temp to try to delete the temporary file either). (I'd have to create the backup file separately, rather than using it as the temporary file, in this scheme, of course.)
- Steve
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Copying a file to a temporary file
by BrowserUk (Patriarch) on Jun 16, 2004 at 13:05 UTC | |
by shay (Beadle) on Jun 16, 2004 at 16:01 UTC | |
by BrowserUk (Patriarch) on Jun 16, 2004 at 17:53 UTC | |
by shay (Beadle) on Jun 17, 2004 at 07:43 UTC | |
by BrowserUk (Patriarch) on Jun 17, 2004 at 11:35 UTC |