7.5. Creating Temporary FilesProblemYou need to create a temporary file and have it automatically deleted when your program exits. For instance, you want to write a temporary configuration file to feed a program you launch. In this case, you want to know the temporary file's name to tell the utility program. In other cases, you may just want a temporary file to write to and read from, and don't need to know its filename. Solution
If you don't need to know the file's name, use the use IO::File; $fh = IO::File->new_tmpfile or die "Unable to make new temporary file: $!";
If you need to know the file's name, use the use IO::File; use POSIX qw(tmpnam); # try new temporary filenames until we get one that didn't already exist do { $name = tmpnam() } until $fh = IO::File->new($name, O_RDWR|O_CREAT|O_EXCL); # 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 : $!" } # now go on to use the file ... Discussion
If you only need scratch space, the IO::File module's for (;;) { $name = tmpnam(); sysopen(TMP, $tmpnam, O_RDWR | O_CREAT | O_EXCL) && last; } unlink $tmpnam; This file will be automatically deleted when your program finally exits or the file is closed. You cannot find its name to tell another process, because it doesn't have a name. In fact, on systems that support such semantics, the filename was already deleted before the method returned. A child process could inherit the open file descriptor, however.[ 2 ]
This shows use IO::File; $fh = IO::File->new_tmpfile or die "IO::File->new_tmpfile: $!"; $fh->autoflush(1); print $fh "$i\n" while $i++ < 10; seek($fh, 0, 0) or die "seek: $!"; print "Tmp file has: ", <$fh>;
The second solution gets a temporary file whose name you can give to another process. We use the
See AlsoThe documentation for the standard IO::File and POSIX modules (also in Chapter 7 of Programming Perl ); Recipe 7.19 ; your system's tmpnam (3) manpage Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|