tempdir#

Create a uniquely named temporary directory and return its path.

Synopsis#

my $dir = tempdir();
my $dir = tempdir($template, DIR => $parent, CLEANUP => 1);
my $dir = tempdir(CLEANUP => 1);

What you get back#

A plain string — the full path of the new directory, created with mode 0700. The directory is not removed automatically unless CLEANUP => 1 is passed.

Options#

  • DIR => $parent — parent directory for the new temp dir.

  • TMPDIR => 1 — place the directory under the system temp dir.

  • TEMPLATE => $t — template with at least four trailing X.

  • CLEANUP => 1 — remove the directory (and its contents) at exit.

Examples#

use File::Temp qw/ tempdir /;
my $dir = tempdir(CLEANUP => 1);
open my $fh, '>', "$dir/notes.txt";

## $dir and its contents are removed at process exit
my $dir = tempdir('buildXXXXXX', DIR => '/var/tmp');

## directory remains after the process exits (no CLEANUP)

Edge cases#

  • Called as File::Temp->tempdir (method form): croaks.

  • Parent directory missing or not a directory: croaks.

  • Without CLEANUP, the directory persists — the caller owns it.

Differences from upstream#

Fully compatible with upstream File::Temp 0.2312.

See also#

  • File::Temp->newdir — OO form whose object removes the tree on destruction.

  • mkdtemp — template-only low-level variant.

  • tempfile — file counterpart with the same option vocabulary.

  • cleanup — trigger registered cleanups manually.