how to catch die in a module

Hello,

I use such a module from other people.
In this module there is the "die" statement, for example,

connect(...) or die $!;

Then my main program will die follow up this "die".
But I don't want the caller to die even if "die" was happened in the module.
How to handle with it?

Thank you.
Eliza
0
eli
8/8/2019 2:44:53 AM
perl.beginners 29349 articles. 3 followers. Follow

4 Replies
10 Views

Similar Articles

[PageSpeed] 0

--000000000000def12e058f922252
Content-Type: text/plain; charset="UTF-8"

You can create a signal handler for die and handle the exception in there.
Refer https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node116.html

On Thu, 8 Aug, 2019, 8:15 AM Eliza, <eli@chinabuckets.com> wrote:

> Hello,
>
> I use such a module from other people.
> In this module there is the "die" statement, for example,
>
> connect(...) or die $!;
>
> Then my main program will die follow up this "die".
> But I don't want the caller to die even if "die" was happened in the
> module.
> How to handle with it?
>
> Thank you.
> Eliza
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

--000000000000def12e058f922252
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">You can create a signal handler for die and handle the ex=
ception in there. Refer=C2=A0<a href=3D"https://users.cs.cf.ac.uk/Dave.Mars=
hall/PERL/node116.html">https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node11=
6.html</a></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gm=
ail_attr">On Thu, 8 Aug, 2019, 8:15 AM Eliza, &lt;<a href=3D"mailto:eli@chi=
nabuckets.com">eli@chinabuckets.com</a>&gt; wrote:<br></div><blockquote cla=
ss=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pa=
dding-left:1ex">Hello,<br>
<br>
I use such a module from other people.<br>
In this module there is the &quot;die&quot; statement, for example,<br>
<br>
connect(...) or die $!;<br>
<br>
Then my main program will die follow up this &quot;die&quot;.<br>
But I don&#39;t want the caller to die even if &quot;die&quot; was happened=
 in the module.<br>
How to handle with it?<br>
<br>
Thank you.<br>
Eliza<br>
<br>
-- <br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org" t=
arget=3D"_blank" rel=3D"noreferrer">beginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
 target=3D"_blank" rel=3D"noreferrer">beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer noreferrer" target=3D"=
_blank">http://learn.perl.org/</a><br>
<br>
<br>
</blockquote></div>

--000000000000def12e058f922252--
0
chankey007
8/8/2019 2:51:24 AM
--00000000000084c919058f94cd4e
Content-Type: text/plain; charset="UTF-8"

Hi Eliza,

For a simpler approach without "action at a distance" wrap the call which
might trigger the die in an eval. For example:

https://gist.github.com/andrewsolomon/e43522deebd71c6539ed12026f82788b

Once you feel comfortable with this you can explore various libraries for
handling this in a more elegant way. Try::Tiny is a good starting point
(with pointers to other options at the end of this page):

https://metacpan.org/pod/Try::Tiny


On Thu, Aug 8, 2019 at 3:51 AM Chankey Pathak <chankey007@gmail.com> wrote:

> You can create a signal handler for die and handle the exception in there.
> Refer https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node116.html
>
> On Thu, 8 Aug, 2019, 8:15 AM Eliza, <eli@chinabuckets.com> wrote:
>
>> Hello,
>>
>> I use such a module from other people.
>> In this module there is the "die" statement, for example,
>>
>> connect(...) or die $!;
>>
>> Then my main program will die follow up this "die".
>> But I don't want the caller to die even if "die" was happened in the
>> module.
>> How to handle with it?
>>
>> Thank you.
>> Eliza
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
>> For additional commands, e-mail: beginners-help@perl.org
>> http://learn.perl.org/
>>
>>
>>

-- 


Andrew Solomon
Director

P: +44 7931 946 062
E: andrew@geekuni.com

--00000000000084c919058f94cd4e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi Eliza,<div><br></div><div>For a simpler approach withou=
t &quot;action at a distance&quot; wrap the call which might trigger the di=
e in an eval. For example:</div><div><br></div><div><a href=3D"https://gist=
..github.com/andrewsolomon/e43522deebd71c6539ed12026f82788b">https://gist.gi=
thub.com/andrewsolomon/e43522deebd71c6539ed12026f82788b</a><br></div><div><=
br></div><div>Once you feel comfortable with this you can explore various l=
ibraries for handling this in a more elegant way. Try::Tiny is a good start=
ing point (with pointers to other options at the end of this page):</div><d=
iv><br></div><div><a href=3D"https://metacpan.org/pod/Try::Tiny">https://me=
tacpan.org/pod/Try::Tiny</a><br></div><div><br></div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, Aug 8, 2019 =
at 3:51 AM Chankey Pathak &lt;<a href=3D"mailto:chankey007@gmail.com">chank=
ey007@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pad=
ding-left:1ex"><div dir=3D"auto">You can create a signal handler for die an=
d handle the exception in there. Refer=C2=A0<a href=3D"https://users.cs.cf.=
ac.uk/Dave.Marshall/PERL/node116.html" target=3D"_blank">https://users.cs.c=
f.ac.uk/Dave.Marshall/PERL/node116.html</a></div><br><div class=3D"gmail_qu=
ote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, 8 Aug, 2019, 8:15 AM Eli=
za, &lt;<a href=3D"mailto:eli@chinabuckets.com" target=3D"_blank">eli@china=
buckets.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding=
-left:1ex">Hello,<br>
<br>
I use such a module from other people.<br>
In this module there is the &quot;die&quot; statement, for example,<br>
<br>
connect(...) or die $!;<br>
<br>
Then my main program will die follow up this &quot;die&quot;.<br>
But I don&#39;t want the caller to die even if &quot;die&quot; was happened=
 in the module.<br>
How to handle with it?<br>
<br>
Thank you.<br>
Eliza<br>
<br>
-- <br>
To unsubscribe, e-mail: <a href=3D"mailto:beginners-unsubscribe@perl.org" r=
el=3D"noreferrer" target=3D"_blank">beginners-unsubscribe@perl.org</a><br>
For additional commands, e-mail: <a href=3D"mailto:beginners-help@perl.org"=
 rel=3D"noreferrer" target=3D"_blank">beginners-help@perl.org</a><br>
<a href=3D"http://learn.perl.org/" rel=3D"noreferrer noreferrer" target=3D"=
_blank">http://learn.perl.org/</a><br>
<br>
<br>
</blockquote></div>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
 class=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"ltr"><div dir=
=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr=
"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div =
dir=3D"ltr"><div><br></div><div><img src=3D"https://docs.google.com/uc?expo=
rt=3Ddownload&amp;id=3D15h2UEASKUcLW7qDGevPEusquwvvKEL1_&amp;revid=3D0B-2XU=
FfrRr8CU1ZySzE5aUQvVmtZOWJYRytKRlNtc3hwcnl3PQ"><br></div><div><div><div><sp=
an style=3D"font-size:large">Andrew Solomon</span><br></div><div>Director</=
div><br></div><div>P: +44 7931 946 062</div><div>E:=C2=A0<a href=3D"mailto:=
andrew@geekuni.com" target=3D"_blank">andrew@geekuni.com</a></div></div><di=
v><br></div><div><br></div></div></div></div></div></div></div></div></div>=
</div></div></div></div></div></div>

--00000000000084c919058f94cd4e--
0
andrew
8/8/2019 6:02:18 AM
Hi,

On Thu, 8 Aug 2019 08:21:24 +0530
Chankey Pathak <chankey007@gmail.com> wrote:

> You can create a signal handler for die and handle the exception in there.
> Refer https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node116.html
>=20

Also see block eval - https://perldoc.pl/functions/eval - or one of its
wrappers like https://metacpan.org/pod/Try::Tiny .

Caveat: I maintain https://metacpan.org/pod/Error and recommend against usi=
ng
the latter.

> On Thu, 8 Aug, 2019, 8:15 AM Eliza, <eli@chinabuckets.com> wrote:
>=20
> > Hello,
> >
> > I use such a module from other people.
> > In this module there is the "die" statement, for example,
> >
> > connect(...) or die $!;
> >
> > Then my main program will die follow up this "die".
> > But I don't want the caller to die even if "die" was happened in the
> > module.
> > How to handle with it?
> >
> > Thank you.
> > Eliza
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > http://learn.perl.org/
> >
> >
> > =20



--=20
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Perl Elems to Avoid - http://perl-begin.org/tutorials/bad-elements/

John: Hey, we are completely non-violent vampires. We don=E2=80=99t suck bl=
ood.
Selina: I thought all vampires suck blood.
John: Bullocks, hen. Vampires come in all shapes and sizes.
    =E2=80=94 http://www.shlomifish.org/humour/Selina-Mandrake/

Please reply to list if it's a mailing list post - http://shlom.in/reply .
0
shlomif
8/8/2019 6:19:11 AM
Hi all,

Try::Tiny seems work fine to me. thanks.

在 2019/8/8 14:19, Shlomi Fish 写道:
>> You can create a signal handler for die and handle the exception in there.
>> Referhttps://users.cs.cf.ac.uk/Dave.Marshall/PERL/node116.html
>>
> Also see block eval -https://perldoc.pl/functions/eval  - or one of its
> wrappers likehttps://metacpan.org/pod/Try::Tiny  .
> 
> Caveat: I maintainhttps://metacpan.org/pod/Error  and recommend against using
> the latter.
> 
0
eli
8/8/2019 6:28:42 AM
Reply: