Creating Temporary Files

Problem

You need to create a temporary file and have it automatically deleted when your program exits. For instance, you want to write a temporary configuration file to feed a program you launch. In this case, you want to know the temporary file's name to tell the utility program. In other cases, you may just want a temporary file to write to and read from, and don't need to know its filename.

Solution

If you don't need to know the file's name, use the new_tmpfile class method from the IO::File module to get a filehandle opened for reading and writing:

use IO::File; $fh = IO::File->new_tmpfile or die "Unable to make new temporary file: $!";

If you need to know the file's name, use the tmpnam function from the POSIX module to get a filename that you then open yourself:

use IO::File; use POSIX qw(tmpnam); # try new temporary filenames until we get one that didn't already exist do {
 $name = tmpnam()
}
until $fh = IO::File->new($name, O_RDWR|O_CREAT|O_EXCL); # install atexit-style handler so that when we exit or die, # we automatically delete this temporary file END {
 unlink($name) or die "Couldn't unlink $name : $!"
}
# now go on to use the file ...

Discussion

If you only need scratch space, the IO::File module's new_tmpfile class method returns a filehandle connected to a temporary file that's been opened read-write by using the following code:

for (;;) {
 $name = tmpnam(); sysopen(TMP, $tmpnam, O_RDWR | O_CREAT | O_EXCL) && last;
}
unlink $tmpnam;

This file will be automatically deleted when your program finally exits or the file is closed. You cannot find its name to tell another process, because it doesn't have a name. In fact, on systems that support such semantics, the filename was already deleted before the method returned. A child process could inherit the open file descriptor, however.[2]

[2] But you'd better set $^F to at least fileno($fh) before you exec anything.

This shows new_tmpfile in action. We create a temporary file, write to it, rewind, and print what we wrote:

use IO::File; $fh = IO::File->new_tmpfile or die "IO::File->new_tmpfile: $!"; $fh->autoflush(1);
print $fh "$i\n" while $i++ < 10; seek($fh, 0, 0) or die "seek: $!";
print "Tmp file has: ", <$fh>;

The second solution gets a temporary file whose name you can give to another process. We use the POSIX::tmpnam function, open the file ourselves, and delete it when we're done. We don't test whether a file of that name exists before opening it because that would introduce a race condition - someone might create the file between our checking whether it exists and our creating the file.[3] Instead, we wrap tmpnam in a loop to make sure we get a new file and don't accidentally clobber someone else's. Two processes shouldn't get the same filename from new_tmpfile, in theory.

[3] Race conditions are explained in .

See Also

The documentation for the standard IO::File and POSIX modules (also in of Perl Developing); ; your system's tmpnam (3) manpage