man-pages/man3/IO::AtomicFile.3pm.html
2021-03-31 01:06:50 +01:00

123 lines
3.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of IO::AtomicFile</TITLE>
</HEAD><BODY>
<H1>IO::AtomicFile</H1>
Section: User Contributed Perl Documentation (3pm)<BR>Updated: 2019-02-28<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
IO::AtomicFile - write a file which is updated atomically
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
use IO::AtomicFile;
### Write a temp file, and have it install itself when closed:
my $FH = IO::AtomicFile-&gt;open(&quot;bar.dat&quot;, &quot;w&quot;);
print $FH &quot;Hello!\n&quot;;
$FH-&gt;close || die &quot;couldn't install atomic file: $!&quot;;
### Write a temp file, but delete it before it gets installed:
my $FH = IO::AtomicFile-&gt;open(&quot;bar.dat&quot;, &quot;w&quot;);
print $FH &quot;Hello!\n&quot;;
$FH-&gt;delete;
### Write a temp file, but neither install it nor delete it:
my $FH = IO::AtomicFile-&gt;open(&quot;bar.dat&quot;, &quot;w&quot;);
print $FH &quot;Hello!\n&quot;;
$FH-&gt;detach;
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
This module is intended for people who need to update files
reliably in the face of unexpected program termination.
<P>
For example, you generally don't want to be halfway in the middle of
writing <I>/etc/passwd</I> and have your program terminate! Even
the act of writing a single scalar to a filehandle is <I>not</I> atomic.
<P>
But this module gives you true atomic updates, via <B>rename()</B>.
When you open a file <I>/foo/bar.dat</I> via this module, you are <I>actually</I>
opening a temporary file <I>/foo/bar.dat..TMP</I>, and writing your
output there. The act of closing this file (either explicitly
via <B>close()</B>, or implicitly via the destruction of the object)
will cause <B>rename()</B> to be called... therefore, from the point
of view of the outside world, the file's contents are updated
in a single time quantum.
<P>
To ensure that problems do not go undetected, the ``close'' method
done by the destructor will raise a fatal exception if the <B>rename()</B>
fails. The explicit <B>close()</B> just returns undef.
<P>
You can also decide at any point to trash the file you've been
building.
<A NAME="lbAE">&nbsp;</A>
<H2>AUTHOR</H2>
<A NAME="lbAF">&nbsp;</A>
<H3>Primary Maintainer</H3>
Dianne Skoll (<I><A HREF="mailto:dfs@roaringpenguin.com">dfs@roaringpenguin.com</A></I>).
<A NAME="lbAG">&nbsp;</A>
<H3>Original Author</H3>
Eryq (<I><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></I>).
President, ZeeGee Software Inc (<I><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></I>).
<A NAME="lbAH">&nbsp;</A>
<H2>REVISION</H2>
<TT>$Revision:</TT> 1.2 $
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">AUTHOR</A><DD>
<DL>
<DT id="5"><A HREF="#lbAF">Primary Maintainer</A><DD>
<DT id="6"><A HREF="#lbAG">Original Author</A><DD>
</DL>
<DT id="7"><A HREF="#lbAH">REVISION</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:46 GMT, March 31, 2021
</BODY>
</HTML>