Front page | perl.beginners |
Postings from October 2021
Re: avoid errors when printing to socket
Thread Previous
|
Thread Next
From:
Andy Bach
Date:
October 1, 2021 22:01
Subject:
Re: avoid errors when printing to socket
Message ID:
CACV6rWJVsv6qEbw5g3mfsqGNWMuNbKwKc7ioGhUpHfmxPVCfoA@mail.gmail.com
> If the signal would be caught, I could handle the error and try to
reconnect, or, if that also fails, do something else.
Oh. I was thinking you wanted it to check the connection before trying to
print. Well, the print stmt returning undef will tell you the same thing.
Hmm, it would appear printing to a closed file handle *doesn't* (doesn't
always?) throw a SIGPIPE. I set the signal handler
$ perl -wE 'use strict; $SIG{"PIPE"} = sub { die "pipe\n" }; open(OUT, ">",
"/tmp/out"); 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 2.
$
So, I added a sleep and sent a SIGPIPE to the job:
$ perl -wE 'use strict; $SIG{"PIPE"} = sub { die "pipe\n" }; open(OUT, ">",
"/tmp/out"); close OUT ;my $res = print OUT "hi mom\n"; say $res; sleep
10'&
[1] 36594
print() on closed filehandle OUT at -e line 1.
Use of uninitialized value $res in say at -e line 2.
$ kill -SIGPIPE 36594
pipe
$
On Thu, Sep 30, 2021 at 5:20 PM hw <hw@adminart.net> wrote:
> On Thursday, September 30, 2021 9:30:01 PM CEST Andy Bach wrote:
> > > 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.
>
> If the signal would be caught, I could handle the error and try to
> reconnect,
> or, if that also fails, do something else.
>
> The source is here:
>
>
> https://www.adminart.net/Relaiscontrol.pm.html
> https://www.adminart.net/Relaiscontrol.pm.gz
>
>
> It's for controlling this relais:
> https://www.adminart.net/Relaiscontrol.pm.gz
>
> It works fine, I just would like to add some more error handling.
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--
a
Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk
Thread Previous
|
Thread Next