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