IO::AtomicFile(3)
πΎ IO::AtomicFile β the Perl manual, bestie edition π
okay so this is IO::AtomicFile(3), a User Contributed Perl Documentation page, and it's literally about writing files that update atomically. no cap. let me tell you everything π
πΈ NAME πΈ
IO::AtomicFile β write a file which is updated atomically β¨
like, the whole file. at once. no half-written chaos. periodt.
π¦ SYNOPSIS π¦
use strict;
use warnings;
use feature 'say';
use IO::AtomicFile;
# Write a temp file, and have it install itself when closed:
my $fh = IO::AtomicFile->open("bar.dat", "w");
$fh->say("Hello!");
$fh->close || die "couldn't install atomic file: $!";
# Write a temp file, but delete it before it gets installed:
my $fh = IO::AtomicFile->open("bar.dat", "w");
$fh->say("Hello!");
$fh->delete;
# Write a temp file, but neither install it nor delete it:
my $fh = IO::AtomicFile->open("bar.dat", "w");
$fh->say("Hello!");
$fh->detach;
three whole vibes, three whole outcomes π write-then-install, write-then-nuke, write-then-ghost. pick your main character moment.
π§ DESCRIPTION π§
so this module is for people who need to update files reliably even when their program just... dies unexpectedly π like, mid-run, bye.
for example β you generally do not want to be halfway through writing /etc/passwd and have your program terminate! not the vibe. not the vibe at all. and listen: even writing a single scalar to a filehandle is not atomic. not even one. π
but this module gives you true atomic updates, via "rename". here's the tea π: when you open a file /foo/bar.dat through this module, you're actually opening a temporary file /foo/bar.dat..TMP and writing all your output there. then, the act of closing this file β either explicitly via "close", or implicitly via the destruction of the object β will cause "rename" to be called...
so from the outside world's point of view? the file's contents are updated in a single time quantum β¨ iconic. literally a glow-up with zero in-between frames.
to make sure problems don't go undetected, the "close" method that the destructor runs will raise a fatal exception if the "rename" fails π₯. the explicit "close" just returns "undef". different energy, on purpose.
you can also decide at any point to trash the file you've been building π your build, your rules.
π METHODS π
IO::AtomicFile inherits all methods from IO::File and implements the following new ones. yes, all of them. nepotism but make it inheritance π«Ά
π close
$fh->close();
this one calls its parent "close" in IO::File and then renames its temporary file as the original file name. that's the whole slay. commit the temp file to the real filename π
π delete
$fh->delete();
this one calls its parent "close" in IO::File and then deletes the temporary file. so long, temp file, you were fun while you lasted π
π detach
$fh->detach();
this one calls its parent "close" in IO::File. and β unlike "delete" in IO::AtomicFile β it does not then delete the temporary file. it just... leaves it there. ghosting the temp file, honestly π§
β AUTHOR β
Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com). founder energy fr fr π
π CONTRIBUTORS π
Dianne Skoll (dfs@roaringpenguin.com). we stan a contributor π«Ά
πΈ COPYRIGHT & LICENSE πΈ
Copyright (c) 1997 Erik (Eryq) Dorfman, ZeeGee Software, Inc. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. yes bestie, same terms as Perl itself, that's the license, go read it and thrive π
ngl, if you're about to write something important β /etc/passwd-level important β please, please use the atomic path. your future self will thank you ππ«Ά
β¨ cute little sign-off β¨ β this page came from perl v5.42.0, dated 2020-06-04, and yes, it's IO::AtomicFile(3) in the User Contributed Perl Documentation section. that's a whole vibe check passed π
π
π¦ where this page comes from
- opensuse-leap 16.0: perl-IO-stringy 2.113-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms