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

Hi, I have the followinig code which runs in a number of threads -
my $socket = new IO::Socket::INET ( PeerAddr => $ipAddress, PeerPort => $port, Proto => 'tcp' ); if(defined $socket) { print $socket "Hi!\n"; $socket.close(); } else { print "Failed to connect to video server: $ipAddress, $port\n"; }
The problem is that if I manage to reach the line print $socket "Hi!\n"; I will get the warning "print() on closed filehandle STDOUT" every time I try to print in the normal fashion after this point. However, I can print fine in those threads that dont manage to connect to a server socket (those that dont run the line print $socket "Hi!\n";). Why have STDOUT close down for some threads?
Thanks.

Replies are listed 'Best First'.
Re: print() on closed filehandle STDOUT after printing to socket
by almut (Canon) on Mar 18, 2009 at 21:36 UTC
    $socket.close();

    You probably meant $socket->close() — the dot operator is string concatenation in Perl.   close() without any argument will close the currently selected filehandle — presumably STDOUT in this case...

Re: print() on closed filehandle STDOUT after printing to socket
by ikegami (Patriarch) on Mar 18, 2009 at 22:06 UTC

    Perhaps you should pay attention to the earlier Useless use of concatenation (.) or string in void context error...

Re: print() on closed filehandle STDOUT after printing to socket
by apl (Monsignor) on Mar 19, 2009 at 11:58 UTC
    The use strict; use warnings; strictures might have helped reveal the problem almut pointed out.