mkstemp#

Create a file from a template and return a filehandle together with its pathname.

Synopsis#

my ($fh, $path) = mkstemp('/tmp/dataXXXXXX');
my  $fh         = mkstemp('/tmp/dataXXXXXX');   # scalar context

What you get back#

In list context ($fh, $path); in scalar context just the filehandle. The template must end in at least four X characters, which are replaced atomically with random bytes.

Examples#

use File::Temp qw/ mkstemp /;
my ($fh, $path) = mkstemp('/var/tmp/workXXXXXX');
print $fh "payload\n";

Edge cases#

  • Template with fewer than four trailing Xs: croaks.

  • Wrong arg count: croaks with a usage message.

Differences from upstream#

Fully compatible with upstream File::Temp 0.2312.

See also#

  • tempfile — higher-level variant accepting DIR, SUFFIX, UNLINK.

  • mkstemps — like mkstemp but keeps a literal suffix on the name.

  • mkdtemp — directory counterpart.