Debian Codegraph @

upstream/opensuse-tumbleweed/man3/File::Temp.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "File::Temp 3pm"
61 .TH File::Temp 3pm 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 File::Temp \- return name and handle of a temporary file safely
68 .SH VERSION
69 .IX Header "VERSION"
70 version 0.2311
71 .SH SYNOPSIS
72 .IX Header "SYNOPSIS"
73 .Vb 1
74 \& use File::Temp qw/ tempfile tempdir /;
75 \&
76 \& $fh = tempfile();
77 \& ($fh, $filename) = tempfile();
78 \&
79 \& ($fh, $filename) = tempfile( $template, DIR => $dir);
80 \& ($fh, $filename) = tempfile( $template, SUFFIX => \*(Aq.dat\*(Aq);
81 \& ($fh, $filename) = tempfile( $template, TMPDIR => 1 );
82 \&
83 \& binmode( $fh, ":utf8" );
84 \&
85 \& $dir = tempdir( CLEANUP => 1 );
86 \& ($fh, $filename) = tempfile( DIR => $dir );
87 .Ve
88 .PP
89 Object interface:
90 .PP
91 .Vb 3
92 \& require File::Temp;
93 \& use File::Temp ();
94 \& use File::Temp qw/ :seekable /;
95 \&
96 \& $fh = File::Temp\->new();
97 \& $fname = $fh\->filename;
98 \&
99 \& $fh = File::Temp\->new(TEMPLATE => $template);
100 \& $fname = $fh\->filename;
101 \&
102 \& $tmp = File::Temp\->new( UNLINK => 0, SUFFIX => \*(Aq.dat\*(Aq );
103 \& print $tmp "Some data\en";
104 \& print "Filename is $tmp\en";
105 \& $tmp\->seek( 0, SEEK_END );
106 \&
107 \& $dir = File::Temp\->newdir(); # CLEANUP => 1 by default
108 .Ve
109 .PP
110 The following interfaces are provided for compatibility with
111 existing APIs. They should not be used in new code.
112 .PP
113 MkTemp family:
114 .PP
115 .Vb 1
116 \& use File::Temp qw/ :mktemp /;
117 \&
118 \& ($fh, $file) = mkstemp( "tmpfileXXXXX" );
119 \& ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix);
120 \&
121 \& $tmpdir = mkdtemp( $template );
122 \&
123 \& $unopened_file = mktemp( $template );
124 .Ve
125 .PP
126 POSIX functions:
127 .PP
128 .Vb 1
129 \& use File::Temp qw/ :POSIX /;
130 \&
131 \& $file = tmpnam();
132 \& $fh = tmpfile();
133 \&
134 \& ($fh, $file) = tmpnam();
135 .Ve
136 .PP
137 Compatibility functions:
138 .PP
139 .Vb 1
140 \& $unopened_file = File::Temp::tempnam( $dir, $pfx );
141 .Ve
142 .SH DESCRIPTION
143 .IX Header "DESCRIPTION"
144 \&\f(CW\*(C`File::Temp\*(C'\fR can be used to create and open temporary files in a safe
145 way. There is both a function interface and an object\-oriented
146 interface. The File::Temp constructor or the \fBtempfile()\fR function can
147 be used to return the name and the open filehandle of a temporary
148 file. The \fBtempdir()\fR function can be used to create a temporary
149 directory.
150 .PP
151 The security aspect of temporary file creation is emphasized such that
152 a filehandle and filename are returned together. This helps guarantee
153 that a race condition can not occur where the temporary file is
154 created by another process between checking for the existence of the
155 file and its opening. Additional security levels are provided to
156 check, for example, that the sticky bit is set on world writable
157 directories. See "safe_level" for more information.
158 .PP
159 For compatibility with popular C library functions, Perl implementations of
160 the \fBmkstemp()\fR family of functions are provided. These are, \fBmkstemp()\fR,
161 \&\fBmkstemps()\fR, \fBmkdtemp()\fR and \fBmktemp()\fR.
162 .PP
163 Additionally, implementations of the standard POSIX
164 \&\fBtmpnam()\fR and \fBtmpfile()\fR functions are provided if required.
165 .PP
166 Implementations of \fBmktemp()\fR, \fBtmpnam()\fR, and \fBtempnam()\fR are provided,
167 but should be used with caution since they return only a filename
168 that was valid when function was called, so cannot guarantee
169 that the file will not exist by the time the caller opens the filename.
170 .PP
171 Filehandles returned by these functions support the seekable methods.
172 .SH "OBJECT\-ORIENTED INTERFACE"
173 .IX Header "OBJECT-ORIENTED INTERFACE"
174 This is the primary interface for interacting with
175 \&\f(CW\*(C`File::Temp\*(C'\fR. Using the OO interface a temporary file can be created
176 when the object is constructed and the file can be removed when the
177 object is no longer required.
178 .PP
179 Note that there is no method to obtain the filehandle from the
180 \&\f(CW\*(C`File::Temp\*(C'\fR object. The object itself acts as a filehandle. The object
181 isa \f(CW\*(C`IO::Handle\*(C'\fR and isa \f(CW\*(C`IO::Seekable\*(C'\fR so all those methods are
182 available.
183 .PP
184 Also, the object is configured such that it stringifies to the name of the
185 temporary file and so can be compared to a filename directly. It numifies
186 to the \f(CW\*(C`refaddr\*(C'\fR the same as other handles and so can be compared to other
187 handles with \f(CW\*(C`==\*(C'\fR.
188 .PP
189 .Vb 2
190 \& $fh eq $filename # as a string
191 \& $fh != \e*STDOUT # as a number
192 .Ve
193 .PP
194 Available since 0.14.
195 .IP \fBnew\fR 4
196 .IX Item "new"
197 Create a temporary file object.
198 .Sp
199 .Vb 1
200 \& my $tmp = File::Temp\->new();
201 .Ve
202 .Sp
203 by default the object is constructed as if \f(CW\*(C`tempfile\*(C'\fR
204 was called without options, but with the additional behaviour
205 that the temporary file is removed by the object destructor
206 if UNLINK is set to true (the default).
207 .Sp
208 Supported arguments are the same as for \f(CW\*(C`tempfile\*(C'\fR: UNLINK
209 (defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
210 Additionally, the filename
211 template is specified using the TEMPLATE option. The OPEN option
212 is not supported (the file is always opened).
213 .Sp
214 .Vb 3
215 \& $tmp = File::Temp\->new( TEMPLATE => \*(AqtempXXXXX\*(Aq,
216 \& DIR => \*(Aqmydir\*(Aq,
217 \& SUFFIX => \*(Aq.dat\*(Aq);
218 .Ve
219 .Sp
220 Arguments are case insensitive.
221 .Sp
222 Can call \fBcroak()\fR if an error occurs.
223 .Sp
224 Available since 0.14.
225 .Sp
226 TEMPLATE available since 0.23
227 .IP \fBnewdir\fR 4
228 .IX Item "newdir"
229 Create a temporary directory using an object oriented interface.
230 .Sp
231 .Vb 1
232 \& $dir = File::Temp\->newdir();
233 .Ve
234 .Sp
235 By default the directory is deleted when the object goes out of scope.
236 .Sp
237 Supports the same options as the \f(CW\*(C`tempdir\*(C'\fR function. Note that directories
238 created with this method default to CLEANUP => 1.
239 .Sp
240 .Vb 1
241 \& $dir = File::Temp\->newdir( $template, %options );
242 .Ve
243 .Sp
244 A template may be specified either with a leading template or
245 with a TEMPLATE argument.
246 .Sp
247 Available since 0.19.
248 .Sp
249 TEMPLATE available since 0.23.
250 .IP \fBfilename\fR 4
251 .IX Item "filename"
252 Return the name of the temporary file associated with this object
253 (if the object was created using the "new" constructor).
254 .Sp
255 .Vb 1
256 \& $filename = $tmp\->filename;
257 .Ve
258 .Sp
259 This method is called automatically when the object is used as
260 a string.
261 .Sp
262 Current API available since 0.14
263 .IP \fBdirname\fR 4
264 .IX Item "dirname"
265 Return the name of the temporary directory associated with this
266 object (if the object was created using the "newdir" constructor).
267 .Sp
268 .Vb 1
269 \& $dirname = $tmpdir\->dirname;
270 .Ve
271 .Sp
272 This method is called automatically when the object is used in string context.
273 .IP \fBunlink_on_destroy\fR 4
274 .IX Item "unlink_on_destroy"
275 Control whether the file is unlinked when the object goes out of scope.
276 The file is removed if this value is true and \f(CW$KEEP_ALL\fR is not.
277 .Sp
278 .Vb 1
279 \& $fh\->unlink_on_destroy( 1 );
280 .Ve
281 .Sp
282 Default is for the file to be removed.
283 .Sp
284 Current API available since 0.15
285 .IP \fBDESTROY\fR 4
286 .IX Item "DESTROY"
287 When the object goes out of scope, the destructor is called. This
288 destructor will attempt to unlink the file (using unlink1)
289 if the constructor was called with UNLINK set to 1 (the default state
290 if UNLINK is not specified).
291 .Sp
292 No error is given if the unlink fails.
293 .Sp
294 If the object has been passed to a child process during a fork, the
295 file will be deleted when the object goes out of scope in the parent.
296 .Sp
297 For a temporary directory object the directory will be removed unless
298 the CLEANUP argument was used in the constructor (and set to false) or
299 \&\f(CW\*(C`unlink_on_destroy\*(C'\fR was modified after creation. Note that if a temp
300 directory is your current directory, it cannot be removed \- a warning
301 will be given in this case. \f(CWchdir()\fR out of the directory before
302 letting the object go out of scope.
303 .Sp
304 If the global variable \f(CW$KEEP_ALL\fR is true, the file or directory
305 will not be removed.
306 .SH FUNCTIONS
307 .IX Header "FUNCTIONS"
308 This section describes the recommended interface for generating
309 temporary files and directories.
310 .IP \fBtempfile\fR 4
311 .IX Item "tempfile"
312 This is the basic function to generate temporary files.
313 The behaviour of the file can be changed using various options:
314 .Sp
315 .Vb 2
316 \& $fh = tempfile();
317 \& ($fh, $filename) = tempfile();
318 .Ve
319 .Sp
320 Create a temporary file in the directory specified for temporary
321 files, as specified by the \fBtmpdir()\fR function in File::Spec.
322 .Sp
323 .Vb 1
324 \& ($fh, $filename) = tempfile($template);
325 .Ve
326 .Sp
327 Create a temporary file in the current directory using the supplied
328 template. Trailing \`X\*(Aq characters are replaced with random letters to
329 generate the filename. At least four \`X\*(Aq characters must be present
330 at the end of the template.
331 .Sp
332 .Vb 1
333 \& ($fh, $filename) = tempfile($template, SUFFIX => $suffix)
334 .Ve
335 .Sp
336 Same as previously, except that a suffix is added to the template
337 after the \`X\*(Aq translation. Useful for ensuring that a temporary
338 filename has a particular extension when needed by other applications.
339 But see the WARNING at the end.
340 .Sp
341 .Vb 1
342 \& ($fh, $filename) = tempfile($template, DIR => $dir);
343 .Ve
344 .Sp
345 Translates the template as before except that a directory name
346 is specified.
347 .Sp
348 .Vb 1
349 \& ($fh, $filename) = tempfile($template, TMPDIR => 1);
350 .Ve
351 .Sp
352 Equivalent to specifying a DIR of "File::Spec\->tmpdir", writing the file
353 into the same temporary directory as would be used if no template was
354 specified at all.
355 .Sp
356 .Vb 1
357 \& ($fh, $filename) = tempfile($template, UNLINK => 1);
358 .Ve
359 .Sp
360 Return the filename and filehandle as before except that the file is
361 automatically removed when the program exits (dependent on
362 \&\f(CW$KEEP_ALL\fR). Default is for the file to be removed if a file handle is
363 requested and to be kept if the filename is requested. In a scalar
364 context (where no filename is returned) the file is always deleted
365 either (depending on the operating system) on exit or when it is
366 closed (unless \f(CW$KEEP_ALL\fR is true when the temp file is created).
367 .Sp
368 Use the object\-oriented interface if fine\-grained control of when
369 a file is removed is required.
370 .Sp
371 If the template is not specified, a template is always
372 automatically generated. This temporary file is placed in \fBtmpdir()\fR
373 (File::Spec) unless a directory is specified explicitly with the
374 DIR option.
375 .Sp
376 .Vb 1
377 \& $fh = tempfile( DIR => $dir );
378 .Ve
379 .Sp
380 If called in scalar context, only the filehandle is returned and the
381 file will automatically be deleted when closed on operating systems
382 that support this (see the description of \fBtmpfile()\fR elsewhere in this
383 document). This is the preferred mode of operation, as if you only
384 have a filehandle, you can never create a race condition by fumbling
385 with the filename. On systems that can not unlink an open file or can
386 not mark a file as temporary when it is opened (for example, Windows
387 NT uses the \f(CW\*(C`O_TEMPORARY\*(C'\fR flag) the file is marked for deletion when
388 the program ends (equivalent to setting UNLINK to 1). The \f(CW\*(C`UNLINK\*(C'\fR
389 flag is ignored if present.
390 .Sp
391 .Vb 1
392 \& (undef, $filename) = tempfile($template, OPEN => 0);
393 .Ve
394 .Sp
395 This will return the filename based on the template but
396 will not open this file. Cannot be used in conjunction with
397 UNLINK set to true. Default is to always open the file
398 to protect from possible race conditions. A warning is issued
399 if warnings are turned on. Consider using the \fBtmpnam()\fR
400 and \fBmktemp()\fR functions described elsewhere in this document
401 if opening the file is not required.
402 .Sp
403 To open the temporary filehandle with O_EXLOCK (open with exclusive
404 file lock) use \f(CW\*(C`EXLOCK=>1\*(C'\fR. This is supported only by some
405 operating systems (most notably BSD derived systems). By default
406 EXLOCK will be false. Former \f(CW\*(C`File::Temp\*(C'\fR versions set EXLOCK to
407 true, so to be sure to get an unlocked filehandle also with older
408 versions, explicitly set \f(CW\*(C`EXLOCK=>0\*(C'\fR.
409 .Sp
410 .Vb 1
411 \& ($fh, $filename) = tempfile($template, EXLOCK => 1);
412 .Ve
413 .Sp
414 By default, the temp file is created with 0600 file permissions.
415 Use \f(CW\*(C`PERMS\*(C'\fR to change this:
416 .Sp
417 .Vb 1
418 \& ($fh, $filename) = tempfile($template, PERMS => 0666);
419 .Ve
420 .Sp
421 Options can be combined as required.
422 .Sp
423 Will \fBcroak()\fR if there is an error.
424 .Sp
425 Available since 0.05.
426 .Sp
427 UNLINK flag available since 0.10.
428 .Sp
429 TMPDIR flag available since 0.19.
430 .Sp
431 EXLOCK flag available since 0.19.
432 .Sp
433 PERMS flag available since 0.2310.
434 .IP \fBtempdir\fR 4
435 .IX Item "tempdir"
436 This is the recommended interface for creation of temporary
437 directories. By default the directory will not be removed on exit
438 (that is, it won\*(Aqt be temporary; this behaviour can not be changed
439 because of issues with backwards compatibility). To enable removal
440 either use the CLEANUP option which will trigger removal on program
441 exit, or consider using the "newdir" method in the object interface which
442 will allow the directory to be cleaned up when the object goes out of
443 scope.
444 .Sp
445 The behaviour of the function depends on the arguments:
446 .Sp
447 .Vb 1
448 \& $tempdir = tempdir();
449 .Ve
450 .Sp
451 Create a directory in \fBtmpdir()\fR (see File::Spec).
452 .Sp
453 .Vb 1
454 \& $tempdir = tempdir( $template );
455 .Ve
456 .Sp
457 Create a directory from the supplied template. This template is
458 similar to that described for \fBtempfile()\fR. \`X\*(Aq characters at the end
459 of the template are replaced with random letters to construct the
460 directory name. At least four \`X\*(Aq characters must be in the template.
461 .Sp
462 .Vb 1
463 \& $tempdir = tempdir ( DIR => $dir );
464 .Ve
465 .Sp
466 Specifies the directory to use for the temporary directory.
467 The temporary directory name is derived from an internal template.
468 .Sp
469 .Vb 1
470 \& $tempdir = tempdir ( $template, DIR => $dir );
471 .Ve
472 .Sp
473 Prepend the supplied directory name to the template. The template
474 should not include parent directory specifications itself. Any parent
475 directory specifications are removed from the template before
476 prepending the supplied directory.
477 .Sp
478 .Vb 1
479 \& $tempdir = tempdir ( $template, TMPDIR => 1 );
480 .Ve
481 .Sp
482 Using the supplied template, create the temporary directory in
483 a standard location for temporary files. Equivalent to doing
484 .Sp
485 .Vb 1
486 \& $tempdir = tempdir ( $template, DIR => File::Spec\->tmpdir);
487 .Ve
488 .Sp
489 but shorter. Parent directory specifications are stripped from the
490 template itself. The \f(CW\*(C`TMPDIR\*(C'\fR option is ignored if \f(CW\*(C`DIR\*(C'\fR is set
491 explicitly. Additionally, \f(CW\*(C`TMPDIR\*(C'\fR is implied if neither a template
492 nor a directory are supplied.
493 .Sp
494 .Vb 1
495 \& $tempdir = tempdir( $template, CLEANUP => 1);
496 .Ve
497 .Sp
498 Create a temporary directory using the supplied template, but
499 attempt to remove it (and all files inside it) when the program
500 exits. Note that an attempt will be made to remove all files from
501 the directory even if they were not created by this module (otherwise
502 why ask to clean it up?). The directory removal is made with
503 the \fBrmtree()\fR function from the File::Path module.
504 Of course, if the template is not specified, the temporary directory
505 will be created in \fBtmpdir()\fR and will also be removed at program exit.
506 .Sp
507 Will \fBcroak()\fR if there is an error.
508 .Sp
509 Current API available since 0.05.
510 .SH "MKTEMP FUNCTIONS"
511 .IX Header "MKTEMP FUNCTIONS"
512 The following functions are Perl implementations of the
513 \&\fBmktemp()\fR family of temp file generation system calls.
514 .IP \fBmkstemp\fR 4
515 .IX Item "mkstemp"
516 Given a template, returns a filehandle to the temporary file and the name
517 of the file.
518 .Sp
519 .Vb 1
520 \& ($fh, $name) = mkstemp( $template );
521 .Ve
522 .Sp
523 In scalar context, just the filehandle is returned.
524 .Sp
525 The template may be any filename with some number of X\*(Aqs appended
526 to it, for example \fI/tmp/temp.XXXX\fR. The trailing X\*(Aqs are replaced
527 with unique alphanumeric combinations.
528 .Sp
529 Will \fBcroak()\fR if there is an error.
530 .Sp
531 Current API available since 0.05.
532 .IP \fBmkstemps\fR 4
533 .IX Item "mkstemps"
534 Similar to \fBmkstemp()\fR, except that an extra argument can be supplied
535 with a suffix to be appended to the template.
536 .Sp
537 .Vb 1
538 \& ($fh, $name) = mkstemps( $template, $suffix );
539 .Ve
540 .Sp
541 For example a template of \f(CW\*(C`testXXXXXX\*(C'\fR and suffix of \f(CW\*(C`.dat\*(C'\fR
542 would generate a file similar to \fItesthGji_w.dat\fR.
543 .Sp
544 Returns just the filehandle alone when called in scalar context.
545 .Sp
546 Will \fBcroak()\fR if there is an error.
547 .Sp
548 Current API available since 0.05.
549 .IP \fBmkdtemp\fR 4
550 .IX Item "mkdtemp"
551 Create a directory from a template. The template must end in
552 X\*(Aqs that are replaced by the routine.
553 .Sp
554 .Vb 1
555 \& $tmpdir_name = mkdtemp($template);
556 .Ve
557 .Sp
558 Returns the name of the temporary directory created.
559 .Sp
560 Directory must be removed by the caller.
561 .Sp
562 Will \fBcroak()\fR if there is an error.
563 .Sp
564 Current API available since 0.05.
565 .IP \fBmktemp\fR 4
566 .IX Item "mktemp"
567 Returns a valid temporary filename but does not guarantee
568 that the file will not be opened by someone else.
569 .Sp
570 .Vb 1
571 \& $unopened_file = mktemp($template);
572 .Ve
573 .Sp
574 Template is the same as that required by \fBmkstemp()\fR.
575 .Sp
576 Will \fBcroak()\fR if there is an error.
577 .Sp
578 Current API available since 0.05.
579 .SH "POSIX FUNCTIONS"
580 .IX Header "POSIX FUNCTIONS"
581 This section describes the re\-implementation of the \fBtmpnam()\fR
582 and \fBtmpfile()\fR functions described in POSIX
583 using the \fBmkstemp()\fR from this module.
584 .PP
585 Unlike the POSIX implementations, the directory used
586 for the temporary file is not specified in a system include
587 file (\f(CW\*(C`P_tmpdir\*(C'\fR) but simply depends on the choice of \fBtmpdir()\fR
588 returned by File::Spec. On some implementations this
589 location can be set using the \f(CW\*(C`TMPDIR\*(C'\fR environment variable, which
590 may not be secure.
591 If this is a problem, simply use \fBmkstemp()\fR and specify a template.
592 .IP \fBtmpnam\fR 4
593 .IX Item "tmpnam"
594 When called in scalar context, returns the full name (including path)
595 of a temporary file (uses \fBmktemp()\fR). The only check is that the file does
596 not already exist, but there is no guarantee that that condition will
597 continue to apply.
598 .Sp
599 .Vb 1
600 \& $file = tmpnam();
601 .Ve
602 .Sp
603 When called in list context, a filehandle to the open file and
604 a filename are returned. This is achieved by calling \fBmkstemp()\fR
605 after constructing a suitable template.
606 .Sp
607 .Vb 1
608 \& ($fh, $file) = tmpnam();
609 .Ve
610 .Sp
611 If possible, this form should be used to prevent possible
612 race conditions.
613 .Sp
614 See "tmpdir" in File::Spec for information on the choice of temporary
615 directory for a particular operating system.
616 .Sp
617 Will \fBcroak()\fR if there is an error.
618 .Sp
619 Current API available since 0.05.
620 .IP \fBtmpfile\fR 4
621 .IX Item "tmpfile"
622 Returns the filehandle of a temporary file.
623 .Sp
624 .Vb 1
625 \& $fh = tmpfile();
626 .Ve
627 .Sp
628 The file is removed when the filehandle is closed or when the program
629 exits. No access to the filename is provided.
630 .Sp
631 If the temporary file can not be created undef is returned.
632 Currently this command will probably not work when the temporary
633 directory is on an NFS file system.
634 .Sp
635 Will \fBcroak()\fR if there is an error.
636 .Sp
637 Available since 0.05.
638 .Sp
639 Returning undef if unable to create file added in 0.12.
640 .SH "ADDITIONAL FUNCTIONS"
641 .IX Header "ADDITIONAL FUNCTIONS"
642 These functions are provided for backwards compatibility
643 with common tempfile generation C library functions.
644 .PP
645 They are not exported and must be addressed using the full package
646 name.
647 .IP \fBtempnam\fR 4
648 .IX Item "tempnam"
649 Return the name of a temporary file in the specified directory
650 using a prefix. The file is guaranteed not to exist at the time
651 the function was called, but such guarantees are good for one
652 clock tick only. Always use the proper form of \f(CW\*(C`sysopen\*(C'\fR
653 with \f(CW\*(C`O_CREAT | O_EXCL\*(C'\fR if you must open such a filename.
654 .Sp
655 .Vb 1
656 \& $filename = File::Temp::tempnam( $dir, $prefix );
657 .Ve
658 .Sp
659 Equivalent to running \fBmktemp()\fR with \f(CW$dir\fR/$prefixXXXXXXXX
660 (using unix file convention as an example)
661 .Sp
662 Because this function uses \fBmktemp()\fR, it can suffer from race conditions.
663 .Sp
664 Will \fBcroak()\fR if there is an error.
665 .Sp
666 Current API available since 0.05.
667 .SH "UTILITY FUNCTIONS"
668 .IX Header "UTILITY FUNCTIONS"
669 Useful functions for dealing with the filehandle and filename.
670 .IP \fBunlink0\fR 4
671 .IX Item "unlink0"
672 Given an open filehandle and the associated filename, make a safe
673 unlink. This is achieved by first checking that the filename and
674 filehandle initially point to the same file and that the number of
675 links to the file is 1 (all fields returned by \fBstat()\fR are compared).
676 Then the filename is unlinked and the filehandle checked once again to
677 verify that the number of links on that file is now 0. This is the
678 closest you can come to making sure that the filename unlinked was the
679 same as the file whose descriptor you hold.
680 .Sp
681 .Vb 2
682 \& unlink0($fh, $path)
683 \& or die "Error unlinking file $path safely";
684 .Ve
685 .Sp
686 Returns false on error but \fBcroaks()\fR if there is a security
687 anomaly. The filehandle is not closed since on some occasions this is
688 not required.
689 .Sp
690 On some platforms, for example Windows NT, it is not possible to
691 unlink an open file (the file must be closed first). On those
692 platforms, the actual unlinking is deferred until the program ends and
693 good status is returned. A check is still performed to make sure that
694 the filehandle and filename are pointing to the same thing (but not at
695 the time the end block is executed since the deferred removal may not
696 have access to the filehandle).
697 .Sp
698 Additionally, on Windows NT not all the fields returned by \fBstat()\fR can
699 be compared. For example, the \f(CW\*(C`dev\*(C'\fR and \f(CW\*(C`rdev\*(C'\fR fields seem to be
700 different. Also, it seems that the size of the file returned by \fBstat()\fR
701 does not always agree, with \f(CWstat(FH)\fR being more accurate than
702 \&\f(CWstat(filename)\fR, presumably because of caching issues even when
703 using autoflush (this is usually overcome by waiting a while after
704 writing to the tempfile before attempting to \f(CW\*(C`unlink0\*(C'\fR it).
705 .Sp
706 Finally, on NFS file systems the link count of the file handle does
707 not always go to zero immediately after unlinking. Currently, this
708 command is expected to fail on NFS disks.
709 .Sp
710 This function is disabled if the global variable \f(CW$KEEP_ALL\fR is true
711 and an unlink on open file is supported. If the unlink is to be deferred
712 to the END block, the file is still registered for removal.
713 .Sp
714 This function should not be called if you are using the object oriented
715 interface since the it will interfere with the object destructor deleting
716 the file.
717 .Sp
718 Available Since 0.05.
719 .Sp
720 If can not unlink open file, defer removal until later available since 0.06.
721 .IP \fBcmpstat\fR 4
722 .IX Item "cmpstat"
723 Compare \f(CW\*(C`stat\*(C'\fR of filehandle with \f(CW\*(C`stat\*(C'\fR of provided filename. This
724 can be used to check that the filename and filehandle initially point
725 to the same file and that the number of links to the file is 1 (all
726 fields returned by \fBstat()\fR are compared).
727 .Sp
728 .Vb 2
729 \& cmpstat($fh, $path)
730 \& or die "Error comparing handle with file";
731 .Ve
732 .Sp
733 Returns false if the stat information differs or if the link count is
734 greater than 1. Calls croak if there is a security anomaly.
735 .Sp
736 On certain platforms, for example Windows, not all the fields returned by \fBstat()\fR
737 can be compared. For example, the \f(CW\*(C`dev\*(C'\fR and \f(CW\*(C`rdev\*(C'\fR fields seem to be
738 different in Windows. Also, it seems that the size of the file
739 returned by \fBstat()\fR does not always agree, with \f(CWstat(FH)\fR being more
740 accurate than \f(CWstat(filename)\fR, presumably because of caching issues
741 even when using autoflush (this is usually overcome by waiting a while
742 after writing to the tempfile before attempting to \f(CW\*(C`unlink0\*(C'\fR it).
743 .Sp
744 Not exported by default.
745 .Sp
746 Current API available since 0.14.
747 .IP \fBunlink1\fR 4
748 .IX Item "unlink1"
749 Similar to \f(CW\*(C`unlink0\*(C'\fR except after file comparison using cmpstat, the
750 filehandle is closed prior to attempting to unlink the file. This
751 allows the file to be removed without using an END block, but does
752 mean that the post\-unlink comparison of the filehandle state provided
753 by \f(CW\*(C`unlink0\*(C'\fR is not available.
754 .Sp
755 .Vb 2
756 \& unlink1($fh, $path)
757 \& or die "Error closing and unlinking file";
758 .Ve
759 .Sp
760 Usually called from the object destructor when using the OO interface.
761 .Sp
762 Not exported by default.
763 .Sp
764 This function is disabled if the global variable \f(CW$KEEP_ALL\fR is true.
765 .Sp
766 Can call \fBcroak()\fR if there is a security anomaly during the \fBstat()\fR
767 comparison.
768 .Sp
769 Current API available since 0.14.
770 .IP \fBcleanup\fR 4
771 .IX Item "cleanup"
772 Calling this function will cause any temp files or temp directories
773 that are registered for removal to be removed. This happens automatically
774 when the process exits but can be triggered manually if the caller is sure
775 that none of the temp files are required. This method can be registered as
776 an Apache callback.
777 .Sp
778 Note that if a temp directory is your current directory, it cannot be
779 removed. \f(CWchdir()\fR out of the directory first before calling
780 \&\f(CWcleanup()\fR. (For the cleanup at program exit when the CLEANUP flag
781 is set, this happens automatically.)
782 .Sp
783 On OSes where temp files are automatically removed when the temp file
784 is closed, calling this function will have no effect other than to remove
785 temporary directories (which may include temporary files).
786 .Sp
787 .Vb 1
788 \& File::Temp::cleanup();
789 .Ve
790 .Sp
791 Not exported by default.
792 .Sp
793 Current API available since 0.15.
794 .SH "PACKAGE VARIABLES"
795 .IX Header "PACKAGE VARIABLES"
796 These functions control the global state of the package.
797 .IP \fBsafe_level\fR 4
798 .IX Item "safe_level"
799 Controls the lengths to which the module will go to check the safety of the
800 temporary file or directory before proceeding.
801 Options are:
802 .RS 4
803 .IP STANDARD 8
804 .IX Item "STANDARD"
805 Do the basic security measures to ensure the directory exists and is
806 writable, that temporary files are opened only if they do not already
807 exist, and that possible race conditions are avoided. Finally the
808 unlink0 function is used to remove files safely.
809 .IP MEDIUM 8
810 .IX Item "MEDIUM"
811 In addition to the STANDARD security, the output directory is checked
812 to make sure that it is owned either by root or the user running the
813 program. If the directory is writable by group or by other, it is then
814 checked to make sure that the sticky bit is set.
815 .Sp
816 Will not work on platforms that do not support the \f(CW\*(C`\-k\*(C'\fR test
817 for sticky bit.
818 .IP HIGH 8
819 .IX Item "HIGH"
820 In addition to the MEDIUM security checks, also check for the
821 possibility of \`\`\fBchown()\fR giveaway\*(Aq\*(Aq using the POSIX
822 \&\fBsysconf()\fR function. If this is a possibility, each directory in the
823 path is checked in turn for safeness, recursively walking back to the
824 root directory.
825 .Sp
826 For platforms that do not support the POSIX
827 \&\f(CW\*(C`_PC_CHOWN_RESTRICTED\*(C'\fR symbol (for example, Windows NT) it is
828 assumed that \`\`\fBchown()\fR giveaway\*(Aq\*(Aq is possible and the recursive test
829 is performed.
830 .RE
831 .RS 4
832 .Sp
833 The level can be changed as follows:
834 .Sp
835 .Vb 1
836 \& File::Temp\->safe_level( File::Temp::HIGH );
837 .Ve
838 .Sp
839 The level constants are not exported by the module.
840 .Sp
841 Currently, you must be running at least perl v5.6.0 in order to
842 run with MEDIUM or HIGH security. This is simply because the
843 safety tests use functions from Fcntl that are not
844 available in older versions of perl. The problem is that the version
845 number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though
846 they are different versions.
847 .Sp
848 On systems that do not support the HIGH or MEDIUM safety levels
849 (for example Win NT or OS/2) any attempt to change the level will
850 be ignored. The decision to ignore rather than raise an exception
851 allows portable programs to be written with high security in mind
852 for the systems that can support this without those programs failing
853 on systems where the extra tests are irrelevant.
854 .Sp
855 If you really need to see whether the change has been accepted
856 simply examine the return value of \f(CW\*(C`safe_level\*(C'\fR.
857 .Sp
858 .Vb 3
859 \& $newlevel = File::Temp\->safe_level( File::Temp::HIGH );
860 \& die "Could not change to high security"
861 \& if $newlevel != File::Temp::HIGH;
862 .Ve
863 .Sp
864 Available since 0.05.
865 .RE
866 .IP TopSystemUID 4
867 .IX Item "TopSystemUID"
868 This is the highest UID on the current system that refers to a root
869 UID. This is used to make sure that the temporary directory is
870 owned by a system UID (\f(CW\*(C`root\*(C'\fR, \f(CW\*(C`bin\*(C'\fR, \f(CW\*(C`sys\*(C'\fR etc) rather than
871 simply by root.
872 .Sp
873 This is required since on many unix systems \f(CW\*(C`/tmp\*(C'\fR is not owned
874 by root.
875 .Sp
876 Default is to assume that any UID less than or equal to 10 is a root
877 UID.
878 .Sp
879 .Vb 2
880 \& File::Temp\->top_system_uid(10);
881 \& my $topid = File::Temp\->top_system_uid;
882 .Ve
883 .Sp
884 This value can be adjusted to reduce security checking if required.
885 The value is only relevant when \f(CW\*(C`safe_level\*(C'\fR is set to MEDIUM or higher.
886 .Sp
887 Available since 0.05.
888 .ie n .IP \fR\fB$KEEP_ALL\fR\fB\fR 4
889 .el .IP \fR\f(CB$KEEP_ALL\fR\fB\fR 4
890 .IX Item "$KEEP_ALL"
891 Controls whether temporary files and directories should be retained
892 regardless of any instructions in the program to remove them
893 automatically. This is useful for debugging but should not be used in
894 production code.
895 .Sp
896 .Vb 1
897 \& $File::Temp::KEEP_ALL = 1;
898 .Ve
899 .Sp
900 Default is for files to be removed as requested by the caller.
901 .Sp
902 In some cases, files will only be retained if this variable is true
903 when the file is created. This means that you can not create a temporary
904 file, set this variable and expect the temp file to still be around
905 when the program exits.
906 .ie n .IP \fR\fB$DEBUG\fR\fB\fR 4
907 .el .IP \fR\f(CB$DEBUG\fR\fB\fR 4
908 .IX Item "$DEBUG"
909 Controls whether debugging messages should be enabled.
910 .Sp
911 .Vb 1
912 \& $File::Temp::DEBUG = 1;
913 .Ve
914 .Sp
915 Default is for debugging mode to be disabled.
916 .Sp
917 Available since 0.15.
918 .SH WARNING
919 .IX Header "WARNING"
920 For maximum security, endeavour always to avoid ever looking at,
921 touching, or even imputing the existence of the filename. You do not
922 know that that filename is connected to the same file as the handle
923 you have, and attempts to check this can only trigger more race
924 conditions. It\*(Aqs far more secure to use the filehandle alone and
925 dispense with the filename altogether.
926 .PP
927 If you need to pass the handle to something that expects a filename
928 then on a unix system you can use \f(CW\*(C`"/dev/fd/" . fileno($fh)\*(C'\fR for
929 arbitrary programs. Perl code that uses the 2\-argument version of
930 \&\f(CW\*(C`open\*(C'\fR can be passed \f(CW\*(C`"+<=&" . fileno($fh)\*(C'\fR. Otherwise you
931 will need to pass the filename. You will have to clear the
932 close\-on\-exec bit on that file descriptor before passing it to another
933 process.
934 .PP
935 .Vb 3
936 \& use Fcntl qw/F_SETFD F_GETFD/;
937 \& fcntl($tmpfh, F_SETFD, 0)
938 \& or die "Can\*(Aqt clear close\-on\-exec flag on temp fh: $!\en";
939 .Ve
940 .SS "Temporary files and NFS"
941 .IX Subsection "Temporary files and NFS"
942 Some problems are associated with using temporary files that reside
943 on NFS file systems and it is recommended that a local filesystem
944 is used whenever possible. Some of the security tests will most probably
945 fail when the temp file is not local. Additionally, be aware that
946 the performance of I/O operations over NFS will not be as good as for
947 a local disk.
948 .SS Forking
949 .IX Subsection "Forking"
950 In some cases files created by File::Temp are removed from within an
951 END block. Since END blocks are triggered when a child process exits
952 (unless \f(CWPOSIX::_exit()\fR is used by the child) File::Temp takes care
953 to only remove those temp files created by a particular process ID. This
954 means that a child will not attempt to remove temp files created by the
955 parent process.
956 .PP
957 If you are forking many processes in parallel that are all creating
958 temporary files, you may need to reset the random number seed using
959 srand(EXPR) in each child else all the children will attempt to walk
960 through the same set of random file names and may well cause
961 themselves to give up if they exceed the number of retry attempts.
962 .SS "Directory removal"
963 .IX Subsection "Directory removal"
964 Note that if you have chdir\*(Aqed into the temporary directory and it is
965 subsequently cleaned up (either in the END block or as part of object
966 destruction), then you will get a warning from \fBFile::Path::rmtree()\fR.
967 .SS "Taint mode"
968 .IX Subsection "Taint mode"
969 If you need to run code under taint mode, updating to the latest
970 File::Spec is highly recommended. On Windows, if the directory
971 given by File::Spec::tmpdir isn\*(Aqt writable, File::Temp will attempt
972 to fallback to the user\*(Aqs local application data directory or croak
973 with an error.
974 .SS BINMODE
975 .IX Subsection "BINMODE"
976 The file returned by File::Temp will have been opened in binary mode
977 if such a mode is available. If that is not correct, use the \f(CWbinmode()\fR
978 function to change the mode of the filehandle.
979 .PP
980 Note that you can modify the encoding of a file opened by File::Temp
981 also by using \f(CWbinmode()\fR.
982 .SH HISTORY
983 .IX Header "HISTORY"
984 Originally began life in May 1999 as an XS interface to the system
985 \&\fBmkstemp()\fR function. In March 2000, the OpenBSD \fBmkstemp()\fR code was
986 translated to Perl for total control of the code\*(Aqs
987 security checking, to ensure the presence of the function regardless of
988 operating system and to help with portability. The module was shipped
989 as a standard part of perl from v5.6.1.
990 .PP
991 Thanks to Tom Christiansen for suggesting that this module
992 should be written and providing ideas for code improvements and
993 security enhancements.
994 .SH "SEE ALSO"
995 .IX Header "SEE ALSO"
996 "tmpnam" in POSIX, "tmpfile" in POSIX, File::Spec, File::Path
997 .PP
998 See IO::File and File::MkTemp, Apache::TempFile for
999 different implementations of temporary file handling.
1000 .PP
1001 See File::Tempdir for an alternative object\-oriented wrapper for
1002 the \f(CW\*(C`tempdir\*(C'\fR function.
1003 .SH SUPPORT
1004 .IX Header "SUPPORT"
1005 Bugs may be submitted through the RT bug tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=File-Temp>
1006 (or bug\-File\[email protected] <mailto:[email protected]>).
1007 .PP
1008 There is also a mailing list available for users of this distribution, at
1009 <http://lists.perl.org/list/cpan\-workers.html>.
1010 .PP
1011 There is also an irc channel available for users of this distribution, at
1012 \&\f(CW\*(C`#toolchain\*(C'\fR on \f(CW\*(C`irc.perl.org\*(C'\fR <irc://irc.perl.org/#toolchain>.
1013 .SH AUTHOR
1014 .IX Header "AUTHOR"
1015 Tim Jenness <[email protected]>
1016 .SH CONTRIBUTORS
1017 .IX Header "CONTRIBUTORS"
1018 .IP \(bu 4
1019 Tim Jenness <[email protected]>
1020 .IP \(bu 4
1021 Karen Etheridge <[email protected]>
1022 .IP \(bu 4
1023 David Golden <[email protected]>
1024 .IP \(bu 4
1025 Slaven Rezic <[email protected]>
1026 .IP \(bu 4
1027 mohawk2 <[email protected]>
1028 .IP \(bu 4
1029 Roy Ivy III <[email protected]>
1030 .IP \(bu 4
1031 Peter Rabbitson <[email protected]>
1032 .IP \(bu 4
1033 Olivier MenguĂ© <[email protected]>
1034 .IP \(bu 4
1035 Peter John Acklam <[email protected]>
1036 .IP \(bu 4
1037 Tim Gim Yee <[email protected]>
1038 .IP \(bu 4
1039 Nicolas R <[email protected]>
1040 .IP \(bu 4
1041 Brian Mowrey <[email protected]>
1042 .IP \(bu 4
1043 Dagfinn Ilmari MannsĂ„ker <[email protected]>
1044 .IP \(bu 4
1045 David Steinbrunner <[email protected]>
1046 .IP \(bu 4
1047 Ed Avis <[email protected]>
1048 .IP \(bu 4
1049 Guillem Jover <[email protected]>
1050 .IP \(bu 4
1051 James E. Keenan <[email protected]>
1052 .IP \(bu 4
1053 Kevin Ryde <[email protected]>
1054 .IP \(bu 4
1055 Ben Tilly <[email protected]>
1056 .SH "COPYRIGHT AND LICENSE"
1057 .IX Header "COPYRIGHT AND LICENSE"
1058 This software is copyright (c) 2020 by Tim Jenness and the UK Particle Physics and Astronomy Research Council.
1059 .PP
1060 This is free software; you can redistribute it and/or modify it under
1061 the same terms as the Perl 5 programming language system itself.