Skip to content

Instantly share code, notes, and snippets.

@kazuho
Created April 22, 2014 07:32
Show Gist options
  • Select an option

  • Save kazuho/11168660 to your computer and use it in GitHub Desktop.

Select an option

Save kazuho/11168660 to your computer and use it in GitHub Desktop.
tempfile, UNLINK, and flock
my $fn = do {
# some BSD variants (notably OS X) flocks the file handle when calling tempfile()
my ($fh, $fn) = tempfile(UNLINK => 1);
$fn
# setting UNLINK => 1 keeps a hidden reference to the file handle, and thus the file
# is not automatically closed when exitting this block
};
open my $fh, "<", $fn
or die $!;
# flock blocks forever since the file is already locked
flock($fh, LOCK_EX)
or die $!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment