Re: avoid errors when printing to socket

Andy Bach <[email protected]>
Newsgroups gmane.comp.lang.perl.beginners
Message-ID <CACV6rW+UEr-ZsbUaQdeZBh7xOpLEDycEr29uNkGE1Bh0iwtVsw@mail.gmail.com>
> https://perldoc.perl.org/functions/print says that 'print' would return
true
> if successful and doesn't say what it returns otherwise.  It also says
that
> "Printing to a closed pipe or socket will generate a SIGPIPE signal."

Looks like print returns 1 if it succeeds, undef if not:
$ perl -wE 'my $res = print "hi mom\n"; say $res'
hi mom
1
$ perl -wE 'my $res = print OUT "hi mom\n"; say $res'
Name "main::OUT" used only once: possible typo at -e line 1.
print() on unopened filehandle OUT at -e line 1.
Use of uninitialized value $res in say at -e line 1.
$ perl -E 'open(OUT, ">", STDERR) ;my $res = print OUT "hi mom\n"; say $res'
1
$ perl -wE 'open(OUT, ">", STDERR); close OUT ;my $res = print OUT "hi
mom\n"; say $res'
print() on closed filehandle OUT at -e line 1.
Use of uninitialized value $res in say at -e line 1.

>So I tried to install a signal handler, but either I did that wrong, or no
signal was generated.  What can I do?

Well, seeing a SIGPIPE would mean it's failed already, so it's not going to
get you any further. Showing your code might help in debugging.

On Thu, Sep 30, 2021 at 12:44 PM hw <[email protected]> wrote:

> Hi,
>
> I have a program in which I'm creating an UDP socket with IO::Socket::INET
> to
> a device on my LAN.  Opening the socket yields no error.  Writing to the
> socket with 'print' works fine.  The device sends data back, and receiving
> the
> data works fine.
>
> But it doesn't work when the device is offline, like when I pull the
> network
> cable the device is connected to.  Of course it's obvious that it doesn't
> work, and I'm getting an error message:
>
>
> print() on closed filehandle GEN0 at ...
>
>
> My program keeps running nonetheless, which is ok.
>
> Now the problem is that my program needs a way to detect whether the
> device is
> offline or not so I can make it try to reconnect.
>
> Fortunately, IO::Socket has a 'connected' method, yet unfortunately, the
> method can't really tell whether the socket is actually open or not
> (especially with UDP sockets, I guess).
>
> But then, there must be a way to figure this out because otherwise 'print'
> wouldn't give an error message when the socket is not open.
>
> So how can I either find out in advance if a 'print' will be successful,
> or how
> can I find out if 'print' was successful or not?  It's like I'm seeing the
> error message and there isn't anything I could do about it.
>
> https://perldoc.perl.org/functions/print says that 'print' would return
> true
> if successful and doesn't say what it returns otherwise.  It also says
> that
> "Printing to a closed pipe or socket will generate a SIGPIPE signal."
>
> So I tried to install a signal handler, but either I did that wrong, or no
> signal was generated.  What can I do?
>
>
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>

-- 

a

Andy Bach,
[email protected]
608 658-1890 cell
608 261-5738 wk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.