=head1 NAME
-perlfaq5 - Files and Formats ($Revision: 1.38 $, $Date: 1999/05/23 16:08:30 $)
+perlfaq5 - Files and Formats ($Revision: 1.2 $, $Date: 2001/09/26 10:44:41 $)
=head1 DESCRIPTION
=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
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;
}
For passing filehandles to functions, the easiest way is to
-preface them with a star, as in func(*STDIN). See L<perlfaq7/"Passing
-Filehandles"> for details.
+preface them with a star, as in func(*STDIN).
+See L<perlfaq7/"Passing Filehandles"> for details.
If you want to create many anonymous handles, you should check out the
Symbol, FileHandle, or IO::Handle (etc.) modules. Here's the equivalent
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;
=head2 How can I translate tildes (~) in a filename?
-Use the <> (glob()) operator, documented in L<perlfunc>. This
-requires that you have a shell installed that groks tildes, meaning
-csh or tcsh or (some versions of) ksh, and thus your code may have portability
-problems. The Glob::KGlob module (available from CPAN) gives more
-portable glob functionality.
+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
+File::KGlob module (available from CPAN) gives more portable glob
+functionality.
Within Perl, you may use this directly:
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()?
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 L<perlopentut/"File
-Locking"> if you have it (new for 5.6).
+For more information on file locking, see also
+L<perlopentut/"File Locking"> if you have it (new for 5.6).
=back
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 garanteed 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
printf "\nYou said %s, char number %03d\n",
$key, ord $key;
-For legacy DOS systems, Dan Carson <dbc@tc.fluke.COM> reports the following:
-
-To put the PC in "raw" mode, use ioctl with some magic numbers gleaned
-from msdos.c (Perl source file) and Ralf Brown's interrupt list (comes
-across the net every so often):
-
- $old_ioctl = ioctl(STDIN,0,0); # Gets device info
- $old_ioctl &= 0xff;
- ioctl(STDIN,1,$old_ioctl | 32); # Writes it back, setting bit 5
-
-Then to read a single character:
-
- sysread(STDIN,$c,1); # Read a single character
-
-And to put the PC back to "cooked" mode:
-
- ioctl(STDIN,1,$old_ioctl); # Sets it back to cooked mode.
-
-So now you have $c. If C<ord($c) == 0>, you have a two byte code, which
-means you hit a special key. Read another byte with C<sysread(STDIN,$c,1)>,
-and that value tells you what combination it was according to this
-table:
-
- # PC 2-byte keycodes = ^@ + the following:
-
- # HEX KEYS
- # --- ----
- # 0F SHF TAB
- # 10-19 ALT QWERTYUIOP
- # 1E-26 ALT ASDFGHJKL
- # 2C-32 ALT ZXCVBNM
- # 3B-44 F1-F10
- # 47-49 HOME,UP,PgUp
- # 4B LEFT
- # 4D RIGHT
- # 4F-53 END,DOWN,PgDn,Ins,Del
- # 54-5D SHF F1-F10
- # 5E-67 CTR F1-F10
- # 68-71 ALT F1-F10
- # 73-77 CTR LEFT,RIGHT,END,PgDn,HOME
- # 78-83 ALT 1234567890-=
- # 84 CTR PgUp
-
-This is all trial and error I did a long time ago; I hope I'm reading the
-file that worked...
-
=head2 How can I tell whether there's a character waiting on a filehandle?
The very first thing you should do is look into getting the Term::ReadKey
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