BM> Under Unix, -i with no backup unlinks the old file (holding it open) and
BM> then creates the new file in its place, so there's no need for another
BM> filename. Under systems that don't have close-behind semantics (because
BM> they don't have an inode layer), this is impossible: under Win32, you
BM> cannot delete a file while it is open. Therefore, under such systems you
BM> have to provide a backup filename so that both input and output files
BM> get to have a name. (In principle perl could work around this by making
BM> up a temporary filename in the same directory to use instead, but at
BM> least under Win32 it doesn't.)
interesting. i didn't think about that way of doing things.
>> i use the same idea in File::Slurp in write_file with the atomic option
>> on. it writes the whole file to a temp name and when done calls rename
>> (which is atomic) so you always have a legit file around.
BM> This is actually somewhat safer than inplace edit, of course, as the
BM> file is replaced atomically. Even with a backup name, -i does things in
BM> the wrong order: first it renames the old file to the backup, thenit
BM> creates a new file by the original name for the output, so if the
BM> process is interrupted the file is left corrupted. I guess the perl core
BM> doesn't want to get involved with the issue of safely creating a
BM> temporary filename, and just avoids the issue.
well, that is why i call that option atomic. the idea is to always have
a proper file around. i have plans for inplace edit functions in
file::slurp (line by line or whole file) and those will call write_file
with the atomic option. then you can do inplace editing from a program
with just a single sub call (passing it a code ref to do the editing).
BM> I have often thought Unix could do with flink(2) and mkstemp(2)
BM> syscalls, that create a new link to an open fd and create a new file
BM> with no name respectively. It would save a whole lot of mucking about
BM> with temporary names.
open can create temp files now (i think it uses file::temp
underneath). in slurp i take the old file name and append the pid to it
for the temp file. maybe i need to make that part more robust but no one
has mentioned it. one reason i don't use the open temp feature is that
slurp is backwards compatible to 5.005.
uri
--
Uri Guttman ------ (e-mail address removed) --------
http://www.sysarch.com--
----- Perl Architecture, Development, Training, Support, Code Review ------
----------- Search or Offer Perl Jobs -----http://jobs.perl.org ---------
--------- Gourmet Hot Cocoa Mix ----
http://bestfriendscocoa.com---------