Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

File::Temp

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides tempfile() and tempdir() with automatic cleanup on process exit.

All created temp files and directories are tracked in a global registry. On process exit (via cleanup_temp_files()), entries marked for cleanup are removed. This prevents stale temp files accumulating in /tmp.

Synopsis

use File::Temp qw(tempfile tempdir);
my ($fh, $filename) = tempfile(SUFFIX => '.dat');
print $fh "some data\n";
my $dir = tempdir(CLEANUP => 1);
# OO interface
my $tmp = File::Temp->new(DIR => '/tmp', SUFFIX => '.log');
print $tmp "log line\n";
my $name = $tmp->filename;

Functions

cleanup

Explicitly trigger cleanup of all registered temporary files and directories. Normally called automatically at process exit.

Synopsis

use File::Temp 'cleanup';
cleanup();

dir_noop

No-op fallback for File::Temp::Dir overload dispatch.

See also: native_dir_stringify

dir_stringify

Stringify overload for File::Temp::Dir objects, returns the directory path.

See also: tempdir, newdir

filename

Instance method. Returns the filename of a File::Temp object by extracting the __filename key from the blessed hashref.

Synopsis

my $fh = File::Temp->new();
my $name = $fh->filename;

mkdtemp

Create a temporary directory from a template. Trailing X characters are replaced with pseudo-random digits. Registered for automatic cleanup.

Synopsis

use File::Temp 'mkdtemp';
my $dir = mkdtemp("myapp_XXXXXXXX");

mkstemp

Create a temporary file from a template and return ($fh, $filename). Trailing X characters are replaced with pseudo-random digits. The file is created exclusively and registered for cleanup.

Synopsis

use File::Temp 'mkstemp';
my ($fh, $name) = mkstemp("myapp_XXXXXXXX");

mkstemps

Like mkstemp but with a suffix. The second argument specifies the suffix length: template characters after that point are preserved as the file extension.

Synopsis

use File::Temp 'mkstemps';
my ($fh, $name) = mkstemps("myapp_XXXXXXXX.dat", 4);

mktemp

Generate a temporary filename from a template. Trailing X characters in the template are replaced with pseudo-random digits.

Synopsis

use File::Temp 'mktemp';
my $name = mktemp("myapp_XXXXXXXX");

new

OO constructor. Creates a temporary file and returns a blessed File::Temp hashref that doubles as a filehandle. Accepts options: DIR, SUFFIX, TEMPLATE/PREFIX, UNLINK.

Synopsis

use File::Temp;
my $fh = File::Temp->new(SUFFIX => '.dat', DIR => '/tmp');
print $fh "data\n";

newdir

OO constructor for temporary directories. Creates a temp directory and returns a blessed File::Temp::Dir hashref with stringify overload. Accepts options: DIR, CLEANUP, TEMPLATE/PREFIX.

Synopsis

use File::Temp;
my $dir = File::Temp->newdir(CLEANUP => 1);
print "$dir\n";  # stringifies to path

tempdir

Create a temporary directory and return its path. Registered for automatic cleanup unless CLEANUP => 0. Accepts options: DIR, CLEANUP, PREFIX/TEMPLATE.

Synopsis

use File::Temp 'tempdir';
my $dir = tempdir(CLEANUP => 1);

tempfile

Create a temporary file and return ($fh, $filename) in list context. The file is created exclusively (race-free via O_EXCL). Accepts options: DIR, SUFFIX, PREFIX/TEMPLATE, UNLINK.

Synopsis

use File::Temp 'tempfile';
my ($fh, $name) = tempfile(DIR => '/tmp', SUFFIX => '.txt');

tmpfile

tmpnam

Generate a temporary filename (without creating it). Returns a path in the system temp directory with a pseudo-random name.

Synopsis

use File::Temp 'tmpnam';
my $name = tmpnam();

unlink0

Unlink a temporary file by name while it may still be open, and remove it from the cleanup registry (since it is already deleted).

Synopsis

use File::Temp 'unlink0';
unlink0($fh, $filename);