File::Append::TempFile - Perl extension for appending data to files
use File::Append::TempFile;
$f = new File::Append::TempFile(); $f->begin_work('/etc/hosts') or die "Appending: ".$f->err(); $f->add_line("127.0.0.2 localhvost\n"); $f->commit();
$f->begin_work('/etc/hosts') or die "Appending: ".$f->err(); $f->add_line("...\n"); $f->rollback();
The File::Append::TempFile
module provides an OOP interface to appending
data to files using a temporary file, in order to ensure the atomicity of
the updates.
An append session is initiated by invoking the begin_work()
method and
passing it the name of the file. At this point, a temporary file is
created in the same directory as the original file and the original's
contents is copied to the temporary. More data is added to the temporary
file using the add_line()
method. When done appending, the commit()
method will atomically move the temporary file over the original.
If something goes wrong, the rollback()
method will remove the temporary
file without affecting the original in any way.
The File::Append::TempFile
class defines the following methods:
File::Append::TempFile
object. No file processing is
done at this point.
File::Append::TempFile
object.
add_line()
method and then either stored as the
original with the commit()
method, or discarded with the rollback()
method.
commit()
is invoked.
add_line()
.
NOTE: This method uninitializes the File::Append::TempFile
object,
that is, removes any association between it and the original file and
even file name! The next method invoked on this File::Append::TempFile
object should be begin_work()
.
NOTE: This method uninitializes the File::Append::TempFile
object,
that is, removes any association between it and the original file and
even file name! The next method invoked on this File::Append::TempFile
object should be begin_work()
.
There are also several methods used internally by the
File::Append::TempFile
routines:
begin_work()
time. This allows derived classes to modify
the file structure if needed.
The two parameters are the file handles for the original and the temporary file.
Nothing really for the present :)
begin_work()
and commit()
- those changes will be lost!
Peter Pentchev, <roam@ringlet.net>
Copyright (C) 2006 by Peter Pentchev.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.
$Id: TempFile.pm 306 2006-02-13 14:52:08Z roam $