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