Document the changes with regards to running of END blocks.
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index e4ad3fa..fc7c563 100644 (file)
@@ -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;
@@ -466,11 +459,11 @@ whatever:
 
 =head2 How can I translate tildes (~) in a filename?
 
-Use the <> (glob()) operator, documented in L<perlfunc>.  This
-requires that you have a shell installed that groks tildes, meaning
-csh or tcsh or (some versions of) ksh, and thus your code may have portability
-problems.  The Glob::KGlob module (available from CPAN) gives more
-portable glob functionality.
+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
+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()?
@@ -723,6 +716,34 @@ Here's a much better web-page hit counter:
 
 If the count doesn't impress your friends, then the code might.  :-)
 
+=head2 All I want to do is append a small amount of text to the end of a file.  Do I still have to use locking?
+
+If you are on a system that correctly implements flock() and you use the
+example appending code from "perldoc -f flock" everything will be OK
+even if the OS you are on doesn't implement append mode correctly (if
+such a system exists.) So if you are happy to restrict yourself to OSs
+that implement flock() (and that's not really much of a restriction)
+then that is what you should do.
+
+If you know you are only going to use a system that does correctly
+implement appending (i.e. not Win32) then you can omit the seek() from
+the above code.
+
+If you know you are only writing code to run on an OS and filesystem that
+does implement append mode correctly (a local filesystem on a modern
+Unix for example), and you keep the file in block-buffered mode and you
+write less than one buffer-full of output between each manual flushing
+of the buffer then each bufferload is almost garanteed to be written to
+the end of the file in one chunk without getting intermingled with
+anyone else's output. You can also use the syswrite() function which is
+simply a wrapper around your systems write(2) system call.
+
+There is still a small theoretical chance that a signal will interrupt
+the system level write() operation before completion.  There is also a
+possibility that some STDIO implementations may call multiple system
+level write()s even if the buffer was empty to start.  There may be some
+systems where this probability is reduced to zero.
+
 =head2 How do I randomly update a binary file?
 
 If you're just trying to patch a binary, in many cases something as
@@ -955,52 +976,6 @@ include also support for non-portable systems as well.
     printf "\nYou said %s, char number %03d\n",
         $key, ord $key;
 
-For legacy DOS systems, Dan Carson <dbc@tc.fluke.COM> reports the following:
-
-To put the PC in "raw" mode, use ioctl with some magic numbers gleaned
-from msdos.c (Perl source file) and Ralf Brown's interrupt list (comes
-across the net every so often):
-
-    $old_ioctl = ioctl(STDIN,0,0);     # Gets device info
-    $old_ioctl &= 0xff;
-    ioctl(STDIN,1,$old_ioctl | 32);    # Writes it back, setting bit 5
-
-Then to read a single character:
-
-    sysread(STDIN,$c,1);               # Read a single character
-
-And to put the PC back to "cooked" mode:
-
-    ioctl(STDIN,1,$old_ioctl);         # Sets it back to cooked mode.
-
-So now you have $c.  If C<ord($c) == 0>, you have a two byte code, which
-means you hit a special key.  Read another byte with C<sysread(STDIN,$c,1)>,
-and that value tells you what combination it was according to this
-table:
-
-    # PC 2-byte keycodes = ^@ + the following:
-
-    # HEX     KEYS
-    # ---     ----
-    # 0F      SHF TAB
-    # 10-19   ALT QWERTYUIOP
-    # 1E-26   ALT ASDFGHJKL
-    # 2C-32   ALT ZXCVBNM
-    # 3B-44   F1-F10
-    # 47-49   HOME,UP,PgUp
-    # 4B      LEFT
-    # 4D      RIGHT
-    # 4F-53   END,DOWN,PgDn,Ins,Del
-    # 54-5D   SHF F1-F10
-    # 5E-67   CTR F1-F10
-    # 68-71   ALT F1-F10
-    # 73-77   CTR LEFT,RIGHT,END,PgDn,HOME
-    # 78-83   ALT 1234567890-=
-    # 84      CTR PgUp
-
-This is all trial and error I did a long time ago; I hope I'm reading the
-file that worked...
-
 =head2 How can I tell whether there's a character waiting on a filehandle?
 
 The very first thing you should do is look into getting the Term::ReadKey
@@ -1204,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