Integrate mainline + lib/open.t patch from Chromatic
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index dde1fea..bfd6d35 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq5 - Files and Formats ($Revision: 1.38 $, $Date: 1999/05/23 16:08:30 $)
+perlfaq5 - Files and Formats ($Revision: 1.2 $, $Date: 2001/09/26 10:44:41 $)
 
 =head1 DESCRIPTION
 
@@ -174,32 +174,25 @@ This assumes no funny games with newline translations.
 
 =head2 How do I make a temporary file name?
 
-Use the C<new_tmpfile> class method from the IO::File module to get a
-filehandle opened for reading and writing.  Use it if you don't
-need to know the file's name:
+Use the File::Temp module, see L<File::Temp> for more information.
 
-    use IO::File;
-    $fh = IO::File->new_tmpfile()
-       or die "Unable to make new temporary file: $!";
+  use File::Temp qw/ tempfile tempdir /; 
 
-If you do need to know the file's name, you can use the C<tmpnam>
-function from the POSIX module to get a filename that you then open
-yourself:
+  $dir = tempdir( CLEANUP => 1 );
+  ($fh, $filename) = tempfile( DIR => $dir );
 
+  # or if you don't need to know the filename
 
-    use Fcntl;
-    use POSIX qw(tmpnam);
-
-    # try new temporary filenames until we get one that didn't already
-    # exist;  the check should be unnecessary, but you can't be too careful
-    do { $name = tmpnam() }
-        until sysopen(FH, $name, O_RDWR|O_CREAT|O_EXCL);
+  $fh = tempfile( DIR => $dir );
 
-    # install atexit-style handler so that when we exit or die,
-    # we automatically delete this temporary file
-    END { unlink($name) or die "Couldn't unlink $name : $!" }
+The File::Temp has been a standard module since Perl 5.6.1.  If you
+don't have a modern enough Perl installed, use the C<new_tmpfile>
+class method from the IO::File module to get a filehandle opened for
+reading and writing.  Use it if you don't need to know the file's name:
 
-    # now go on to use the file ...
+    use IO::File;
+    $fh = IO::File->new_tmpfile()
+       or die "Unable to make new temporary file: $!";
 
 If you're committed to creating a temporary file by hand, use the
 process ID and/or the current time-value.  If you need to have many
@@ -207,7 +200,7 @@ temporary files in one process, use a counter:
 
     BEGIN {
        use Fcntl;
-       my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMP} || $ENV{TEMP};
+       my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMPDIR} || $ENV{TEMP};
        my $base_name = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
        sub temp_file {
            local *FH;
@@ -435,9 +428,9 @@ See L<perlform/"Accessing Formatting Internals"> for an swrite() function.
 This one will do it for you:
 
     sub commify {
-       local $_  = shift;
-       1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
-       return $_;
+        my $number = shift;
+       1 while ($number =~ s/^([-+]?\d+)(\d{3})/$1,$2/);
+       return $number;
     }
 
     $n = 23659019423.2331;
@@ -469,7 +462,7 @@ whatever:
 Use the <> (glob()) operator, documented in L<perlfunc>.  Older
 versions of Perl require that you have a shell installed that groks
 tildes.  Recent perl versions have this feature built in. The
-Glob::KGlob module (available from CPAN) gives more portable glob
+File::KGlob module (available from CPAN) gives more portable glob
 functionality.
 
 Within Perl, you may use this directly:
@@ -569,7 +562,7 @@ C<Argument list too long>.  People who installed tcsh as csh won't
 have this problem, but their users may be surprised by it.
 
 To get around this, either upgrade to Perl v5.6.0 or later, do the glob
-yourself with readdir() and patterns, or use a module like Glob::KGlob,
+yourself with readdir() and patterns, or use a module like File::KGlob,
 one that doesn't use the shell to do globbing.
 
 =head2 Is there a leak/bug in glob()?
@@ -1186,10 +1179,8 @@ If your array contains lines, just print them:
 Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington.
 All rights reserved.
 
-When included as an integrated part of the Standard Distribution
-of Perl or of its documentation (printed or otherwise), this works is
-covered under Perl's Artistic License.  For separate distributions of
-all or part of this FAQ outside of that, see L<perlfaq>.
+This documentation is free; you can redistribute it and/or modify it
+under the same terms as Perl itself.
 
 Irrespective of its distribution, all code examples here are in the public
 domain.  You are permitted and encouraged to use this code and any