Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Read from temp file

by ITmajor (Beadle)
on Sep 03, 2008 at 20:33 UTC ( [id://708846]=perlquestion: print w/replies, xml ) Need Help??

ITmajor has asked for the wisdom of the Perl Monks concerning the following question:

I want to store data in a temp file and be able to use the file at another point in the program. The problem is that the variable that's supposed to contain the name of the temp file is the same as the filehandle.
$fh = File::Temp->new(SUFFIX=>'.txt'); $tempfile = $fh->filename;
So $fh and $tempfile both end up like this /tmp/DZ4R093.txt. I want to be able to read the data from the temp file using $tempfile. Why is it giving me the path of the temp file?

Replies are listed 'Best First'.
Re: Read from temp file
by FunkyMonk (Chancellor) on Sep 03, 2008 at 20:41 UTC
    From File::Temp:
    Note that there is no method to obtain the filehandle from the "File::Temp" object. The object itself acts as a filehandle. Also, the object is configured such that it stringifies to the name of the temporary file, and can be compared to a filename directly. The object isa "IO::Handle" and isa "IO::Seekable" so all those methods are available.
    (with my emphasis).

    Update: I used Acme::ESP when I wrote the above. I assumed you wanted to write to the tempfile and read it in again later.

Re: Read from temp file
by Thelonius (Priest) on Sep 03, 2008 at 21:44 UTC
    use File::Temp; use strict; my $fh = File::Temp->new(SUFFIX=>'.txt') or die "File::Temp: $!\n"; # $fh is a file handle. You can use it like this print $fh "This is a test\n"; # or as an object $fh->print("This is a only a test\n"); $fh->write("Testing 1,2,3\n"); # You can rewind and read it seek $fh, 0, 0 or die "Seek $fh failed: $!\n"; while (<$fh>) { print "Guess what I read: $_"; } # You can also use it as a filename to open it open ANOTHERFH, "<", $fh or die "Cannot open $fh: $!\n"; while (<ANOTHERFH>) { print "ANOTHERFH: $_"; }
Re: Read from temp file
by toolic (Bishop) on Sep 03, 2008 at 20:50 UTC
    Using Data::Dumper will illustrate moritz's point:
    use strict; use warnings; use Data::Dumper; use File::Temp; my $fh = File::Temp->new(SUFFIX=>'.txt'); my $tempfile = $fh->filename; print "fh =$fh\n"; print "tempfile=$tempfile\n"; print 'fh ', Dumper($fh); print 'tempfile ', Dumper($tempfile); __END__ fh =/tmp/JskZI6GUgu.txt tempfile=/tmp/JskZI6GUgu.txt fh $VAR1 = bless( \*{'File::Temp::$fh'}, 'File::Temp' ); tempfile $VAR1 = '/tmp/JskZI6GUgu.txt';
Re: Read from temp file
by moritz (Cardinal) on Sep 03, 2008 at 20:41 UTC
    $fh is a handle to a new file, which is empty and thus not really interesting to read, which is why $fh is opened for writing only. If you want to read from it, use open to explicitly for reading.
      which is why $fh is opened for writing only
      The filehandle is opened read-write.
Re: Read from temp file
by alexm (Chaplain) on Sep 03, 2008 at 22:22 UTC
    Maybe instead of using a temporary file you could benefit from one of the serveral Cache modules so you can store Perl data and retrieve it without seeking the file up and down. I've used Cache::Cache several times but I'm sure that there are other good modules that fit the idea.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://708846]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found