Safe creation of tempfiles


These are safe against accidental conflict; deliberate conflicts may or may not be covered:

use File::Temp qw/ tempfile tempdir /;

($fh, $filename) = tempfile(“foo.XXXXXXXXXX”, DIR => File::Spec->tmpdir());


/bin/mktemp -t foo.XXXXXXXXXX



The risk with /tmp/foo.$$ is the birthday paradox. For 16-bit PIDs, n ~= sqrt(2 * 65536 ln (1 / (1-0.5)) ~= 300, so a few hundred tempfiles lying around means that clashes are more likely to occur than not.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: