Lines Matching full:pod

6 #pod =begin :__INTERNALS
7 #pod
8 #pod =head1 PORTABILITY
9 #pod
10 #pod This section is at the top in order to provide easier access to
11 #pod porters. It is not expected to be rendered by a standard pod
12 #pod formatting tool. Please skip straight to the SYNOPSIS section if you
13 #pod are not trying to port this module to a new platform.
14 #pod
15 #pod This module is designed to be portable across operating systems and it
16 #pod currently supports Unix, VMS, DOS, OS/2, Windows and Mac OS
17 #pod (Classic). When porting to a new OS there are generally three main
18 #pod issues that have to be solved:
19 #pod
20 #pod =over 4
21 #pod
22 #pod =item *
23 #pod
24 #pod Can the OS unlink an open file? If it can not then the
25 #pod C<_can_unlink_opened_file> method should be modified.
26 #pod
27 #pod =item *
28 #pod
29 #pod Are the return values from C<stat> reliable? By default all the
30 #pod return values from C<stat> are compared when unlinking a temporary
31 #pod file using the filename and the handle. Operating systems other than
32 #pod unix do not always have valid entries in all fields. If utility function
33 #pod C<File::Temp::unlink0> fails then the C<stat> comparison should be
34 #pod modified accordingly.
35 #pod
36 #pod =item *
37 #pod
38 #pod Security. Systems that can not support a test for the sticky bit
39 #pod on a directory can not use the MEDIUM and HIGH security tests.
40 #pod The C<_can_do_level> method should be modified accordingly.
41 #pod
42 #pod =back
43 #pod
44 #pod =end :__INTERNALS
45 #pod
46 #pod =head1 SYNOPSIS
47 #pod
48 #pod use File::Temp qw/ tempfile tempdir /;
49 #pod
50 #pod $fh = tempfile();
51 #pod ($fh, $filename) = tempfile();
52 #pod
53 #pod ($fh, $filename) = tempfile( $template, DIR => $dir);
54 #pod ($fh, $filename) = tempfile( $template, SUFFIX => '.dat');
55 #pod ($fh, $filename) = tempfile( $template, TMPDIR => 1 );
56 #pod
57 #pod binmode( $fh, ":utf8" );
58 #pod
59 #pod $dir = tempdir( CLEANUP => 1 );
60 #pod ($fh, $filename) = tempfile( DIR => $dir );
61 #pod
62 #pod Object interface:
63 #pod
64 #pod require File::Temp;
65 #pod use File::Temp ();
66 #pod use File::Temp qw/ :seekable /;
67 #pod
68 #pod $fh = File::Temp->new();
69 #pod $fname = $fh->filename;
70 #pod
71 #pod $fh = File::Temp->new(TEMPLATE => $template);
72 #pod $fname = $fh->filename;
73 #pod
74 #pod $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.dat' );
75 #pod print $tmp "Some data\n";
76 #pod print "Filename is $tmp\n";
77 #pod $tmp->seek( 0, SEEK_END );
78 #pod
79 #pod $dir = File::Temp->newdir(); # CLEANUP => 1 by default
80 #pod
81 #pod The following interfaces are provided for compatibility with
82 #pod existing APIs. They should not be used in new code.
83 #pod
84 #pod MkTemp family:
85 #pod
86 #pod use File::Temp qw/ :mktemp /;
87 #pod
88 #pod ($fh, $file) = mkstemp( "tmpfileXXXXX" );
89 #pod ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix);
90 #pod
91 #pod $tmpdir = mkdtemp( $template );
92 #pod
93 #pod $unopened_file = mktemp( $template );
94 #pod
95 #pod POSIX functions:
96 #pod
97 #pod use File::Temp qw/ :POSIX /;
98 #pod
99 #pod $file = tmpnam();
100 #pod $fh = tmpfile();
101 #pod
102 #pod ($fh, $file) = tmpnam();
103 #pod
104 #pod Compatibility functions:
105 #pod
106 #pod $unopened_file = File::Temp::tempnam( $dir, $pfx );
107 #pod
108 #pod =head1 DESCRIPTION
109 #pod
110 #pod C<File::Temp> can be used to create and open temporary files in a safe
111 #pod way. There is both a function interface and an object-oriented
112 #pod interface. The File::Temp constructor or the tempfile() function can
113 #pod be used to return the name and the open filehandle of a temporary
114 #pod file. The tempdir() function can be used to create a temporary
115 #pod directory.
116 #pod
117 #pod The security aspect of temporary file creation is emphasized such that
118 #pod a filehandle and filename are returned together. This helps guarantee
119 #pod that a race condition can not occur where the temporary file is
120 #pod created by another process between checking for the existence of the
121 #pod file and its opening. Additional security levels are provided to
122 #pod check, for example, that the sticky bit is set on world writable
123 #pod directories. See L<"safe_level"> for more information.
124 #pod
125 #pod For compatibility with popular C library functions, Perl implementations of
126 #pod the mkstemp() family of functions are provided. These are, mkstemp(),
127 #pod mkstemps(), mkdtemp() and mktemp().
128 #pod
129 #pod Additionally, implementations of the standard L<POSIX|POSIX>
130 #pod tmpnam() and tmpfile() functions are provided if required.
131 #pod
132 #pod Implementations of mktemp(), tmpnam(), and tempnam() are provided,
133 #pod but should be used with caution since they return only a filename
134 #pod that was valid when function was called, so cannot guarantee
135 #pod that the file will not exist by the time the caller opens the filename.
136 #pod
137 #pod Filehandles returned by these functions support the seekable methods.
138 #pod
139 #pod =cut
1020 #pod =head1 OBJECT-ORIENTED INTERFACE
1021 #pod
1022 #pod This is the primary interface for interacting with
1023 #pod C<File::Temp>. Using the OO interface a temporary file can be created
1024 #pod when the object is constructed and the file can be removed when the
1025 #pod object is no longer required.
1026 #pod
1027 #pod Note that there is no method to obtain the filehandle from the
1028 #pod C<File::Temp> object. The object itself acts as a filehandle. The object
1029 #pod isa C<IO::Handle> and isa C<IO::Seekable> so all those methods are
1030 #pod available.
1031 #pod
1032 #pod Also, the object is configured such that it stringifies to the name of the
1033 #pod temporary file and so can be compared to a filename directly. It numifies
1034 #pod to the C<refaddr> the same as other handles and so can be compared to other
1035 #pod handles with C<==>.
1036 #pod
1037 #pod $fh eq $filename # as a string
1038 #pod $fh != \*STDOUT # as a number
1039 #pod
1040 #pod Available since 0.14.
1041 #pod
1042 #pod =over 4
1043 #pod
1044 #pod =item B<new>
1045 #pod
1046 #pod Create a temporary file object.
1047 #pod
1048 #pod my $tmp = File::Temp->new();
1049 #pod
1050 #pod by default the object is constructed as if C<tempfile>
1051 #pod was called without options, but with the additional behaviour
1052 #pod that the temporary file is removed by the object destructor
1053 #pod if UNLINK is set to true (the default).
1054 #pod
1055 #pod Supported arguments are the same as for C<tempfile>: UNLINK
1056 #pod (defaulting to true), DIR, EXLOCK, PERMS and SUFFIX.
1057 #pod Additionally, the filename
1058 #pod template is specified using the TEMPLATE option. The OPEN option
1059 #pod is not supported (the file is always opened).
1060 #pod
1061 #pod $tmp = File::Temp->new( TEMPLATE => 'tempXXXXX',
1062 #pod DIR => 'mydir',
1063 #pod SUFFIX => '.dat');
1064 #pod
1065 #pod Arguments are case insensitive.
1066 #pod
1067 #pod Can call croak() if an error occurs.
1068 #pod
1069 #pod Available since 0.14.
1070 #pod
1071 #pod TEMPLATE available since 0.23
1072 #pod
1073 #pod =cut
1111 #pod =item B<newdir>
1112 #pod
1113 #pod Create a temporary directory using an object oriented interface.
1114 #pod
1115 #pod $dir = File::Temp->newdir();
1116 #pod
1117 #pod By default the directory is deleted when the object goes out of scope.
1118 #pod
1119 #pod Supports the same options as the C<tempdir> function. Note that directories
1120 #pod created with this method default to CLEANUP => 1.
1121 #pod
1122 #pod $dir = File::Temp->newdir( $template, %options );
1123 #pod
1124 #pod A template may be specified either with a leading template or
1125 #pod with a TEMPLATE argument.
1126 #pod
1127 #pod Available since 0.19.
1128 #pod
1129 #pod TEMPLATE available since 0.23.
1130 #pod
1131 #pod =cut
1156 #pod =item B<filename>
1157 #pod
1158 #pod Return the name of the temporary file associated with this object
1159 #pod (if the object was created using the "new" constructor).
1160 #pod
1161 #pod $filename = $tmp->filename;
1162 #pod
1163 #pod This method is called automatically when the object is used as
1164 #pod a string.
1165 #pod
1166 #pod Current API available since 0.14
1167 #pod
1168 #pod =cut
1187 #pod =item B<dirname>
1188 #pod
1189 #pod Return the name of the temporary directory associated with this
1190 #pod object (if the object was created using the "newdir" constructor).
1191 #pod
1192 #pod $dirname = $tmpdir->dirname;
1193 #pod
1194 #pod This method is called automatically when the object is used in string context.
1195 #pod
1196 #pod =item B<unlink_on_destroy>
1197 #pod
1198 #pod Control whether the file is unlinked when the object goes out of scope.
1199 #pod The file is removed if this value is true and $KEEP_ALL is not.
1200 #pod
1201 #pod $fh->unlink_on_destroy( 1 );
1202 #pod
1203 #pod Default is for the file to be removed.
1204 #pod
1205 #pod Current API available since 0.15
1206 #pod
1207 #pod =cut
1217 #pod =item B<DESTROY>
1218 #pod
1219 #pod When the object goes out of scope, the destructor is called. This
1220 #pod destructor will attempt to unlink the file (using L<unlink1|"unlink1">)
1221 #pod if the constructor was called with UNLINK set to 1 (the default state
1222 #pod if UNLINK is not specified).
1223 #pod
1224 #pod No error is given if the unlink fails.
1225 #pod
1226 #pod If the object has been passed to a child process during a fork, the
1227 #pod file will be deleted when the object goes out of scope in the parent.
1228 #pod
1229 #pod For a temporary directory object the directory will be removed unless
1230 #pod the CLEANUP argument was used in the constructor (and set to false) or
1231 #pod C<unlink_on_destroy> was modified after creation. Note that if a temp
1232 #pod directory is your current directory, it cannot be removed - a warning
1233 #pod will be given in this case. C<chdir()> out of the directory before
1234 #pod letting the object go out of scope.
1235 #pod
1236 #pod If the global variable $KEEP_ALL is true, the file or directory
1237 #pod will not be removed.
1238 #pod
1239 #pod =cut
1273 #pod =back
1274 #pod
1275 #pod =head1 FUNCTIONS
1276 #pod
1277 #pod This section describes the recommended interface for generating
1278 #pod temporary files and directories.
1279 #pod
1280 #pod =over 4
1281 #pod
1282 #pod =item B<tempfile>
1283 #pod
1284 #pod This is the basic function to generate temporary files.
1285 #pod The behaviour of the file can be changed using various options:
1286 #pod
1287 #pod $fh = tempfile();
1288 #pod ($fh, $filename) = tempfile();
1289 #pod
1290 #pod Create a temporary file in the directory specified for temporary
1291 #pod files, as specified by the tmpdir() function in L<File::Spec>.
1292 #pod
1293 #pod ($fh, $filename) = tempfile($template);
1294 #pod
1295 #pod Create a temporary file in the current directory using the supplied
1296 #pod template. Trailing `X' characters are replaced with random letters to
1297 #pod generate the filename. At least four `X' characters must be present
1298 #pod at the end of the template.
1299 #pod
1300 #pod ($fh, $filename) = tempfile($template, SUFFIX => $suffix)
1301 #pod
1302 #pod Same as previously, except that a suffix is added to the template
1303 #pod after the `X' translation. Useful for ensuring that a temporary
1304 #pod filename has a particular extension when needed by other applications.
1305 #pod But see the WARNING at the end.
1306 #pod
1307 #pod ($fh, $filename) = tempfile($template, DIR => $dir);
1308 #pod
1309 #pod Translates the template as before except that a directory name
1310 #pod is specified.
1311 #pod
1312 #pod ($fh, $filename) = tempfile($template, TMPDIR => 1);
1313 #pod
1314 #pod Equivalent to specifying a DIR of "File::Spec->tmpdir", writing the file
1315 #pod into the same temporary directory as would be used if no template was
1316 #pod specified at all.
1317 #pod
1318 #pod ($fh, $filename) = tempfile($template, UNLINK => 1);
1319 #pod
1320 #pod Return the filename and filehandle as before except that the file is
1321 #pod automatically removed when the program exits (dependent on
1322 #pod $KEEP_ALL). Default is for the file to be removed if a file handle is
1323 #pod requested and to be kept if the filename is requested. In a scalar
1324 #pod context (where no filename is returned) the file is always deleted
1325 #pod either (depending on the operating system) on exit or when it is
1326 #pod closed (unless $KEEP_ALL is true when the temp file is created).
1327 #pod
1328 #pod Use the object-oriented interface if fine-grained control of when
1329 #pod a file is removed is required.
1330 #pod
1331 #pod If the template is not specified, a template is always
1332 #pod automatically generated. This temporary file is placed in tmpdir()
1333 #pod (L<File::Spec>) unless a directory is specified explicitly with the
1334 #pod DIR option.
1335 #pod
1336 #pod $fh = tempfile( DIR => $dir );
1337 #pod
1338 #pod If called in scalar context, only the filehandle is returned and the
1339 #pod file will automatically be deleted when closed on operating systems
1340 #pod that support this (see the description of tmpfile() elsewhere in this
1341 #pod document). This is the preferred mode of operation, as if you only
1342 #pod have a filehandle, you can never create a race condition by fumbling
1343 #pod with the filename. On systems that can not unlink an open file or can
1344 #pod not mark a file as temporary when it is opened (for example, Windows
1345 #pod NT uses the C<O_TEMPORARY> flag) the file is marked for deletion when
1346 #pod the program ends (equivalent to setting UNLINK to 1). The C<UNLINK>
1347 #pod flag is ignored if present.
1348 #pod
1349 #pod (undef, $filename) = tempfile($template, OPEN => 0);
1350 #pod
1351 #pod This will return the filename based on the template but
1352 #pod will not open this file. Cannot be used in conjunction with
1353 #pod UNLINK set to true. Default is to always open the file
1354 #pod to protect from possible race conditions. A warning is issued
1355 #pod if warnings are turned on. Consider using the tmpnam()
1356 #pod and mktemp() functions described elsewhere in this document
1357 #pod if opening the file is not required.
1358 #pod
1359 #pod To open the temporary filehandle with O_EXLOCK (open with exclusive
1360 #pod file lock) use C<< EXLOCK=>1 >>. This is supported only by some
1361 #pod operating systems (most notably BSD derived systems). By default
1362 #pod EXLOCK will be false. Former C<File::Temp> versions set EXLOCK to
1363 #pod true, so to be sure to get an unlocked filehandle also with older
1364 #pod versions, explicitly set C<< EXLOCK=>0 >>.
1365 #pod
1366 #pod ($fh, $filename) = tempfile($template, EXLOCK => 1);
1367 #pod
1368 #pod By default, the temp file is created with 0600 file permissions.
1369 #pod Use C<PERMS> to change this:
1370 #pod
1371 #pod ($fh, $filename) = tempfile($template, PERMS => 0666);
1372 #pod
1373 #pod Options can be combined as required.
1374 #pod
1375 #pod Will croak() if there is an error.
1376 #pod
1377 #pod Available since 0.05.
1378 #pod
1379 #pod UNLINK flag available since 0.10.
1380 #pod
1381 #pod TMPDIR flag available since 0.19.
1382 #pod
1383 #pod EXLOCK flag available since 0.19.
1384 #pod
1385 #pod PERMS flag available since 0.2310.
1386 #pod
1387 #pod =cut
1572 #pod =item B<tempdir>
1573 #pod
1574 #pod This is the recommended interface for creation of temporary
1575 #pod directories. By default the directory will not be removed on exit
1576 #pod (that is, it won't be temporary; this behaviour can not be changed
1577 #pod because of issues with backwards compatibility). To enable removal
1578 #pod either use the CLEANUP option which will trigger removal on program
1579 #pod exit, or consider using the "newdir" method in the object interface which
1580 #pod will allow the directory to be cleaned up when the object goes out of
1581 #pod scope.
1582 #pod
1583 #pod The behaviour of the function depends on the arguments:
1584 #pod
1585 #pod $tempdir = tempdir();
1586 #pod
1587 #pod Create a directory in tmpdir() (see L<File::Spec|File::Spec>).
1588 #pod
1589 #pod $tempdir = tempdir( $template );
1590 #pod
1591 #pod Create a directory from the supplied template. This template is
1592 #pod similar to that described for tempfile(). `X' characters at the end
1593 #pod of the template are replaced with random letters to construct the
1594 #pod directory name. At least four `X' characters must be in the template.
1595 #pod
1596 #pod $tempdir = tempdir ( DIR => $dir );
1597 #pod
1598 #pod Specifies the directory to use for the temporary directory.
1599 #pod The temporary directory name is derived from an internal template.
1600 #pod
1601 #pod $tempdir = tempdir ( $template, DIR => $dir );
1602 #pod
1603 #pod Prepend the supplied directory name to the template. The template
1604 #pod should not include parent directory specifications itself. Any parent
1605 #pod directory specifications are removed from the template before
1606 #pod prepending the supplied directory.
1607 #pod
1608 #pod $tempdir = tempdir ( $template, TMPDIR => 1 );
1609 #pod
1610 #pod Using the supplied template, create the temporary directory in
1611 #pod a standard location for temporary files. Equivalent to doing
1612 #pod
1613 #pod $tempdir = tempdir ( $template, DIR => File::Spec->tmpdir);
1614 #pod
1615 #pod but shorter. Parent directory specifications are stripped from the
1616 #pod template itself. The C<TMPDIR> option is ignored if C<DIR> is set
1617 #pod explicitly. Additionally, C<TMPDIR> is implied if neither a template
1618 #pod nor a directory are supplied.
1619 #pod
1620 #pod $tempdir = tempdir( $template, CLEANUP => 1);
1621 #pod
1622 #pod Create a temporary directory using the supplied template, but
1623 #pod attempt to remove it (and all files inside it) when the program
1624 #pod exits. Note that an attempt will be made to remove all files from
1625 #pod the directory even if they were not created by this module (otherwise
1626 #pod why ask to clean it up?). The directory removal is made with
1627 #pod the rmtree() function from the L<File::Path|File::Path> module.
1628 #pod Of course, if the template is not specified, the temporary directory
1629 #pod will be created in tmpdir() and will also be removed at program exit.
1630 #pod
1631 #pod Will croak() if there is an error.
1632 #pod
1633 #pod Current API available since 0.05.
1634 #pod
1635 #pod =cut
1737 #pod =back
1738 #pod
1739 #pod =head1 MKTEMP FUNCTIONS
1740 #pod
1741 #pod The following functions are Perl implementations of the
1742 #pod mktemp() family of temp file generation system calls.
1743 #pod
1744 #pod =over 4
1745 #pod
1746 #pod =item B<mkstemp>
1747 #pod
1748 #pod Given a template, returns a filehandle to the temporary file and the name
1749 #pod of the file.
1750 #pod
1751 #pod ($fh, $name) = mkstemp( $template );
1752 #pod
1753 #pod In scalar context, just the filehandle is returned.
1754 #pod
1755 #pod The template may be any filename with some number of X's appended
1756 #pod to it, for example F</tmp/temp.XXXX>. The trailing X's are replaced
1757 #pod with unique alphanumeric combinations.
1758 #pod
1759 #pod Will croak() if there is an error.
1760 #pod
1761 #pod Current API available since 0.05.
1762 #pod
1763 #pod =cut
1792 #pod =item B<mkstemps>
1793 #pod
1794 #pod Similar to mkstemp(), except that an extra argument can be supplied
1795 #pod with a suffix to be appended to the template.
1796 #pod
1797 #pod ($fh, $name) = mkstemps( $template, $suffix );
1798 #pod
1799 #pod For example a template of C<testXXXXXX> and suffix of C<.dat>
1800 #pod would generate a file similar to F<testhGji_w.dat>.
1801 #pod
1802 #pod Returns just the filehandle alone when called in scalar context.
1803 #pod
1804 #pod Will croak() if there is an error.
1805 #pod
1806 #pod Current API available since 0.05.
1807 #pod
1808 #pod =cut
1838 #pod =item B<mkdtemp>
1839 #pod
1840 #pod Create a directory from a template. The template must end in
1841 #pod X's that are replaced by the routine.
1842 #pod
1843 #pod $tmpdir_name = mkdtemp($template);
1844 #pod
1845 #pod Returns the name of the temporary directory created.
1846 #pod
1847 #pod Directory must be removed by the caller.
1848 #pod
1849 #pod Will croak() if there is an error.
1850 #pod
1851 #pod Current API available since 0.05.
1852 #pod
1853 #pod =cut
1885 #pod =item B<mktemp>
1886 #pod
1887 #pod Returns a valid temporary filename but does not guarantee
1888 #pod that the file will not be opened by someone else.
1889 #pod
1890 #pod $unopened_file = mktemp($template);
1891 #pod
1892 #pod Template is the same as that required by mkstemp().
1893 #pod
1894 #pod Will croak() if there is an error.
1895 #pod
1896 #pod Current API available since 0.05.
1897 #pod
1898 #pod =cut
1919 #pod =back
1920 #pod
1921 #pod =head1 POSIX FUNCTIONS
1922 #pod
1923 #pod This section describes the re-implementation of the tmpnam()
1924 #pod and tmpfile() functions described in L<POSIX>
1925 #pod using the mkstemp() from this module.
1926 #pod
1927 #pod Unlike the L<POSIX|POSIX> implementations, the directory used
1928 #pod for the temporary file is not specified in a system include
1929 #pod file (C<P_tmpdir>) but simply depends on the choice of tmpdir()
1930 #pod returned by L<File::Spec|File::Spec>. On some implementations this
1931 #pod location can be set using the C<TMPDIR> environment variable, which
1932 #pod may not be secure.
1933 #pod If this is a problem, simply use mkstemp() and specify a template.
1934 #pod
1935 #pod =over 4
1936 #pod
1937 #pod =item B<tmpnam>
1938 #pod
1939 #pod When called in scalar context, returns the full name (including path)
1940 #pod of a temporary file (uses mktemp()). The only check is that the file does
1941 #pod not already exist, but there is no guarantee that that condition will
1942 #pod continue to apply.
1943 #pod
1944 #pod $file = tmpnam();
1945 #pod
1946 #pod When called in list context, a filehandle to the open file and
1947 #pod a filename are returned. This is achieved by calling mkstemp()
1948 #pod after constructing a suitable template.
1949 #pod
1950 #pod ($fh, $file) = tmpnam();
1951 #pod
1952 #pod If possible, this form should be used to prevent possible
1953 #pod race conditions.
1954 #pod
1955 #pod See L<File::Spec/tmpdir> for information on the choice of temporary
1956 #pod directory for a particular operating system.
1957 #pod
1958 #pod Will croak() if there is an error.
1959 #pod
1960 #pod Current API available since 0.05.
1961 #pod
1962 #pod =cut
1984 #pod =item B<tmpfile>
1985 #pod
1986 #pod Returns the filehandle of a temporary file.
1987 #pod
1988 #pod $fh = tmpfile();
1989 #pod
1990 #pod The file is removed when the filehandle is closed or when the program
1991 #pod exits. No access to the filename is provided.
1992 #pod
1993 #pod If the temporary file can not be created undef is returned.
1994 #pod Currently this command will probably not work when the temporary
1995 #pod directory is on an NFS file system.
1996 #pod
1997 #pod Will croak() if there is an error.
1998 #pod
1999 #pod Available since 0.05.
2000 #pod
2001 #pod Returning undef if unable to create file added in 0.12.
2002 #pod
2003 #pod =cut
2019 #pod =back
2020 #pod
2021 #pod =head1 ADDITIONAL FUNCTIONS
2022 #pod
2023 #pod These functions are provided for backwards compatibility
2024 #pod with common tempfile generation C library functions.
2025 #pod
2026 #pod They are not exported and must be addressed using the full package
2027 #pod name.
2028 #pod
2029 #pod =over 4
2030 #pod
2031 #pod =item B<tempnam>
2032 #pod
2033 #pod Return the name of a temporary file in the specified directory
2034 #pod using a prefix. The file is guaranteed not to exist at the time
2035 #pod the function was called, but such guarantees are good for one
2036 #pod clock tick only. Always use the proper form of C<sysopen>
2037 #pod with C<O_CREAT | O_EXCL> if you must open such a filename.
2038 #pod
2039 #pod $filename = File::Temp::tempnam( $dir, $prefix );
2040 #pod
2041 #pod Equivalent to running mktemp() with $dir/$prefixXXXXXXXX
2042 #pod (using unix file convention as an example)
2043 #pod
2044 #pod Because this function uses mktemp(), it can suffer from race conditions.
2045 #pod
2046 #pod Will croak() if there is an error.
2047 #pod
2048 #pod Current API available since 0.05.
2049 #pod
2050 #pod =cut
2068 #pod =back
2069 #pod
2070 #pod =head1 UTILITY FUNCTIONS
2071 #pod
2072 #pod Useful functions for dealing with the filehandle and filename.
2073 #pod
2074 #pod =over 4
2075 #pod
2076 #pod =item B<unlink0>
2077 #pod
2078 #pod Given an open filehandle and the associated filename, make a safe
2079 #pod unlink. This is achieved by first checking that the filename and
2080 #pod filehandle initially point to the same file and that the number of
2081 #pod links to the file is 1 (all fields returned by stat() are compared).
2082 #pod Then the filename is unlinked and the filehandle checked once again to
2083 #pod verify that the number of links on that file is now 0. This is the
2084 #pod closest you can come to making sure that the filename unlinked was the
2085 #pod same as the file whose descriptor you hold.
2086 #pod
2087 #pod unlink0($fh, $path)
2088 #pod or die "Error unlinking file $path safely";
2089 #pod
2090 #pod Returns false on error but croaks() if there is a security
2091 #pod anomaly. The filehandle is not closed since on some occasions this is
2092 #pod not required.
2093 #pod
2094 #pod On some platforms, for example Windows NT, it is not possible to
2095 #pod unlink an open file (the file must be closed first). On those
2096 #pod platforms, the actual unlinking is deferred until the program ends and
2097 #pod good status is returned. A check is still performed to make sure that
2098 #pod the filehandle and filename are pointing to the same thing (but not at
2099 #pod the time the end block is executed since the deferred removal may not
2100 #pod have access to the filehandle).
2101 #pod
2102 #pod Additionally, on Windows NT not all the fields returned by stat() can
2103 #pod be compared. For example, the C<dev> and C<rdev> fields seem to be
2104 #pod different. Also, it seems that the size of the file returned by stat()
2105 #pod does not always agree, with C<stat(FH)> being more accurate than
2106 #pod C<stat(filename)>, presumably because of caching issues even when
2107 #pod using autoflush (this is usually overcome by waiting a while after
2108 #pod writing to the tempfile before attempting to C<unlink0> it).
2109 #pod
2110 #pod Finally, on NFS file systems the link count of the file handle does
2111 #pod not always go to zero immediately after unlinking. Currently, this
2112 #pod command is expected to fail on NFS disks.
2113 #pod
2114 #pod This function is disabled if the global variable $KEEP_ALL is true
2115 #pod and an unlink on open file is supported. If the unlink is to be deferred
2116 #pod to the END block, the file is still registered for removal.
2117 #pod
2118 #pod This function should not be called if you are using the object oriented
2119 #pod interface since the it will interfere with the object destructor deleting
2120 #pod the file.
2121 #pod
2122 #pod Available Since 0.05.
2123 #pod
2124 #pod If can not unlink open file, defer removal until later available since 0.06.
2125 #pod
2126 #pod =cut
2167 #pod =item B<cmpstat>
2168 #pod
2169 #pod Compare C<stat> of filehandle with C<stat> of provided filename. This
2170 #pod can be used to check that the filename and filehandle initially point
2171 #pod to the same file and that the number of links to the file is 1 (all
2172 #pod fields returned by stat() are compared).
2173 #pod
2174 #pod cmpstat($fh, $path)
2175 #pod or die "Error comparing handle with file";
2176 #pod
2177 #pod Returns false if the stat information differs or if the link count is
2178 #pod greater than 1. Calls croak if there is a security anomaly.
2179 #pod
2180 #pod On certain platforms, for example Windows, not all the fields returned by stat()
2181 #pod can be compared. For example, the C<dev> and C<rdev> fields seem to be
2182 #pod different in Windows. Also, it seems that the size of the file
2183 #pod returned by stat() does not always agree, with C<stat(FH)> being more
2184 #pod accurate than C<stat(filename)>, presumably because of caching issues
2185 #pod even when using autoflush (this is usually overcome by waiting a while
2186 #pod after writing to the tempfile before attempting to C<unlink0> it).
2187 #pod
2188 #pod Not exported by default.
2189 #pod
2190 #pod Current API available since 0.14.
2191 #pod
2192 #pod =cut
2265 #pod =item B<unlink1>
2266 #pod
2267 #pod Similar to C<unlink0> except after file comparison using cmpstat, the
2268 #pod filehandle is closed prior to attempting to unlink the file. This
2269 #pod allows the file to be removed without using an END block, but does
2270 #pod mean that the post-unlink comparison of the filehandle state provided
2271 #pod by C<unlink0> is not available.
2272 #pod
2273 #pod unlink1($fh, $path)
2274 #pod or die "Error closing and unlinking file";
2275 #pod
2276 #pod Usually called from the object destructor when using the OO interface.
2277 #pod
2278 #pod Not exported by default.
2279 #pod
2280 #pod This function is disabled if the global variable $KEEP_ALL is true.
2281 #pod
2282 #pod Can call croak() if there is a security anomaly during the stat()
2283 #pod comparison.
2284 #pod
2285 #pod Current API available since 0.14.
2286 #pod
2287 #pod =cut
2311 #pod =item B<cleanup>
2312 #pod
2313 #pod Calling this function will cause any temp files or temp directories
2314 #pod that are registered for removal to be removed. This happens automatically
2315 #pod when the process exits but can be triggered manually if the caller is sure
2316 #pod that none of the temp files are required. This method can be registered as
2317 #pod an Apache callback.
2318 #pod
2319 #pod Note that if a temp directory is your current directory, it cannot be
2320 #pod removed. C<chdir()> out of the directory first before calling
2321 #pod C<cleanup()>. (For the cleanup at program exit when the CLEANUP flag
2322 #pod is set, this happens automatically.)
2323 #pod
2324 #pod On OSes where temp files are automatically removed when the temp file
2325 #pod is closed, calling this function will have no effect other than to remove
2326 #pod temporary directories (which may include temporary files).
2327 #pod
2328 #pod File::Temp::cleanup();
2329 #pod
2330 #pod Not exported by default.
2331 #pod
2332 #pod Current API available since 0.15.
2333 #pod
2334 #pod =back
2335 #pod
2336 #pod =head1 PACKAGE VARIABLES
2337 #pod
2338 #pod These functions control the global state of the package.
2339 #pod
2340 #pod =over 4
2341 #pod
2342 #pod =item B<safe_level>
2343 #pod
2344 #pod Controls the lengths to which the module will go to check the safety of the
2345 #pod temporary file or directory before proceeding.
2346 #pod Options are:
2347 #pod
2348 #pod =over 8
2349 #pod
2350 #pod =item STANDARD
2351 #pod
2352 #pod Do the basic security measures to ensure the directory exists and is
2353 #pod writable, that temporary files are opened only if they do not already
2354 #pod exist, and that possible race conditions are avoided. Finally the
2355 #pod L<unlink0|"unlink0"> function is used to remove files safely.
2356 #pod
2357 #pod =item MEDIUM
2358 #pod
2359 #pod In addition to the STANDARD security, the output directory is checked
2360 #pod to make sure that it is owned either by root or the user running the
2361 #pod program. If the directory is writable by group or by other, it is then
2362 #pod checked to make sure that the sticky bit is set.
2363 #pod
2364 #pod Will not work on platforms that do not support the C<-k> test
2365 #pod for sticky bit.
2366 #pod
2367 #pod =item HIGH
2368 #pod
2369 #pod In addition to the MEDIUM security checks, also check for the
2370 #pod possibility of ``chown() giveaway'' using the L<POSIX|POSIX>
2371 #pod sysconf() function. If this is a possibility, each directory in the
2372 #pod path is checked in turn for safeness, recursively walking back to the
2373 #pod root directory.
2374 #pod
2375 #pod For platforms that do not support the L<POSIX|POSIX>
2376 #pod C<_PC_CHOWN_RESTRICTED> symbol (for example, Windows NT) it is
2377 #pod assumed that ``chown() giveaway'' is possible and the recursive test
2378 #pod is performed.
2379 #pod
2380 #pod =back
2381 #pod
2382 #pod The level can be changed as follows:
2383 #pod
2384 #pod File::Temp->safe_level( File::Temp::HIGH );
2385 #pod
2386 #pod The level constants are not exported by the module.
2387 #pod
2388 #pod Currently, you must be running at least perl v5.6.0 in order to
2389 #pod run with MEDIUM or HIGH security. This is simply because the
2390 #pod safety tests use functions from L<Fcntl|Fcntl> that are not
2391 #pod available in older versions of perl. The problem is that the version
2392 #pod number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though
2393 #pod they are different versions.
2394 #pod
2395 #pod On systems that do not support the HIGH or MEDIUM safety levels
2396 #pod (for example Win NT or OS/2) any attempt to change the level will
2397 #pod be ignored. The decision to ignore rather than raise an exception
2398 #pod allows portable programs to be written with high security in mind
2399 #pod for the systems that can support this without those programs failing
2400 #pod on systems where the extra tests are irrelevant.
2401 #pod
2402 #pod If you really need to see whether the change has been accepted
2403 #pod simply examine the return value of C<safe_level>.
2404 #pod
2405 #pod $newlevel = File::Temp->safe_level( File::Temp::HIGH );
2406 #pod die "Could not change to high security"
2407 #pod if $newlevel != File::Temp::HIGH;
2408 #pod
2409 #pod Available since 0.05.
2410 #pod
2411 #pod =cut
2437 #pod =item TopSystemUID
2438 #pod
2439 #pod This is the highest UID on the current system that refers to a root
2440 #pod UID. This is used to make sure that the temporary directory is
2441 #pod owned by a system UID (C<root>, C<bin>, C<sys> etc) rather than
2442 #pod simply by root.
2443 #pod
2444 #pod This is required since on many unix systems C</tmp> is not owned
2445 #pod by root.
2446 #pod
2447 #pod Default is to assume that any UID less than or equal to 10 is a root
2448 #pod UID.
2449 #pod
2450 #pod File::Temp->top_system_uid(10);
2451 #pod my $topid = File::Temp->top_system_uid;
2452 #pod
2453 #pod This value can be adjusted to reduce security checking if required.
2454 #pod The value is only relevant when C<safe_level> is set to MEDIUM or higher.
2455 #pod
2456 #pod Available since 0.05.
2457 #pod
2458 #pod =cut
2475 #pod =item B<$KEEP_ALL>
2476 #pod
2477 #pod Controls whether temporary files and directories should be retained
2478 #pod regardless of any instructions in the program to remove them
2479 #pod automatically. This is useful for debugging but should not be used in
2480 #pod production code.
2481 #pod
2482 #pod $File::Temp::KEEP_ALL = 1;
2483 #pod
2484 #pod Default is for files to be removed as requested by the caller.
2485 #pod
2486 #pod In some cases, files will only be retained if this variable is true
2487 #pod when the file is created. This means that you can not create a temporary
2488 #pod file, set this variable and expect the temp file to still be around
2489 #pod when the program exits.
2490 #pod
2491 #pod =item B<$DEBUG>
2492 #pod
2493 #pod Controls whether debugging messages should be enabled.
2494 #pod
2495 #pod $File::Temp::DEBUG = 1;
2496 #pod
2497 #pod Default is for debugging mode to be disabled.
2498 #pod
2499 #pod Available since 0.15.
2500 #pod
2501 #pod =back
2502 #pod
2503 #pod =head1 WARNING
2504 #pod
2505 #pod For maximum security, endeavour always to avoid ever looking at,
2506 #pod touching, or even imputing the existence of the filename. You do not
2507 #pod know that that filename is connected to the same file as the handle
2508 #pod you have, and attempts to check this can only trigger more race
2509 #pod conditions. It's far more secure to use the filehandle alone and
2510 #pod dispense with the filename altogether.
2511 #pod
2512 #pod If you need to pass the handle to something that expects a filename
2513 #pod then on a unix system you can use C<"/dev/fd/" . fileno($fh)> for
2514 #pod arbitrary programs. Perl code that uses the 2-argument version of
2515 #pod C<< open >> can be passed C<< "+<=&" . fileno($fh) >>. Otherwise you
2516 #pod will need to pass the filename. You will have to clear the
2517 #pod close-on-exec bit on that file descriptor before passing it to another
2518 #pod process.
2519 #pod
2520 #pod use Fcntl qw/F_SETFD F_GETFD/;
2521 #pod fcntl($tmpfh, F_SETFD, 0)
2522 #pod or die "Can't clear close-on-exec flag on temp fh: $!\n";
2523 #pod
2524 #pod =head2 Temporary files and NFS
2525 #pod
2526 #pod Some problems are associated with using temporary files that reside
2527 #pod on NFS file systems and it is recommended that a local filesystem
2528 #pod is used whenever possible. Some of the security tests will most probably
2529 #pod fail when the temp file is not local. Additionally, be aware that
2530 #pod the performance of I/O operations over NFS will not be as good as for
2531 #pod a local disk.
2532 #pod
2533 #pod =head2 Forking
2534 #pod
2535 #pod In some cases files created by File::Temp are removed from within an
2536 #pod END block. Since END blocks are triggered when a child process exits
2537 #pod (unless C<POSIX::_exit()> is used by the child) File::Temp takes care
2538 #pod to only remove those temp files created by a particular process ID. This
2539 #pod means that a child will not attempt to remove temp files created by the
2540 #pod parent process.
2541 #pod
2542 #pod If you are forking many processes in parallel that are all creating
2543 #pod temporary files, you may need to reset the random number seed using
2544 #pod srand(EXPR) in each child else all the children will attempt to walk
2545 #pod through the same set of random file names and may well cause
2546 #pod themselves to give up if they exceed the number of retry attempts.
2547 #pod
2548 #pod =head2 Directory removal
2549 #pod
2550 #pod Note that if you have chdir'ed into the temporary directory and it is
2551 #pod subsequently cleaned up (either in the END block or as part of object
2552 #pod destruction), then you will get a warning from File::Path::rmtree().
2553 #pod
2554 #pod =head2 Taint mode
2555 #pod
2556 #pod If you need to run code under taint mode, updating to the latest
2557 #pod L<File::Spec> is highly recommended. On Windows, if the directory
2558 #pod given by L<File::Spec::tmpdir> isn't writable, File::Temp will attempt
2559 #pod to fallback to the user's local application data directory or croak
2560 #pod with an error.
2561 #pod
2562 #pod =head2 BINMODE
2563 #pod
2564 #pod The file returned by File::Temp will have been opened in binary mode
2565 #pod if such a mode is available. If that is not correct, use the C<binmode()>
2566 #pod function to change the mode of the filehandle.
2567 #pod
2568 #pod Note that you can modify the encoding of a file opened by File::Temp
2569 #pod also by using C<binmode()>.
2570 #pod
2571 #pod =head1 HISTORY
2572 #pod
2573 #pod Originally began life in May 1999 as an XS interface to the system
2574 #pod mkstemp() function. In March 2000, the OpenBSD mkstemp() code was
2575 #pod translated to Perl for total control of the code's
2576 #pod security checking, to ensure the presence of the function regardless of
2577 #pod operating system and to help with portability. The module was shipped
2578 #pod as a standard part of perl from v5.6.1.
2579 #pod
2580 #pod Thanks to Tom Christiansen for suggesting that this module
2581 #pod should be written and providing ideas for code improvements and
2582 #pod security enhancements.
2583 #pod
2584 #pod =head1 SEE ALSO
2585 #pod
2586 #pod L<POSIX/tmpnam>, L<POSIX/tmpfile>, L<File::Spec>, L<File::Path>
2587 #pod
2588 #pod See L<IO::File> and L<File::MkTemp>, L<Apache::TempFile> for
2589 #pod different implementations of temporary file handling.
2590 #pod
2591 #pod See L<File::Tempdir> for an alternative object-oriented wrapper for
2592 #pod the C<tempdir> function.
2593 #pod
2594 #pod =cut
2656 =pod
2766 porters. It is not expected to be rendered by a standard pod
3613 =for Pod::Coverage STRINGIFY NUMIFY top_system_uid