cool quote for perldebug
[p5sagit/p5-mst-13.2.git] / pod / perlfaq5.pod
index 9c5e842..03d5e6a 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq5 - Files and Formats ($Revision: 1.20 $, $Date: 1997/03/19 17:24:51 $)
+perlfaq5 - Files and Formats ($Revision: 1.22 $, $Date: 1997/04/24 22:44:02 $)
 
 =head1 DESCRIPTION
 
@@ -138,7 +138,7 @@ 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) } 
+        while ( <FH> ) { $addr = tell(FH) unless eof(FH) }
         truncate(FH, $addr);
 
 Error checking is left as an exercise for the reader.
@@ -231,9 +231,39 @@ Internally, Perl believes filehandles to be of class IO::Handle.  You
 may use that module directly if you'd like (see L<IO::Handle>), or
 one of its more specific derived classes.
 
+Once you have IO::File or FileHandle objects, you can pass them
+between subroutines or store them in hashes as you would any other
+scalar values:
+
+    use FileHandle;
+
+    # Storing filehandles in a hash and array
+    foreach $filename (@names) {
+        my $fh = new FileHandle($filename)             or die;
+        $file{$filename} = $fh;
+        push(@files, $fh);
+    }
+
+    # Using the filehandles in the array
+    foreach $file (@files) {
+       print $file "Testing\n";
+    }
+
+    # You have to do the { } ugliness when you're specifying the
+    # filehandle by anything other than a simple scalar variable.
+    print { $files[2] } "Testing\n";
+
+    # Passing filehandles to subroutines
+    sub debug {
+       my $filehandle = shift;
+       printf $filehandle "DEBUG: ", @_;
+    }
+
+    debug($fh, "Testing\n");
+
 =head2 How can I set up a footer format to be used with write()?
 
-There's no built-in way to do this, but L<perlform> has a couple of
+There's no builtin way to do this, but L<perlform> has a couple of
 techniques to make it possible for the intrepid hacker.
 
 =head2 How can I write() into a string?
@@ -262,6 +292,18 @@ You can't just:
 because you have to put the comma in and then recalculate your
 position.
 
+Alternatively, this 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 reverse $input;
+    }
+
 =head2 How can I translate tildes (~) in a filename?
 
 Use the E<lt>E<gt> (glob()) operator, documented in L<perlfunc>.  This
@@ -358,7 +400,7 @@ permissions, timestamps, inode info, etc.
 
 =head2 How can I lock a file?
 
-Perl's built-in flock() function (see L<perlfunc> for details) will call
+Perl's builtin flock() function (see L<perlfunc> for details) will call
 flock(2) if that exists, fcntl(2) if it doesn't (on perl version 5.004 and
 later), and lockf(3) if neither of the two previous system calls exists.
 On some systems, it may even use a different form of native locking.
@@ -409,8 +451,7 @@ over NFS, so this won't work (at least, not every time) over the net.
 Various schemes involving involving link() have been suggested, but
 these tend to involve busy-wait, which is also subdesirable.
 
-=head2 I still don't get locking.  I just want to increment the number 
-in the file.  How can I do this?
+=head2 I still don't get locking.  I just want to increment the number in the file.  How can I do this?
 
 Didn't anyone ever tell you web-page hit counters were useless?
 
@@ -786,3 +827,4 @@ file in.
 
 Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
 All rights reserved.  See L<perlfaq> for distribution information.
+