1IO::AtomicFile(3) User Contributed Perl Documentation IO::AtomicFile(3)
2
3
4
6 IO::AtomicFile - write a file which is updated atomically
7
9 use strict;
10 use warnings;
11 use feature 'say';
12 use IO::AtomicFile;
13
14 # Write a temp file, and have it install itself when closed:
15 my $fh = IO::AtomicFile->open("bar.dat", "w");
16 $fh->say("Hello!");
17 $fh->close || die "couldn't install atomic file: $!";
18
19 # Write a temp file, but delete it before it gets installed:
20 my $fh = IO::AtomicFile->open("bar.dat", "w");
21 $fh->say("Hello!");
22 $fh->delete;
23
24 # Write a temp file, but neither install it nor delete it:
25 my $fh = IO::AtomicFile->open("bar.dat", "w");
26 $fh->say("Hello!");
27 $fh->detach;
28
30 This module is intended for people who need to update files reliably in
31 the face of unexpected program termination.
32
33 For example, you generally don't want to be halfway in the middle of
34 writing /etc/passwd and have your program terminate! Even the act of
35 writing a single scalar to a filehandle is not atomic.
36
37 But this module gives you true atomic updates, via "rename". When you
38 open a file /foo/bar.dat via this module, you are actually opening a
39 temporary file /foo/bar.dat..TMP, and writing your output there. The
40 act of closing this file (either explicitly via "close", or implicitly
41 via the destruction of the object) will cause "rename" to be called...
42 therefore, from the point of view of the outside world, the file's
43 contents are updated in a single time quantum.
44
45 To ensure that problems do not go undetected, the "close" method done
46 by the destructor will raise a fatal exception if the "rename" fails.
47 The explicit "close" just returns "undef".
48
49 You can also decide at any point to trash the file you've been
50 building.
51
53 IO::AtomicFile inherits all methods from IO::File and implements the
54 following new ones.
55
56 close
57 $fh->close();
58
59 This method calls its parent "close" in IO::File and then renames its
60 temporary file as the original file name.
61
62 delete
63 $fh->delete();
64
65 This method calls its parent "close" in IO::File and then deletes the
66 temporary file.
67
68 detach
69 $fh->detach();
70
71 This method calls its parent "close" in IO::File. Unlike "delete" in
72 IO::AtomicFile it does not then delete the temporary file.
73
75 Eryq (eryq@zeegee.com). President, ZeeGee Software Inc
76 (http://www.zeegee.com).
77
79 Dianne Skoll (dfs@roaringpenguin.com).
80
82 Copyright (c) 1997 Erik (Eryq) Dorfman, ZeeGee Software, Inc. All
83 rights reserved.
84
85 This program is free software; you can redistribute it and/or modify it
86 under the same terms as Perl itself.
87
88
89
90perl v5.36.0 2023-01-20 IO::AtomicFile(3)