Skip to Content
Programming Perl, 3rd Edition
book

Programming Perl, 3rd Edition

by Larry Wall, Tom Christiansen, Jon Orwant
July 2000
Intermediate to advanced
1104 pages
35h 1m
English
O'Reilly Media, Inc.
Content preview from Programming Perl, 3rd Edition

File::Temp

use File::Temp qw(tempfile tempdir);

$dir = tempdir(CLEANUP => 1);
($fh, $filename) = tempfile(DIR => $dir);
($fh, $filename) = tempfile($template, DIR => $dir);
($fh, $filename) = tempfile($template, SUFFIX => ".data");
$fh = tempfile();

use File::Temp ':mktemp';

($fh, $filename) = mkstemp("tmpfileXXXXX");
($fh, $filename) = mkstemps("tmpfileXXXXXX", $suffix);
$tmpdir = mkdtemp($template);
$unopened_file = mktemp($template);

New to version 5.6.1 of Perl, the File::Temp module provides convenient functions for creating and opening temporary files securely. It's better to use this module than to try to pick a temporary file on your own. Otherwise, you'll just fall into all the same traps as everyone else before you. This module guards you against various subtle race conditions, as well as the dangers of using directories that others can write to; see Chapter 23. The tempfile function returns both a filehandle and filename. It's safest to use the filehandle that's already open and ignore the filename entirely (except perhaps for error messages). Once the file is closed, it is automatically deleted. For compatibility with the C library, the :mktemp import tag provides access to functions with names familiar to C programmers, but please remember that filenames are always less secure than filehandles.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Perl, 4th Edition

Programming Perl, 4th Edition

Tom Christiansen, brian d foy, Larry Wall, Jon Orwant
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming the Perl DBI

Programming the Perl DBI

Tim Bunce, Alligator Descartes

Publisher Resources

ISBN: 0596000278Supplemental ContentErrata