File::Temp#
Create temporary files and directories with race-safe naming and automatic cleanup.
File::Temp hands you a fresh, uniquely-named file (or directory)
in one step: the name is picked, the file is created, and the
filehandle is returned together — so no other process can slip in
between naming and opening. On destruction, temporary files and
directories are removed automatically unless you ask to keep them.
Two idioms are supported, and they share the same options:
Object-oriented —
File::Temp->newreturns a blessed filehandle object. The object stringifies to its filename and unlinks the file when it goes out of scope.File::Temp->newdiris the directory counterpart and cleans up its tree on destruction.Functional —
tempfilereturns($fh, $filename), andtempdirreturns a directory path. Both accept the sameDIR,SUFFIX,TEMPLATE,UNLINK,CLEANUP,PERMS, andTMPDIRoptions as the OO forms.
Templates use trailing X characters (at least four) which are
replaced with random name-safe bytes at creation time; a SUFFIX
appended to the template is preserved across the random fill.
Functions#
Temporary files#
tempfile#
Create a temporary file and return an open filehandle paired with its filename.
new#
Construct a File::Temp object wrapping a newly created temporary file.
filename#
Return the on-disk path associated with a File::Temp object.
stringify#
Overload handler for "" — stringify a File::Temp object to its filename.
numify#
Overload handler for 0+ — numify a File::Temp object to its reference address.
tmpnam#
POSIX-style tmpnam — in scalar context a candidate filename in the system temp directory; in list context a created file with its filehandle.
tmpfile#
POSIX-style tmpfile — open an anonymous temporary file and return the filehandle. The file is unlinked immediately.
mkstemp#
Create a file from a template and return a filehandle together with its pathname.
mkstemps#
Create a file from a template with a literal suffix preserved at the end of the name.
Temporary directories#
tempdir#
Create a uniquely named temporary directory and return its path.
newdir#
Construct a File::Temp::Dir object wrapping a newly created temporary directory.
mkdtemp#
Create a directory from a template and return its path.
dir_dirname#
Return the on-disk path for a File::Temp::Dir object.
dir_stringify#
Overload handler for "" — stringify a File::Temp::Dir to its pathname.
dir_numify#
Overload handler for 0+ — numify a File::Temp::Dir object to its refaddr.
What you get back
The integer refaddr of the underlying reference, used for
identity comparison via ==.
Differences from upstream
Fully compatible with upstream File::Temp 0.2312.
See also
STRINGIFY— companion overload for string contexts.
Cleanup control#
unlink_on_destroy#
Toggle or query whether a File::Temp object unlinks its file when destroyed.
destroy#
Destructor for File::Temp objects — removes the underlying file when UNLINK is set and $File::Temp::KEEP_ALL is false.
cleanup#
Remove every temp file and directory currently registered for cleanup, immediately, and return 1.
unlink0#
Remove a temporary file while its filehandle is still open.
unlink1#
Unlink a temporary file. Upstream closes $fh first; here the filehandle is left open and closed by its usual refcount path.
safe_level#
Report the current safety level used by File::Temp checks.
top_system_uid#
Return the highest uid treated as a system account for ancestor ownership checks.
cmpstat#
Check whether a filehandle and a pathname refer to the same file — used to guard against race-based substitution attacks.
dir_unlink_on_destroy#
Toggle or query whether a File::Temp::Dir object removes its directory tree on destruction.
dir_destroy#
Destructor for File::Temp::Dir objects — removes the directory tree when CLEANUP is true, the current pid matches the creator, and $File::Temp::KEEP_ALL is false.
Template handling#
tempnam#
Return a candidate filename under $dir starting with $prefix — does not create the file.
mktemp#
Fill in the trailing Xs of a template and return the resulting filename — does not create the file.