use File::Temp qw(tempfile tempdir); $dir = tempdir(CLEANUP => 1); ($fh, $filename) = tempfile(DIR => $dir); ($fh, $filename) = tempfile($template, DIR => $dir); ($fh, $filename) = tempfile($template, SUFFIX => ".data"); $fh = tempfile(); use File::Temp ':mktemp'; ($fh, $filename) = mkstemp("tmpfileXXXXX"); ($fh, $filename) = mkstemps("tmpfileXXXXXX", $suffix); $tmpdir = mkdtemp($template); $unopened_file = mktemp($template);
New to version 5.6.1 of Perl, the File::Temp
module provides convenient functions for creating and opening
temporary files securely. It's better to use this module than to try
to pick a temporary file on your own. Otherwise, you'll just fall into
all the same traps as everyone else before you. This module guards you
against various subtle race conditions, as well as the dangers of
using directories that others can write to; see Chapter 23. The
tempfile
function returns both a filehandle and
filename. It's safest to use the filehandle that's already open and
ignore the filename entirely (except perhaps for error messages). Once
the file is closed, it is automatically deleted. For compatibility
with the C library, the :mktemp
import tag provides
access to functions with names familiar to C programmers, but please
remember that filenames are always less secure than
filehandles.