Sync(3pm)

πŸ’– NAME πŸ’–

File::Sync β€” bestie, this is your all-access pass to the Perl side of fsync() and sync() ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

use File::Sync qw(fsync sync);
sync();
fsync(\*FILEHANDLE) or die "fsync: $!";
# and if fdatasync() is available on your system:
fdatasync($fh) or die "fdatasync: $!";

use File::Sync qw(fsync);
use FileHandle;
$fh = new FileHandle("> /tmp/foo")
    or die "new FileHandle: $!";
...
$fh->fsync() or die "fsync: $!";

like. look at her. she's serving closed code blocks AND correct error handling 😌 periodt.


🌸 DESCRIPTION 🌸

okay so. fsync() takes a Perl file handle as its only argument, grabs its fileno(), and hands that straight to the C function fsync(). Returns undef if it flops, or true if it slays. πŸ’…

fdatasync() has the exact same return value energy, but instead of calling fsync() it calls C fdatasync() β€” which syncs only the data in the file, not the metadata. data only bestie, metadata can wait 😌

fsync_fd() is the lil internal helper fsync() uses behind the scenes; it takes a file descriptor as its only argument.

sync() is literally identical to the C function sync(). no notes.

kay now here's the tea about exports: this module does not export any methods by default, but fsync() is made available as a method of the FileHandle class. and note carefully πŸ‘€ β€” as of 0.11, we no longer clobber anything in IO::Handle. so if you had calls to IO::Handle::fsync(), you can just swap them for IO::Handle::sync():

https://rt.cpan.org/Public/Bug/Display.html?id=50418

not me dropping an RT link, the lore is real πŸ™ˆ


πŸŽ€ NOTES πŸŽ€

  • doing fsync() while the stdio buffers aren't flushed (with $| or the autoflush method) is probably pointless. like girly, flush first πŸ’€
  • calling sync() too often on a multi-user system is slightly antisocial. so true. be mindful of your roomies 🫢
fr fr, those two are the whole vibe check on this page. don't be the person hammering sync() on a shared box, it's giving main character energy in the worst way 😭

πŸ‘‘ AUTHOR πŸ‘‘

Carey Evans <c.evans@clear.net.nz> πŸ’œ


🌈 SEE ALSO 🌈

perl(1), fsync(2), sync(2), perlvar(1)


and that's the whole page bestie!! documented under perl v5.42.3, dated 2026-08-19, living in the User Contributed Perl Documentation section as Sync(3pm) ⭐ go forth and flush your buffers before you fsync, ily πŸ§šπŸ’–

πŸ“¦ where this page comes from