CXUX_BROKEN_CONSTANT_CONVERT isn't used anymore.
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index ca2fb7e..49a348a 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq5 - Files and Formats ($Revision: 1.26 $, $Date: 2002/09/21 21:04:17 $)
+perlfaq5 - Files and Formats ($Revision: 1.30 $, $Date: 2003/11/23 08:07:46 $)
 
 =head1 DESCRIPTION
 
@@ -30,7 +30,7 @@ value, Perl will flush the handle's buffer after each
 print() or write(). Setting $| affects buffering only for
 the currently selected default file handle. You choose this
 handle with the one argument select() call (see
-L<perlvar/$|> and L<perlfunc/select>).
+L<perlvar/$E<verbar>> and L<perlfunc/select>).
 
 Use select() to choose the desired handle, then set its
 per-filehandle variables.
@@ -110,7 +110,7 @@ C<.c.orig> file.
 
 Use the File::Temp module, see L<File::Temp> for more information.
 
-  use File::Temp qw/ tempfile tempdir /; 
+  use File::Temp qw/ tempfile tempdir /;
 
   $dir = tempdir( CLEANUP => 1 );
   ($fh, $filename) = tempfile( DIR => $dir );
@@ -141,6 +141,7 @@ temporary files in one process, use a counter:
            my $count = 0;
            until (defined(fileno(FH)) || $count++ > 100) {
                $base_name =~ s/-(\d+)$/"-" . (1 + $1)/e;
+               # O_EXCL is required for security reasons.
                sysopen(FH, $base_name, O_WRONLY|O_EXCL|O_CREAT);
            }
            if (defined(fileno(FH))
@@ -153,8 +154,10 @@ temporary files in one process, use a counter:
 
 =head2 How can I manipulate fixed-record-length files?
 
-The most efficient way is using pack() and unpack().  This is faster than
-using substr() when taking many, many strings.  It is slower for just a few.
+The most efficient way is using L<pack()|perlfunc/"pack"> and
+L<unpack()|perlfunc/"unpack">.  This is faster than using
+L<substr()|perlfunc/"substr"> when taking many, many strings.  It is
+slower for just a few.
 
 Here is a sample chunk of code to break up and put back together again
 some fixed-format input lines, in this case from the output of a normal,
@@ -162,22 +165,23 @@ Berkeley-style ps:
 
     # sample input line:
     #   15158 p5  T      0:00 perl /home/tchrist/scripts/now-what
-    $PS_T = 'A6 A4 A7 A5 A*';
-    open(PS, "ps|");
-    print scalar <PS>; 
-    while (<PS>) {
-       ($pid, $tt, $stat, $time, $command) = unpack($PS_T, $_);
-       for $var (qw!pid tt stat time command!) {
-           print "$var: <$$var>\n";
+    my $PS_T = 'A6 A4 A7 A5 A*';
+    open my $ps, '-|', 'ps';
+    print scalar <$ps>;
+    my @fields = qw( pid tt stat time command );
+    while (<$ps>) {
+        my %process;
+        @process{@fields} = unpack($PS_T, $_);
+       for my $field ( @fields ) {
+           print "$field: <$process{$field}>\n";
        }
-       print 'line=', pack($PS_T, $pid, $tt, $stat, $time, $command),
-               "\n";
+       print 'line=', pack($PS_T, @process{@fields} ), "\n";
     }
 
-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 okay in small programs, but doesn't scale
-well.   It also only works on global variables, not lexicals.
+We've used a hash slice in order to easily handle the fields of each row.
+Storing the keys in an array means it's easy to operate on them as a
+group or loop over them with for. It also avoids polluting the program
+with global variables and using symbolic references.
 
 =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?
 
@@ -282,9 +286,9 @@ an expression where you would place the filehandle:
 That block is a proper block like any other, so you can put more
 complicated code there.  This sends the message out to one of two places:
 
-    $ok = -x "/bin/cat";                
+    $ok = -x "/bin/cat";
     print { $ok ? $fd[1] : $fd[2] } "cat stat $ok\n";
-    print { $fd[ 1+ ($ok || 0) ]  } "cat stat $ok\n";           
+    print { $fd[ 1+ ($ok || 0) ]  } "cat stat $ok\n";
 
 This approach of treating C<print> and C<printf> like object methods
 calls doesn't work for the diamond operator.  That's because it's a
@@ -369,7 +373,7 @@ I<then> gives you read-write access:
     open(FH, "+> /path/name");         # WRONG (almost always)
 
 Whoops.  You should instead use this, which will fail if the file
-doesn't exist.  
+doesn't exist.
 
     open(FH, "+< /path/name");         # open for update
 
@@ -424,8 +428,8 @@ To open file for update, file must not exist:
 
 To open a file without blocking, creating if necessary:
 
-    sysopen(FH, "/tmp/somefile", O_WRONLY|O_NDELAY|O_CREAT)
-           or die "can't open /tmp/somefile: $!":
+    sysopen(FH, "/foo/somefile", O_WRONLY|O_NDELAY|O_CREAT)
+           or die "can't open /foo/somefile: $!":
 
 Be warned that neither creation nor deletion of files is guaranteed to
 be an atomic operation over NFS.  That is, two processes might both
@@ -434,7 +438,7 @@ isn't as exclusive as you might wish.
 
 See also the new L<perlopentut> if you have it (new for 5.6).
 
-=head2 Why do I sometimes get an "Argument list too long" when I use <*>?
+=head2 Why do I sometimes get an "Argument list too long" when I use E<lt>*E<gt>?
 
 The C<< <> >> operator performs a globbing operation (see above).
 In Perl versions earlier than v5.6.0, the internal glob() operator forks
@@ -458,11 +462,11 @@ best therefore to use glob() only in list context.
 
 Normally perl ignores trailing blanks in filenames, and interprets
 certain leading characters (or a trailing "|") to mean something
-special. 
+special.
 
 The three argument form of open() lets you specify the mode
 separately from the filename.  The open() function treats
-special mode characters and whitespace in the filename as 
+special mode characters and whitespace in the filename as
 literals
 
        open FILE, "<", "  file  ";  # filename is "   file   "
@@ -532,12 +536,12 @@ for your own system's idiosyncrasies (sometimes called "features").
 Slavish adherence to portability concerns shouldn't get in the way of
 your getting your job done.)
 
-For more information on file locking, see also 
+For more information on file locking, see also
 L<perlopentut/"File Locking"> if you have it (new for 5.6).
 
 =back
 
-=head2 Why can't I just open(FH, ">file.lock")?
+=head2 Why can't I just open(FH, "E<gt>file.lock")?
 
 A common bit of code B<NOT TO USE> is this:
 
@@ -549,7 +553,7 @@ which must be done in one.  That's why computer hardware provides an
 atomic test-and-set instruction.   In theory, this "ought" to work:
 
     sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT)
-               or die "can't open  file.lock: $!":
+               or die "can't open  file.lock: $!";
 
 except that lamentably, file creation (and deletion) is not atomic
 over NFS, so this won't work (at least, not every time) over the net.
@@ -699,7 +703,7 @@ to each filehandle.
 You can use the File::Slurp module to do it in one step.
 
        use File::Slurp;
-       
+
        $all_of_it = read_file($filename); # entire file in scalar
     @all_lines = read_file($filename); # one line perl element
 
@@ -710,7 +714,7 @@ do so one line at a time:
     while (<INPUT>) {
        chomp;
        # do something with $_
-    } 
+    }
     close(INPUT)               || die "can't close $file: $!";
 
 This is tremendously more efficient than reading the entire file into
@@ -735,7 +739,7 @@ You can read the entire filehandle contents into a scalar.
        $var = <INPUT>;
     }
 
-That temporarily undefs your record separator, and will automatically 
+That temporarily undefs your record separator, and will automatically
 close the file at block exit.  If the file is already open, just use this:
 
     $var = do { local $/; <INPUT> };
@@ -754,7 +758,7 @@ set it to C<""> to eliminate empty paragraphs (C<"abc\n\n\n\ndef">,
 for instance, gets treated as two paragraphs and not three), or
 C<"\n\n"> to accept empty paragraphs.
 
-Note that a blank line must have no blanks in it.  Thus 
+Note that a blank line must have no blanks in it.  Thus
 S<C<"fred\n \nstuff\n\n">> is one paragraph, but C<"fred\n\nstuff\n\n"> is two.
 
 =head2 How can I read a single character from a file?  From the keyboard?
@@ -921,7 +925,7 @@ There's also a File::Tail module from CPAN.
 If you check L<perlfunc/open>, you'll see that several of the ways
 to call open() should do the trick.  For example:
 
-    open(LOG, ">>/tmp/logfile");
+    open(LOG, ">>/foo/logfile");
     open(STDERR, ">&LOG");
 
 Or even with a literal numeric descriptor:
@@ -931,7 +935,7 @@ Or even with a literal numeric descriptor:
 
 Note that "<&STDIN" makes a copy, but "<&=STDIN" make
 an alias.  That means if you close an aliased handle, all
-aliases become inaccessible.  This is not true with 
+aliases become inaccessible.  This is not true with
 a copied one.
 
 Error checking, as always, has been left as an exercise for the reader.
@@ -949,8 +953,8 @@ to, you may be able to do this:
 
 Or, just use the fdopen(3S) feature of open():
 
-    { 
-       local *F; 
+    {
+       local *F;
        open F, "<&=$fd" or die "Cannot reopen fd=$fd: $!";
        close F;
     }