```{index} single: File::Temp; Perl module ``` # File::Temp ```{pperl-module-badges} 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->new` returns a blessed filehandle object. The object stringifies to its filename and unlinks the file when it goes out of scope. `File::Temp->newdir` is the directory counterpart and cleans up its tree on destruction. - **Functional** — `tempfile` returns `($fh, $filename)`, and `tempdir` returns a directory path. Both accept the same `DIR`, `SUFFIX`, `TEMPLATE`, `UNLINK`, `CLEANUP`, `PERMS`, and `TMPDIR` options 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`](Temp/tempfile) Create a temporary file and return an open filehandle paired with its filename. #### [`new`](Temp/new) Construct a `File::Temp` object wrapping a newly created temporary file. #### [`filename`](Temp/filename) Return the on-disk path associated with a `File::Temp` object. #### [`stringify`](Temp/stringify) Overload handler for `""` — stringify a `File::Temp` object to its filename. #### [`numify`](Temp/numify) Overload handler for `0+` — numify a `File::Temp` object to its reference address. #### [`tmpnam`](Temp/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`](Temp/tmpfile) POSIX-style `tmpfile` — open an anonymous temporary file and return the filehandle. The file is unlinked immediately. #### [`mkstemp`](Temp/mkstemp) Create a file from a template and return a filehandle together with its pathname. #### [`mkstemps`](Temp/mkstemps) Create a file from a template with a literal suffix preserved at the end of the name. ### Temporary directories #### [`tempdir`](Temp/tempdir) Create a uniquely named temporary directory and return its path. #### [`newdir`](Temp/newdir) Construct a `File::Temp::Dir` object wrapping a newly created temporary directory. #### [`mkdtemp`](Temp/mkdtemp) Create a directory from a template and return its path. #### [`dir_dirname`](Temp/dir_dirname) Return the on-disk path for a `File::Temp::Dir` object. #### [`dir_stringify`](Temp/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`](Temp/unlink_on_destroy) Toggle or query whether a `File::Temp` object unlinks its file when destroyed. #### [`destroy`](Temp/destroy) Destructor for `File::Temp` objects — removes the underlying file when `UNLINK` is set and `$File::Temp::KEEP_ALL` is false. #### [`cleanup`](Temp/cleanup) Remove every temp file and directory currently registered for cleanup, immediately, and return `1`. #### [`unlink0`](Temp/unlink0) Remove a temporary file while its filehandle is still open. #### [`unlink1`](Temp/unlink1) Unlink a temporary file. Upstream closes `$fh` first; here the filehandle is left open and closed by its usual refcount path. #### [`safe_level`](Temp/safe_level) Report the current safety level used by `File::Temp` checks. #### [`top_system_uid`](Temp/top_system_uid) Return the highest uid treated as a system account for ancestor ownership checks. #### [`cmpstat`](Temp/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`](Temp/dir_unlink_on_destroy) Toggle or query whether a `File::Temp::Dir` object removes its directory tree on destruction. #### [`dir_destroy`](Temp/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`](Temp/tempnam) Return a candidate filename under `$dir` starting with `$prefix` — does not create the file. #### [`mktemp`](Temp/mktemp) Fill in the trailing `X`s of a template and return the resulting filename — does not create the file. ```{toctree} :hidden: :maxdepth: 1 Temp/tempfile Temp/tempdir Temp/new Temp/newdir Temp/filename Temp/stringify Temp/numify Temp/unlink_on_destroy Temp/destroy Temp/tmpnam Temp/tmpfile Temp/tempnam Temp/mktemp Temp/mkstemp Temp/mkstemps Temp/mkdtemp Temp/cleanup Temp/unlink0 Temp/unlink1 Temp/safe_level Temp/top_system_uid Temp/cmpstat Temp/dir_dirname Temp/dir_stringify Temp/dir_unlink_on_destroy Temp/dir_destroy ```