new#

Construct a File::Temp object wrapping a newly created temporary file.

Synopsis#

my $tmp = File::Temp->new;
my $tmp = File::Temp->new(TEMPLATE => 'dataXXXXXX', DIR => '/var/tmp', SUFFIX => '.dat');

What you get back#

A blessed object that acts as an open read/write filehandle. It stringifies to its filename and numifies to refaddr. The underlying file is unlinked when the object is destroyed, unless you opt out with UNLINK => 0.

Options#

Same as tempfile, with two differences: UNLINK defaults to true and OPEN is always on. Pass TEMPLATE => 'nameXXXXXX' to control the filename pattern.

Examples#

use File::Temp;
my $tmp = File::Temp->new(SUFFIX => '.json');
print $tmp qq({"ok":1}\n);
my $path = $tmp->filename;                  # /tmp/XXXXXXX.json
my $tmp = File::Temp->new(UNLINK => 0);     # keep the file after exit

Edge cases#

  • OPEN => 0 is silently ignored; the file is always opened.

  • Object destruction while $File::Temp::KEEP_ALL is true skips unlink.

Differences from upstream#

Fully compatible with upstream File::Temp 0.2312.

See also#

  • tempfile — functional counterpart returning ($fh, $filename).

  • File::Temp->newdir — directory constructor.

  • filename — retrieve the pathname without stringifying.

  • unlink_on_destroy — toggle the unlink-on-destruction flag.