File::Temp help

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


I must be missing something.  I hope somebody can tell me what it is.

Short question:

I'm trying to use a temporary file.  However,

my $fh = tempfile();
while (<FILE>)
  print $fh $_;

only results in an empty file.

Long question:

According to the perldoc for File::Temp

    Returns the filehandle of a temporary file.
      $fh = tmpfile();

Now, I was under the impression that "the filehandle of a temporarly file"
is an incomplete concept -- that part of the definition of a filehandle
includes whether the file is open for reading or writing.  So I'm not I
understand exactly what is being returned from the tempfile() command,
or how I can use it to write data into a temporary file.

Can somebody enlighten me?



Re: File::Temp help

Naturally, as soon as I've decided I've searched everything I know,
and post a question to the newsgroup, then the next link I stumble over
has the answer.

In our last episode, the evil Dr. Lacto had captured our hero, (hymie!), who said:

Quoted text here. Click to load it

close ($fh);



Re: File::Temp help

Quoted text here. Click to load it

sounds like you were:

  Suffering from Buffering

Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Re: File::Temp help

Quoth (hymie!):
Quoted text here. Click to load it

Files created with tempfile will normally be deleted when the process
exits, or under some systems as soon as the file is closed. How are you
discovering the file is empty? You don't ask File::Temp for the

Quoted text here. Click to load it

tempfile returns a handle open for both reading and writing. In Perl
terms, the file is opened with the "<+" mode.


Re: File::Temp help

Ben Morrow wrote:
Quoted text here. Click to load it

actually more like >+. There really is no perl analogue; it's O_CREAT|
O_EXCL|O_RDWR, like, 'create if nonexistent, open only if nonexistent,
open for both reading and writing,' atomically.


   "Six by nine. Forty two."
   "That's it. That's all there is."
   "I always thought something was fundamentally wrong with the universe"

Site Timeline