Make surrogates illegal also on EBCDIC.
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index 7491baa..28888f6 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.4 $, $Date: 2001/11/09 08:06:04 $)
 
 =head1 DESCRIPTION
 
@@ -67,7 +67,7 @@ or even this:
     $document = join('', <$sock>);
     print "DOC IS: $document\n";
 
-Note the bizarrely hardcoded carriage return and newline in their octal
+Note the bizarrely hard coded carriage return and newline in their octal
 equivalents.  This is the ONLY way (currently) to assure a proper flush
 on all platforms, including Macintosh.  That's the way things work in
 network programming: you really should specify the exact bit pattern
@@ -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;
@@ -249,7 +242,7 @@ Berkeley-style ps:
 
 We've used C<$$var> in a way that forbidden by C<use strict 'refs'>.
 That is, we've promoted a string to a scalar variable reference using
-symbolic references.  This is ok in small programs, but doesn't scale
+symbolic references.  This is okay in small programs, but doesn't scale
 well.   It also only works on global variables, not lexicals.
 
 =head2 How can I make a filehandle local to a subroutine?  How do I pass filehandles between subroutines?  How do I make an array of filehandles?
@@ -383,7 +376,7 @@ In the examples above, we assigned the filehandle to a scalar variable
 before using it.  That is because only simple scalar variables, not
 expressions or subscripts of hashes or arrays, can be used with
 built-ins like C<print>, C<printf>, or the diamond operator.  Using
-something other than a simple scalar varaible as a filehandle is
+something other than a simple scalar variable as a filehandle is
 illegal and won't even compile:
 
     @fd = (*STDIN, *STDOUT, *STDERR);
@@ -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()?
@@ -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 guaranteed 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
@@ -750,7 +771,7 @@ Don't forget them or you'll be quite sorry.
 
 If you want to retrieve the time at which the file was last read,
 written, or had its meta-data (owner, etc) changed, you use the B<-M>,
-B<-A>, or B<-C> filetest operations as documented in L<perlfunc>.  These
+B<-A>, or B<-C> file test operations as documented in L<perlfunc>.  These
 retrieve the age of the file (measured against the start-time of your
 program) in days as a floating point number.  To retrieve the "raw"
 time in seconds since the epoch, you would call the stat function,
@@ -817,7 +838,7 @@ Or even:
 
 Otherwise you'll have to write your own multiplexing print
 function--or your own tee program--or use Tom Christiansen's,
-at http://www.perl.com/CPAN/authors/id/TOMC/scripts/tct.gz , which is
+at http://www.cpan.org/authors/id/TOMC/scripts/tct.gz , which is
 written in Perl and offers much greater functionality
 than the stock version.
 
@@ -1003,7 +1024,7 @@ Or write a small C program using the editor of champions:
     % ./fionread
     0x4004667f
 
-And then hard-code it, leaving porting as an exercise to your successor.
+And then hard code it, leaving porting as an exercise to your successor.
 
     $FIONREAD = 0x4004667f;         # XXX: opsys dependent
 
@@ -1109,7 +1130,7 @@ documentation for details.
 
 This is elaborately and painstakingly described in the "Far More Than
 You Ever Wanted To Know" in
-http://www.perl.com/CPAN/doc/FMTEYEWTK/file-dir-perms .
+http://www.cpan.org/doc/FMTEYEWTK/file-dir-perms .
 
 The executive summary: learn how your filesystem works.  The
 permissions on a file say what can happen to the data in that file.
@@ -1158,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