Safe creation of tempfiles

by

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());

or

/bin/mktemp -t foo.XXXXXXXXXX

or

mkstemp(“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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: