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

Hello, I started a perl script as a service on a UNIX box. This service is connected to a socket number (datasource). So each time my PERL script is printing to STDOUT, this will be directly printing in the datasource. My problem is when the datasource has been desactivated (manually) and so that the connection to the socket has been lost. The only way I found to see this loss of connection is to use the SIGPIPE signal. But this one will only occur after the next print attempt. It could be a long time before I got it!! My script is not writting all the time in the datasource (it depends on a configurable parameters). Any idea on a way to see this loss of connection before trying to send a print to STDOUT (and so to the datasource)? Thx in advance.

Replies are listed 'Best First'.
Re: Catch loss of communication
by Anonymous Monk on May 31, 2005 at 12:29 UTC
    Try IO::Select's can_read can_write
      Could you be more detailed? I guess these method has to be used before writting or reading in the socket. But my problem is that I do not want to wait till the next print to know that I lost the connection with the socket (like with signal).
        then use select() to wait for something to happen and then check whether you can write, read or it's an error examining its output arguments.

        There are two versions of select available, one from the core (see perlfunc) and the other, maybe easier to use, from IO::Select.

Re: Catch loss of communication
by thcsoft (Monk) on May 31, 2005 at 12:57 UTC
    you probably need to install hooks for any relevant signals. e.g.:
    local $SIG{PIPE} = sub { ... };

    language is a virus from outer space.
Re: Catch loss of communication
by splinky (Hermit) on May 31, 2005 at 14:38 UTC
    As far as I know, there's no way to get that information before you attempt the write. In general, under *nix at least, your program doesn't get notified of changes to the status of filehandles until it tries to manipulate the filehandle.
Re: Catch loss of communication
by zentara (Cardinal) on May 31, 2005 at 16:25 UTC
Re: Catch loss of communication
by ikegami (Patriarch) on May 31, 2005 at 16:33 UTC
    If select doesn't help, you could periodically "ping" the connection by issuing a command that does nothing.