FAQ sync.
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index 95957eb..4cf2598 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq5 - Files and Formats ($Revision: 1.3 $, $Date: 2001/10/16 13:27:22 $)
+perlfaq5 - Files and Formats ($Revision: 1.15 $, $Date: 2002/04/12 02:02:05 $)
 
 =head1 DESCRIPTION
 
@@ -78,83 +78,8 @@ See L<perlfaq9> for other examples of fetching URLs over the web.
 
 =head2 How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?
 
-Those are operations of a text editor.  Perl is not a text editor.
-Perl is a programming language.  You have to decompose the problem into
-low-level calls to read, write, open, close, and seek.
-
-Although humans have an easy time thinking of a text file as being a
-sequence of lines that operates much like a stack of playing cards--or
-punch cards--computers usually see the text file as a sequence of bytes.
-In general, there's no direct way for Perl to seek to a particular line
-of a file, insert text into a file, or remove text from a file.
-
-(There are exceptions in special circumstances.  You can add or remove
-data at the very end of the file.  A sequence of bytes can be replaced
-with another sequence of the same length.  The C<$DB_RECNO> array
-bindings as documented in L<DB_File> also provide a direct way of
-modifying a file.  Files where all lines are the same length are also
-easy to alter.)
-
-The general solution is to create a temporary copy of the text file with
-the changes you want, then copy that over the original.  This assumes
-no locking.
-
-    $old = $file;
-    $new = "$file.tmp.$$";
-    $bak = "$file.orig";
-
-    open(OLD, "< $old")        or die "can't open $old: $!";
-    open(NEW, "> $new")        or die "can't open $new: $!";
-
-    # Correct typos, preserving case
-    while (<OLD>) {
-       s/\b(p)earl\b/${1}erl/i;
-       (print NEW $_)          or die "can't write to $new: $!";
-    }
-
-    close(OLD)                 or die "can't close $old: $!";
-    close(NEW)                         or die "can't close $new: $!";
-
-    rename($old, $bak)         or die "can't rename $old to $bak: $!";
-    rename($new, $old)         or die "can't rename $new to $old: $!";
-
-Perl can do this sort of thing for you automatically with the C<-i>
-command-line switch or the closely-related C<$^I> variable (see
-L<perlrun> for more details).  Note that
-C<-i> may require a suffix on some non-Unix systems; see the
-platform-specific documentation that came with your port.
-
-    # Renumber a series of tests from the command line
-    perl -pi -e 's/(^\s+test\s+)\d+/ $1 . ++$count /e' t/op/taint.t
-
-    # form a script
-    local($^I, @ARGV) = ('.orig', glob("*.c"));
-    while (<>) {
-       if ($. == 1) {
-           print "This line should appear at the top of each file\n";
-       }
-       s/\b(p)earl\b/${1}erl/i;        # Correct typos, preserving case
-       print;
-       close ARGV if eof;              # Reset $.
-    }
-
-If you need to seek to an arbitrary line of a file that changes
-infrequently, you could build up an index of byte positions of where
-the line ends are in the file.  If the file is large, an index of
-every tenth or hundredth line end would allow you to seek and read
-fairly efficiently.  If the file is sorted, try the look.pl library
-(part of the standard perl distribution).
-
-In the unique case of deleting lines at the end of a file, you
-can use tell() and truncate().  The following code snippet deletes
-the last line of a file without making a copy or reading the
-whole file into memory:
-
-       open (FH, "+< $file");
-        while ( <FH> ) { $addr = tell(FH) unless eof(FH) }
-        truncate(FH, $addr);
-
-Error checking is left as an exercise for the reader.
+Use the Tie::File module, which is included in the standard
+distribution since Perl 5.8.0.
 
 =head2 How do I count the number of lines in a file?
 
@@ -425,37 +350,23 @@ See L<perlform/"Accessing Formatting Internals"> for an swrite() function.
 
 =head2 How can I output my numbers with commas added?
 
-This one will do it for you:
-
-    sub commify {
-        my $number = shift;
-       1 while ($number =~ s/^([-+]?\d+)(\d{3})/$1,$2/);
-       return $number;
-    }
-
-    $n = 23659019423.2331;
-    print "GOT: ", commify($n), "\n";
+This one from Benjamin Goldberg will do it for you:
 
-    GOT: 23,659,019,423.2331
+   s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
 
-You can't just:
+or written verbosely:
 
-    s/^([-+]?\d+)(\d{3})/$1,$2/g;
-
-because you have to put the comma in and then recalculate your
-position.
-
-Alternatively, this code commifies all numbers in a line regardless of
-whether they have decimal portions, are preceded by + or -, or
-whatever:
-
-    # from Andrew Johnson <ajohnson@gpu.srv.ualberta.ca>
-    sub commify {
-       my $input = shift;
-        $input = reverse $input;
-        $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g;
-        return scalar reverse $input;
-    }
+   s/(
+       ^[-+]?            # beginning of number.
+       \d{1,3}?          # first digits before first comma
+       (?=               # followed by, (but not included in the match) :
+          (?>(?:\d{3})+) # some positive multiple of three digits.
+          (?!\d)         # an *exact* multiple, not x * 3 + 1 or whatever.
+       )
+      |                  # or:
+       \G\d{3}           # after the last group, get three digits
+       (?=\d)            # but they have to have more digits after them.
+   )/$1,/xg;
 
 =head2 How can I translate tildes (~) in a filename?
 
@@ -576,55 +487,37 @@ 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.  To avoid this, you might want to use a routine like the one below.
-It turns incomplete pathnames into explicit relative ones, and tacks a
-trailing null byte on the name to make perl leave it alone:
-
-    sub safe_filename {
-       local $_  = shift;
-        s#^([^./])#./$1#;
-        $_ .= "\0";
-       return $_;
-    }
+special. 
 
-    $badpath = "<<<something really wicked   ";
-    $fn = safe_filename($badpath");
-    open(FH, "> $fn") or "couldn't open $badpath: $!";
+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 
+literals
 
-This assumes that you are using POSIX (portable operating systems
-interface) paths.  If you are on a closed, non-portable, proprietary
-system, you may have to adjust the C<"./"> above.
+       open FILE, "<", "  file  ";  # filename is "   file   "
+       open FILE, ">", ">file";     # filename is ">file"
 
-It would be a lot clearer to use sysopen(), though:
+It may be a lot clearer to use sysopen(), though:
 
     use Fcntl;
     $badpath = "<<<something really wicked   ";
     sysopen (FH, $badpath, O_WRONLY | O_CREAT | O_TRUNC)
        or die "can't open $badpath: $!";
 
-For more information, see also the new L<perlopentut> if you have it
-(new for 5.6).
-
 =head2 How can I reliably rename a file?
 
-Well, usually you just use Perl's rename() function.  That may not
-work everywhere, though, particularly when renaming files across file systems.
-Some sub-Unix systems have broken ports that corrupt the semantics of
-rename()--for example, WinNT does this right, but Win95 and Win98
-are broken.  (The last two parts are not surprising, but the first is. :-)
-
-If your operating system supports a proper mv(1) program or its moral
+If your operating system supports a proper mv(1) utility or its functional
 equivalent, this works:
 
     rename($old, $new) or system("mv", $old, $new);
 
-It may be more compelling to use the File::Copy module instead.  You
-just copy to the new file to the new name (checking return values),
-then delete the old one.  This isn't really the same semantically as a
-real rename(), though, which preserves metainformation like
+It may be more portable to use the File::Copy module instead.
+You just copy to the new file to the new name (checking return
+values), then delete the old one.  This isn't really the same
+semantically as a rename(), which preserves meta-information like
 permissions, timestamps, inode info, etc.
 
-Newer versions of File::Copy exports a move() function.
+Newer versions of File::Copy export a move() function.
 
 =head2 How can I lock a file?
 
@@ -769,14 +662,17 @@ Don't forget them or you'll be quite sorry.
 
 =head2 How do I get a file's timestamp in perl?
 
-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> 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,
-then use localtime(), gmtime(), or POSIX::strftime() to convert this
-into human-readable form.
+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> 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. Some platforms may not have
+all of these times.  See L<perlport> for details. To
+retrieve the "raw" time in seconds since the epoch, you
+would call the stat function, then use localtime(),
+gmtime(), or POSIX::strftime() to convert this into
+human-readable form.
 
 Here's an example:
 
@@ -838,7 +734,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.
 
@@ -861,27 +757,14 @@ you see someone do this:
 
     @lines = <INPUT>;
 
-you should think long and hard about why you need everything loaded
-at once.  It's just not a scalable solution.  You might also find it
-more fun to use the standard DB_File module's $DB_RECNO bindings,
-which allow you to tie an array to a file so that accessing an element
-the array actually accesses the corresponding line in the file.
+you should think long and hard about why you need everything loaded at
+once.  It's just not a scalable solution.  You might also find it more
+fun to use the standard Tie::File module, or the DB_File module's
+$DB_RECNO bindings, which allow you to tie an array to a file so that
+accessing an element the array actually accesses the corresponding
+line in the file.
 
-On very rare occasion, you may have an algorithm that demands that
-the entire file be in memory at once as one scalar.  The simplest solution
-to that is
-
-    $var = `cat $file`;
-
-Being in scalar context, you get the whole thing.  In list context,
-you'd get a list of all the lines:
-
-    @lines = `cat $file`;
-
-This tiny but expedient solution is neat, clean, and portable to
-all systems on which decent tools have been installed.  For those
-who prefer not to use the toolbox, you can of course read the file
-manually, although this makes for more complicated code.
+You can read the entire filehandle contents into a scalar.
 
     {
        local(*INPUT, $/);
@@ -894,6 +777,13 @@ close the file at block exit.  If the file is already open, just use this:
 
     $var = do { local $/; <INPUT> };
 
+For ordinary files you can also use the read function.
+
+       read( INPUT, $var, -s INPUT );
+
+The third argument tests the byte size of the data on the INPUT filehandle
+and reads that many bytes into the buffer $var.
+
 =head2 How can I read in a file by paragraphs?
 
 Use the C<$/> variable (see L<perlvar> for details).  You can either
@@ -901,8 +791,8 @@ 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 C<"fred\n
-\nstuff\n\n"> is one paragraph, but C<"fred\n\nstuff\n\n"> is two.
+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?
 
@@ -1102,7 +992,7 @@ Or, just use the fdopen(3S) feature of open():
        close F;
     }
 
-=head2 Why can't I use "C:\temp\foo" in DOS paths?  What doesn't `C:\temp\foo.exe` work?
+=head2 Why can't I use "C:\temp\foo" in DOS paths?  Why doesn't `C:\temp\foo.exe` work?
 
 Whoops!  You just put a tab and a formfeed into that filename!
 Remember that within double quoted strings ("like\this"), the
@@ -1128,9 +1018,9 @@ documentation for details.
 
 =head2 Why does Perl let me delete read-only files?  Why does C<-i> clobber protected files?  Isn't this a bug in Perl?
 
-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 .
+This is elaborately and painstakingly described in the
+F<file-dir-perms> article in the "Far More Than You Ever Wanted To
+Know" collection in http://www.cpan.org/olddoc/FMTEYEWTK.tgz .
 
 The executive summary: learn how your filesystem works.  The
 permissions on a file say what can happen to the data in that file.
@@ -1176,7 +1066,7 @@ If your array contains lines, just print them:
 
 =head1 AUTHOR AND COPYRIGHT
 
-Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington.
+Copyright (c) 1997-2002 Tom Christiansen and Nathan Torkington.
 All rights reserved.
 
 This documentation is free; you can redistribute it and/or modify it