=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
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.
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?
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
=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.
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?
Copyright (c) 1997 Tom Christiansen and Nathan Torkington.
All rights reserved. See L<perlfaq> for distribution information.
+