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

Hi monks, i am trying to perform the following task.
I have a server, a module and a client. the client and server communicate through the module.
the server transfers a file to the module through a binary stream. the module temporarily saves the binary stream in a tempfile using the File::Temp module. before transmitting the file from the server the filehandles and the sockets are changed to binmode at both the server and module side.
now the problem comes when i try to return the file handle of this temporary file to the client. how do i save it over there? here is my code in the module.
$handle = tempfile(); binmode $handle; binmode $conn1; my $buffer=0; while(my $read = sysread($conn1,$buffer,100)) { print "Writing the file\n"; my $n= syswrite($handle,$buffer,100); } while(<$handle>) { print "$_\n"; } return *handle;
also when i try to print the temporary file using the while loop nothing is printed which means there is nothing in the file. so whats is the problem?
plus what do i do on the client side to save the file?

Replies are listed 'Best First'.
Re: saving a file using a temporary file's handle
by BrowserUk (Patriarch) on Jun 03, 2005 at 05:38 UTC

    Sounds like you need to rewind the filehandle using http://perldoc.perl.org/functions/sysseek.html.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      ok i rewinded it to the start using syseek(FILEHANDLE,0,0) but still i cant save the file on the client side. i use this code on the client side
      my $F=FSClient::getfile("$user","$passwd","$host","$port","$ARGV[1]"); binmode HANDLE; print HANDLE <$F>;
      where getfile is the method returning the handle to the temporary file as explained in the earlier post. how do i save the file over here?
        ok i know what the problem is. let me explain it to u. according to the definition of the module File::Temp the moment the program exits the file handle is closed. so even though i am returning the file handle the client cannot save it cause the handle is already closed by the module. i figured this out cause instead of closing the handle to the temporary file in the module i closed it in the client. so it gave me an error saying
        <code>Close on unopened file <handle> at ./fsclient1.pl line 247.</code how can the handle be not open. the routine from the module just returned it a few lines above. so the reason is that by the time the client tries to save the file the module has exited hence the handle to the temporary file is closed. so how do i solve this problem?
        also am i saving the file in a proper way on the client side?