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.2309
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 and SUFFIX. Additionally, the
133 filename template is specified using the TEMPLATE option. The OPEN
134 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
216 the 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 Options can be combined as required.
311
312 Will croak() if there is an error.
313
314 Available since 0.05.
315
316 UNLINK flag available since 0.10.
317
318 TMPDIR flag available since 0.19.
319
320 EXLOCK flag available since 0.19.
321
322 tempdir
323 This is the recommended interface for creation of temporary
324 directories. By default the directory will not be removed on exit
325 (that is, it won't be temporary; this behaviour can not be changed
326 because of issues with backwards compatibility). To enable removal
327 either use the CLEANUP option which will trigger removal on program
328 exit, or consider using the "newdir" method in the object interface
329 which will allow the directory to be cleaned up when the object
330 goes out of scope.
331
332 The behaviour of the function depends on the arguments:
333
334 $tempdir = tempdir();
335
336 Create a directory in tmpdir() (see File::Spec).
337
338 $tempdir = tempdir( $template );
339
340 Create a directory from the supplied template. This template is
341 similar to that described for tempfile(). `X' characters at the end
342 of the template are replaced with random letters to construct the
343 directory name. At least four `X' characters must be in the
344 template.
345
346 $tempdir = tempdir ( DIR => $dir );
347
348 Specifies the directory to use for the temporary directory. The
349 temporary directory name is derived from an internal template.
350
351 $tempdir = tempdir ( $template, DIR => $dir );
352
353 Prepend the supplied directory name to the template. The template
354 should not include parent directory specifications itself. Any
355 parent directory specifications are removed from the template
356 before prepending the supplied directory.
357
358 $tempdir = tempdir ( $template, TMPDIR => 1 );
359
360 Using the supplied template, create the temporary directory in a
361 standard location for temporary files. Equivalent to doing
362
363 $tempdir = tempdir ( $template, DIR => File::Spec->tmpdir);
364
365 but shorter. Parent directory specifications are stripped from the
366 template itself. The "TMPDIR" option is ignored if "DIR" is set
367 explicitly. Additionally, "TMPDIR" is implied if neither a
368 template nor a directory are supplied.
369
370 $tempdir = tempdir( $template, CLEANUP => 1);
371
372 Create a temporary directory using the supplied template, but
373 attempt to remove it (and all files inside it) when the program
374 exits. Note that an attempt will be made to remove all files from
375 the directory even if they were not created by this module
376 (otherwise why ask to clean it up?). The directory removal is made
377 with the rmtree() function from the File::Path module. Of course,
378 if the template is not specified, the temporary directory will be
379 created in tmpdir() and will also be removed at program exit.
380
381 Will croak() if there is an error.
382
383 Current API available since 0.05.
384
386 The following functions are Perl implementations of the mktemp() family
387 of temp file generation system calls.
388
389 mkstemp
390 Given a template, returns a filehandle to the temporary file and
391 the name of the file.
392
393 ($fh, $name) = mkstemp( $template );
394
395 In scalar context, just the filehandle is returned.
396
397 The template may be any filename with some number of X's appended
398 to it, for example /tmp/temp.XXXX. The trailing X's are replaced
399 with unique alphanumeric combinations.
400
401 Will croak() if there is an error.
402
403 Current API available since 0.05.
404
405 mkstemps
406 Similar to mkstemp(), except that an extra argument can be supplied
407 with a suffix to be appended to the template.
408
409 ($fh, $name) = mkstemps( $template, $suffix );
410
411 For example a template of "testXXXXXX" and suffix of ".dat" would
412 generate a file similar to testhGji_w.dat.
413
414 Returns just the filehandle alone when called in scalar context.
415
416 Will croak() if there is an error.
417
418 Current API available since 0.05.
419
420 mkdtemp
421 Create a directory from a template. The template must end in X's
422 that are replaced by the routine.
423
424 $tmpdir_name = mkdtemp($template);
425
426 Returns the name of the temporary directory created.
427
428 Directory must be removed by the caller.
429
430 Will croak() if there is an error.
431
432 Current API available since 0.05.
433
434 mktemp
435 Returns a valid temporary filename but does not guarantee that the
436 file will not be opened by someone else.
437
438 $unopened_file = mktemp($template);
439
440 Template is the same as that required by mkstemp().
441
442 Will croak() if there is an error.
443
444 Current API available since 0.05.
445
447 This section describes the re-implementation of the tmpnam() and
448 tmpfile() functions described in POSIX using the mkstemp() from this
449 module.
450
451 Unlike the POSIX implementations, the directory used for the temporary
452 file is not specified in a system include file ("P_tmpdir") but simply
453 depends on the choice of tmpdir() returned by File::Spec. On some
454 implementations this location can be set using the "TMPDIR" environment
455 variable, which may not be secure. If this is a problem, simply use
456 mkstemp() and specify a template.
457
458 tmpnam
459 When called in scalar context, returns the full name (including
460 path) of a temporary file (uses mktemp()). The only check is that
461 the file does not already exist, but there is no guarantee that
462 that condition will continue to apply.
463
464 $file = tmpnam();
465
466 When called in list context, a filehandle to the open file and a
467 filename are returned. This is achieved by calling mkstemp() after
468 constructing a suitable template.
469
470 ($fh, $file) = tmpnam();
471
472 If possible, this form should be used to prevent possible race
473 conditions.
474
475 See "tmpdir" in File::Spec for information on the choice of
476 temporary directory for a particular operating system.
477
478 Will croak() if there is an error.
479
480 Current API available since 0.05.
481
482 tmpfile
483 Returns the filehandle of a temporary file.
484
485 $fh = tmpfile();
486
487 The file is removed when the filehandle is closed or when the
488 program exits. No access to the filename is provided.
489
490 If the temporary file can not be created undef is returned.
491 Currently this command will probably not work when the temporary
492 directory is on an NFS file system.
493
494 Will croak() if there is an error.
495
496 Available since 0.05.
497
498 Returning undef if unable to create file added in 0.12.
499
501 These functions are provided for backwards compatibility with common
502 tempfile generation C library functions.
503
504 They are not exported and must be addressed using the full package
505 name.
506
507 tempnam
508 Return the name of a temporary file in the specified directory
509 using a prefix. The file is guaranteed not to exist at the time the
510 function was called, but such guarantees are good for one clock
511 tick only. Always use the proper form of "sysopen" with "O_CREAT |
512 O_EXCL" if you must open such a filename.
513
514 $filename = File::Temp::tempnam( $dir, $prefix );
515
516 Equivalent to running mktemp() with $dir/$prefixXXXXXXXX (using
517 unix file convention as an example)
518
519 Because this function uses mktemp(), it can suffer from race
520 conditions.
521
522 Will croak() if there is an error.
523
524 Current API available since 0.05.
525
527 Useful functions for dealing with the filehandle and filename.
528
529 unlink0
530 Given an open filehandle and the associated filename, make a safe
531 unlink. This is achieved by first checking that the filename and
532 filehandle initially point to the same file and that the number of
533 links to the file is 1 (all fields returned by stat() are
534 compared). Then the filename is unlinked and the filehandle
535 checked once again to verify that the number of links on that file
536 is now 0. This is the closest you can come to making sure that the
537 filename unlinked was the same as the file whose descriptor you
538 hold.
539
540 unlink0($fh, $path)
541 or die "Error unlinking file $path safely";
542
543 Returns false on error but croaks() if there is a security anomaly.
544 The filehandle is not closed since on some occasions this is not
545 required.
546
547 On some platforms, for example Windows NT, it is not possible to
548 unlink an open file (the file must be closed first). On those
549 platforms, the actual unlinking is deferred until the program ends
550 and good status is returned. A check is still performed to make
551 sure that the filehandle and filename are pointing to the same
552 thing (but not at the time the end block is executed since the
553 deferred removal may not have access to the filehandle).
554
555 Additionally, on Windows NT not all the fields returned by stat()
556 can be compared. For example, the "dev" and "rdev" fields seem to
557 be different. Also, it seems that the size of the file returned by
558 stat() does not always agree, with "stat(FH)" being more accurate
559 than "stat(filename)", presumably because of caching issues even
560 when using autoflush (this is usually overcome by waiting a while
561 after writing to the tempfile before attempting to "unlink0" it).
562
563 Finally, on NFS file systems the link count of the file handle does
564 not always go to zero immediately after unlinking. Currently, this
565 command is expected to fail on NFS disks.
566
567 This function is disabled if the global variable $KEEP_ALL is true
568 and an unlink on open file is supported. If the unlink is to be
569 deferred to the END block, the file is still registered for
570 removal.
571
572 This function should not be called if you are using the object
573 oriented interface since the it will interfere with the object
574 destructor deleting the file.
575
576 Available Since 0.05.
577
578 If can not unlink open file, defer removal until later available
579 since 0.06.
580
581 cmpstat
582 Compare "stat" of filehandle with "stat" of provided filename.
583 This can be used to check that the filename and filehandle
584 initially point to the same file and that the number of links to
585 the file is 1 (all fields returned by stat() are compared).
586
587 cmpstat($fh, $path)
588 or die "Error comparing handle with file";
589
590 Returns false if the stat information differs or if the link count
591 is greater than 1. Calls croak if there is a security anomaly.
592
593 On certain platforms, for example Windows, not all the fields
594 returned by stat() can be compared. For example, the "dev" and
595 "rdev" fields seem to be different in Windows. Also, it seems that
596 the size of the file returned by stat() does not always agree, with
597 "stat(FH)" being more accurate than "stat(filename)", presumably
598 because of caching issues even when using autoflush (this is
599 usually overcome by waiting a while after writing to the tempfile
600 before attempting to "unlink0" it).
601
602 Not exported by default.
603
604 Current API available since 0.14.
605
606 unlink1
607 Similar to "unlink0" except after file comparison using cmpstat,
608 the filehandle is closed prior to attempting to unlink the file.
609 This allows the file to be removed without using an END block, but
610 does mean that the post-unlink comparison of the filehandle state
611 provided by "unlink0" is not available.
612
613 unlink1($fh, $path)
614 or die "Error closing and unlinking file";
615
616 Usually called from the object destructor when using the OO
617 interface.
618
619 Not exported by default.
620
621 This function is disabled if the global variable $KEEP_ALL is true.
622
623 Can call croak() if there is a security anomaly during the stat()
624 comparison.
625
626 Current API available since 0.14.
627
628 cleanup
629 Calling this function will cause any temp files or temp directories
630 that are registered for removal to be removed. This happens
631 automatically when the process exits but can be triggered manually
632 if the caller is sure that none of the temp files are required.
633 This method can be registered as an Apache callback.
634
635 Note that if a temp directory is your current directory, it cannot
636 be removed. "chdir()" out of the directory first before calling
637 "cleanup()". (For the cleanup at program exit when the CLEANUP flag
638 is set, this happens automatically.)
639
640 On OSes where temp files are automatically removed when the temp
641 file is closed, calling this function will have no effect other
642 than to remove temporary directories (which may include temporary
643 files).
644
645 File::Temp::cleanup();
646
647 Not exported by default.
648
649 Current API available since 0.15.
650
652 These functions control the global state of the package.
653
654 safe_level
655 Controls the lengths to which the module will go to check the
656 safety of the temporary file or directory before proceeding.
657 Options are:
658
659 STANDARD
660 Do the basic security measures to ensure the directory
661 exists and is writable, that temporary files are opened
662 only if they do not already exist, and that possible race
663 conditions are avoided. Finally the unlink0 function is
664 used to remove files safely.
665
666 MEDIUM In addition to the STANDARD security, the output directory
667 is checked to make sure that it is owned either by root or
668 the user running the program. If the directory is writable
669 by group or by other, it is then checked to make sure that
670 the sticky bit is set.
671
672 Will not work on platforms that do not support the "-k"
673 test for sticky bit.
674
675 HIGH In addition to the MEDIUM security checks, also check for
676 the possibility of ``chown() giveaway'' using the POSIX
677 sysconf() function. If this is a possibility, each
678 directory in the path is checked in turn for safeness,
679 recursively walking back to the root directory.
680
681 For platforms that do not support the POSIX
682 "_PC_CHOWN_RESTRICTED" symbol (for example, Windows NT) it
683 is assumed that ``chown() giveaway'' is possible and the
684 recursive test is performed.
685
686 The level can be changed as follows:
687
688 File::Temp->safe_level( File::Temp::HIGH );
689
690 The level constants are not exported by the module.
691
692 Currently, you must be running at least perl v5.6.0 in order to run
693 with MEDIUM or HIGH security. This is simply because the safety
694 tests use functions from Fcntl that are not available in older
695 versions of perl. The problem is that the version number for Fcntl
696 is the same in perl 5.6.0 and in 5.005_03 even though they are
697 different versions.
698
699 On systems that do not support the HIGH or MEDIUM safety levels
700 (for example Win NT or OS/2) any attempt to change the level will
701 be ignored. The decision to ignore rather than raise an exception
702 allows portable programs to be written with high security in mind
703 for the systems that can support this without those programs
704 failing on systems where the extra tests are irrelevant.
705
706 If you really need to see whether the change has been accepted
707 simply examine the return value of "safe_level".
708
709 $newlevel = File::Temp->safe_level( File::Temp::HIGH );
710 die "Could not change to high security"
711 if $newlevel != File::Temp::HIGH;
712
713 Available since 0.05.
714
715 TopSystemUID
716 This is the highest UID on the current system that refers to a root
717 UID. This is used to make sure that the temporary directory is
718 owned by a system UID ("root", "bin", "sys" etc) rather than simply
719 by root.
720
721 This is required since on many unix systems "/tmp" is not owned by
722 root.
723
724 Default is to assume that any UID less than or equal to 10 is a
725 root UID.
726
727 File::Temp->top_system_uid(10);
728 my $topid = File::Temp->top_system_uid;
729
730 This value can be adjusted to reduce security checking if required.
731 The value is only relevant when "safe_level" is set to MEDIUM or
732 higher.
733
734 Available since 0.05.
735
736 $KEEP_ALL
737 Controls whether temporary files and directories should be retained
738 regardless of any instructions in the program to remove them
739 automatically. This is useful for debugging but should not be used
740 in production code.
741
742 $File::Temp::KEEP_ALL = 1;
743
744 Default is for files to be removed as requested by the caller.
745
746 In some cases, files will only be retained if this variable is true
747 when the file is created. This means that you can not create a
748 temporary file, set this variable and expect the temp file to still
749 be around when the program exits.
750
751 $DEBUG
752 Controls whether debugging messages should be enabled.
753
754 $File::Temp::DEBUG = 1;
755
756 Default is for debugging mode to be disabled.
757
758 Available since 0.15.
759
761 For maximum security, endeavour always to avoid ever looking at,
762 touching, or even imputing the existence of the filename. You do not
763 know that that filename is connected to the same file as the handle you
764 have, and attempts to check this can only trigger more race conditions.
765 It's far more secure to use the filehandle alone and dispense with the
766 filename altogether.
767
768 If you need to pass the handle to something that expects a filename
769 then on a unix system you can use ""/dev/fd/" . fileno($fh)" for
770 arbitrary programs. Perl code that uses the 2-argument version of
771 "open" can be passed ""+<=&" . fileno($fh)". Otherwise you will need to
772 pass the filename. You will have to clear the close-on-exec bit on that
773 file descriptor before passing it to another process.
774
775 use Fcntl qw/F_SETFD F_GETFD/;
776 fcntl($tmpfh, F_SETFD, 0)
777 or die "Can't clear close-on-exec flag on temp fh: $!\n";
778
779 Temporary files and NFS
780 Some problems are associated with using temporary files that reside on
781 NFS file systems and it is recommended that a local filesystem is used
782 whenever possible. Some of the security tests will most probably fail
783 when the temp file is not local. Additionally, be aware that the
784 performance of I/O operations over NFS will not be as good as for a
785 local disk.
786
787 Forking
788 In some cases files created by File::Temp are removed from within an
789 END block. Since END blocks are triggered when a child process exits
790 (unless "POSIX::_exit()" is used by the child) File::Temp takes care to
791 only remove those temp files created by a particular process ID. This
792 means that a child will not attempt to remove temp files created by the
793 parent process.
794
795 If you are forking many processes in parallel that are all creating
796 temporary files, you may need to reset the random number seed using
797 srand(EXPR) in each child else all the children will attempt to walk
798 through the same set of random file names and may well cause themselves
799 to give up if they exceed the number of retry attempts.
800
801 Directory removal
802 Note that if you have chdir'ed into the temporary directory and it is
803 subsequently cleaned up (either in the END block or as part of object
804 destruction), then you will get a warning from File::Path::rmtree().
805
806 Taint mode
807 If you need to run code under taint mode, updating to the latest
808 File::Spec is highly recommended. On Windows, if the directory given
809 by File::Spec::tmpdir isn't writable, File::Temp will attempt to
810 fallback to the user's local application data directory or croak with
811 an error.
812
813 BINMODE
814 The file returned by File::Temp will have been opened in binary mode if
815 such a mode is available. If that is not correct, use the "binmode()"
816 function to change the mode of the filehandle.
817
818 Note that you can modify the encoding of a file opened by File::Temp
819 also by using "binmode()".
820
822 Originally began life in May 1999 as an XS interface to the system
823 mkstemp() function. In March 2000, the OpenBSD mkstemp() code was
824 translated to Perl for total control of the code's security checking,
825 to ensure the presence of the function regardless of operating system
826 and to help with portability. The module was shipped as a standard part
827 of perl from v5.6.1.
828
829 Thanks to Tom Christiansen for suggesting that this module should be
830 written and providing ideas for code improvements and security
831 enhancements.
832
834 "tmpnam" in POSIX, "tmpfile" in POSIX, File::Spec, File::Path
835
836 See IO::File and File::MkTemp, Apache::TempFile for different
837 implementations of temporary file handling.
838
839 See File::Tempdir for an alternative object-oriented wrapper for the
840 "tempdir" function.
841
843 Bugs may be submitted through the RT bug tracker
844 <https://rt.cpan.org/Public/Dist/Display.html?Name=File-Temp> (or
845 bug-File-Temp@rt.cpan.org <mailto:bug-File-Temp@rt.cpan.org>).
846
847 There is also a mailing list available for users of this distribution,
848 at <http://lists.perl.org/list/cpan-workers.html>.
849
850 There is also an irc channel available for users of this distribution,
851 at "#toolchain" on "irc.perl.org" <irc://irc.perl.org/#toolchain>.
852
854 Tim Jenness <tjenness@cpan.org>
855
857 · David Golden <dagolden@cpan.org>
858
859 · Karen Etheridge <ether@cpan.org>
860
861 · Slaven Rezic <slaven@rezic.de>
862
863 · Peter Rabbitson <ribasushi@cpan.org>
864
865 · Olivier Mengue <dolmen@cpan.org>
866
867 · David Golden <xdg@xdg.me>
868
869 · Kevin Ryde <user42@zip.com.au>
870
871 · Peter John Acklam <pjacklam@online.no>
872
873 · Slaven Rezic <slaven.rezic@idealo.de>
874
875 · James E. Keenan <jkeen@verizon.net>
876
877 · Brian Mowrey <brian@drlabs.org>
878
879 · Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
880
881 · David Steinbrunner <dsteinbrunner@pobox.com>
882
883 · Ed Avis <eda@linux01.wcl.local>
884
885 · Guillem Jover <guillem@hadrons.org>
886
887 · Ben Tilly <btilly@gmail.com>
888
890 This software is copyright (c) 2019 by Tim Jenness and the UK Particle
891 Physics and Astronomy Research Council.
892
893 This is free software; you can redistribute it and/or modify it under
894 the same terms as the Perl 5 programming language system itself.
895
896
897
898perl v5.30.1 2020-01-30 File::Temp(3)