'lo all,

  I took a look in my copy of the Perl Cookbook for temp file recipes.
(I love that book.)  They have two examples.  (I have modified them a
little here.)  The first one is good to use as long as you don't need to
know the name of the temp file.


use IO::File;
$fh = IO::File->new_tmpfile
        or die "Unable to make new temporary file: $!";
$fh->autoflush(1);
print $fh "$i\n" while $i++ < 10;
seek($fh, 0, 0);
print "Tmp file has: ", <$fh>;
__END__


  I tested the above script on my Mac and it worked fine.

  If you do need to know the name of the temp file it becomes a little
more complex.  Basically you have to do some of the work yourself.  Here's
the code I used.


use IO::File;
use POSIX qw(tmpnam);
# Keep trying to open a temp file until we sucessfully open one
do { $name = tmpnam() }
        until $fh = IO::File->new($name, O_RDWR|O_CREAT|O_EXCL);
# Set up the automatic deletion of the file
END { unlink($name) or die "Couldn't unlink $name : $!" }
$fh->autoflush(1);
print $fh "$i\n" while $i++ < 10;
seek($fh, 0, 0);
print "$name has: ", <$fh>;
close($fh);
__END__


  This second code snippet worked fine on my Win32 machine at work and on
my Linux shell account.  Unfortunately it doesn't work on my Mac.  The
temp file is created, the name is correctly displayed, and it is deleted
when the script quits.  But the contents of the file are not displayed.  I
am not sure why.  Any ideas?

Andy

On Thu, 1 Feb 2001, Todd Richmond wrote:

> On 2/1/01 2:50 PM, "John Delacour" <[EMAIL PROTECTED]> wrote:
>
> > At 12:10 pm -0800 1/2/01, Todd Richmond wrote:
> >
> > |   What's the best way to do temporary file creation? BioPerl seems to have
> > |   chosen File::Temp, which requires File::Spec, which requires Errno.pm. The
> > |   process fails at the end because 'archname' is not defined for MacOS.
> >
> > I do
> >
> >       $tempdir = $ENV{TMPDIR};
> >       mkdir $tempdir, 0; #creates if necessary
> >       $fout = "$tempdir" . "temp.txt";
> >       open FOUT, ">$fout";
> >
>
> That's a bit simplistic. You need a solution that will create as many unique
> tempfiles as you need (with some checking to make sure that there's enough
> space, etc.). File::Temp would be perfect if it ran under MacOS.
>
> --
> Dr Todd Richmond                 http://cellwall.stanford.edu/todd
> Carnegie Institution             email: [EMAIL PROTECTED]
> Department of Plant Biology      fax: 1-650-325-6857
> 260 Panama Street                phone: 1-650-325-1521 x431
> Stanford, CA 94305
>

-- 
===========================================================================
Andy Berkvam          | "I don't have a distinctive voice.
                      |  At least, not a distinctive voice
Email:                |  like anyone else's..."
 [EMAIL PROTECTED] |                              - Me
 [EMAIL PROTECTED] |-WWW Pages: <http://www.coredcs.com/~aberkvam/>
===========================================================================



Reply via email to