about package method returns

--000000000000377c09059631a802
Content-Type: text/plain; charset="UTF-8"

Hello

Sorry I am new to perl, I was reading the charter about package.

I tried to write the code below:

use strict;
use Net::Ping;

package A;

sub mytest {

   my $host = shift;
   my $p = Net::Ping->new();
   unless ($p->ping($host)) {
       $p->close();
       die "can't ping $host";
   }
}

1;

package B;

sub mytest {

   my $host = shift;
   my $p = Net::Ping->new();
   unless ($p->ping($host)) {
       $p->close();
       return 0;
   }
}

1;

package main;

A::mytest('www.google.com');

print B::mytest('www.google.com');



When I run it, always get:
$ perl test.pl
can't ping www.google.com at test.pl line 12.


Shouldn't I return die() in package's method?
How do I let the caller know what happens when the method fails to run?

Thanks in advance.

Yours
Maggie

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

<div dir=3D"ltr"><div>Hello</div><div><br></div><div>Sorry I am new to perl=
, I was reading the charter about package.</div><div><br></div><div>I tried=
 to write the code below:</div><div><br></div><div>use strict;<br>use Net::=
Ping;<br><br>package A;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $host =
=3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =C2=A0u=
nless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;close(); =
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0die &quot;can&#39;t ping $host&quot;;<br>=C2=
=A0 =C2=A0}<br>}<br><br>1;<br><br>package B;<br><br>sub mytest {<br><br>=C2=
=A0 =C2=A0my $host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new()=
;<br>=C2=A0 =C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0$p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>=C2=A0 =C2=
=A0}<br>}<br><br>1;<br><br>package main;<br><br>A::mytest(&#39;<a href=3D"h=
ttp://www.google.com">www.google.com</a>&#39;);<br><br>print B::mytest(&#39=
;<a href=3D"http://www.google.com">www.google.com</a>&#39;);</div><div><br>=
</div><div><br></div><div><br></div><div>When I run it, always get:</div><d=
iv>$ perl <a href=3D"http://test.pl">test.pl</a><br>can&#39;t ping <a href=
=3D"http://www.google.com">www.google.com</a> at <a href=3D"http://test.pl"=
>test.pl</a> line 12.</div><div><br></div><div><br></div><div>Shouldn&#39;t=
 I return die() in package&#39;s method?</div><div>How do I let the caller =
know what happens when the method fails to run?<br></div><div><br></div><di=
v>Thanks in advance.</div><div><br></div><div>Yours</div><div>Maggie<br></d=
iv></div>

--000000000000377c09059631a802--
0
rothmq
10/31/2019 9:41:32 AM
perl.beginners 29367 articles. 3 followers. Follow

5 Replies
26 Views

Similar Articles

[PageSpeed] 5

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

Hi,

You cannot return `die`, die is a fatal exception that causes`test.pl` to
exit immediately.
One option would be to use warn to emit a warning to STDERR and return to
the caller and let them handle the failure. You may as well add the $! to
the output so the caller gets a copy of the last error.
      ...
       $p->close();
       warn "can't ping $host: $!";
       return 0;
      }



https://perldoc.perl.org/functions/die.html
https://perldoc.perl.org/functions/warn.html
https://perldoc.perl.org/perlvar.html#Error-Variables

On Thu, 31 Oct 2019 at 09:42, Maggie Q Roth <rothmq@gmail.com> wrote:

> Hello
>
> Sorry I am new to perl, I was reading the charter about package.
>
> I tried to write the code below:
>
> use strict;
> use Net::Ping;
>
> package A;
>
> sub mytest {
>
>    my $host = shift;
>    my $p = Net::Ping->new();
>    unless ($p->ping($host)) {
>        $p->close();
>        die "can't ping $host";
>    }
> }
>
> 1;
>
> package B;
>
> sub mytest {
>
>    my $host = shift;
>    my $p = Net::Ping->new();
>    unless ($p->ping($host)) {
>        $p->close();
>        return 0;
>    }
> }
>
> 1;
>
> package main;
>
> A::mytest('www.google.com');
>
> print B::mytest('www.google.com');
>
>
>
> When I run it, always get:
> $ perl test.pl
> can't ping www.google.com at test.pl line 12.
>
>
> Shouldn't I return die() in package's method?
> How do I let the caller know what happens when the method fails to run?
>
> Thanks in advance.
>
> Yours
> Maggie
>

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

<div dir=3D"ltr"><div>Hi,</div><div><br></div><div>You cannot return `die`,=
 die is a fatal exception that causes`<a href=3D"http://test.pl">test.pl</a=
>` to exit immediately. <br></div><div>One option would be to use warn to e=
mit a warning to STDERR and return to the caller and let them handle the fa=
ilure. You may as well add the $! to the output so the caller gets a copy o=
f the last error.<br></div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ...<br></div=
><div>
=C2=A0 =C2=A0 =C2=A0=C2=A0 $p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0=C2=A0 w=
arn &quot;can&#39;t ping $host: $!&quot;; <br></div><div>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 return 0;</div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br>=
</div><div><br></div><div><br></div><div><br></div><div><a href=3D"https://=
perldoc.perl.org/functions/die.html">https://perldoc.perl.org/functions/die=
..html</a></div><div><a href=3D"https://perldoc.perl.org/functions/warn.html=
">https://perldoc.perl.org/functions/warn.html</a></div><div><a href=3D"htt=
ps://perldoc.perl.org/perlvar.html#Error-Variables">https://perldoc.perl.or=
g/perlvar.html#Error-Variables</a></div></div><br><div class=3D"gmail_quote=
"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, 31 Oct 2019 at 09:42, Maggi=
e Q Roth &lt;<a href=3D"mailto:rothmq@gmail.com">rothmq@gmail.com</a>&gt; w=
rote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0p=
x 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=
=3D"ltr"><div>Hello</div><div><br></div><div>Sorry I am new to perl, I was =
reading the charter about package.</div><div><br></div><div>I tried to writ=
e the code below:</div><div><br></div><div>use strict;<br>use Net::Ping;<br=
><br>package A;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $host =3D shift;=
<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =C2=A0unless ($p-=
&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0die &quot;can&#39;t ping $host&quot;;<br>=C2=A0 =C2=A0=
}<br>}<br><br>1;<br><br>package B;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0=
my $host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=
=A0 =C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&g=
t;close(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>=C2=A0 =C2=A0}<br>}<=
br><br>1;<br><br>package main;<br><br>A::mytest(&#39;<a href=3D"http://www.=
google.com" target=3D"_blank">www.google.com</a>&#39;);<br><br>print B::myt=
est(&#39;<a href=3D"http://www.google.com" target=3D"_blank">www.google.com=
</a>&#39;);</div><div><br></div><div><br></div><div><br></div><div>When I r=
un it, always get:</div><div>$ perl <a href=3D"http://test.pl" target=3D"_b=
lank">test.pl</a><br>can&#39;t ping <a href=3D"http://www.google.com" targe=
t=3D"_blank">www.google.com</a> at <a href=3D"http://test.pl" target=3D"_bl=
ank">test.pl</a> line 12.</div><div><br></div><div><br></div><div>Shouldn&#=
39;t I return die() in package&#39;s method?</div><div>How do I let the cal=
ler know what happens when the method fails to run?<br></div><div><br></div=
><div>Thanks in advance.</div><div><br></div><div>Yours</div><div>Maggie<br=
></div></div>
</blockquote></div>

--000000000000ba0bca05963b135c--
0
paikkos
10/31/2019 8:55:52 PM
--000000000000ec100205963b8f80
Content-Type: text/plain; charset="UTF-8"

> How do I let the caller know what happens when the method fails to run?

As noted, die() ends the program at that point, sending the text to stderr
and setting an exit code to the shell. If you want your subs are to
communicate with their calling code, you can use the return values to show
success or failure:
sub mytest {

   my $host = shift;
   my $p = Net::Ping->new();
   unless ($p->ping($host)) {
       $p->close();
       return 0;
   }
   return 1;
}

my $host = 'www.google.com';
if ( not A::mytest($host) ) {
   print "failed to ping: $host\n";
}
else {
   print "$host is alive\n";
}

The choice of returning one or zero is up to you (or anything else - a
range of values for different failures), just needs to be agreed on ahead
of time.  You can do something like try/catch blocks around code that dies
by using "eval", something like:
package main;

eval {
A::mytest('www.google.com');
};
if ( $@ ) {
        print "A::mytest died, saying $@\n";
}

The Perl var "$@" gets what ever the called code might have sent to stderr,
e.g.:
$ test_net_ping.pl
A::mytest died, saying can't ping www.google.com at /usr/local/bin/
test_net_ping.pl line 14.

Just to note, as a package test, normally, your package A and B would be
separate scripts (say Apingtest.pm and Bpingtest.pm, in the @INC path) and
the main code would just have
#!/usr/bin/perl
use Apingtest;
use Bpingtest;
use strict;
use warnings;
....

On Thu, Oct 31, 2019 at 4:42 AM Maggie Q Roth <rothmq@gmail.com> wrote:

> Hello
>
> Sorry I am new to perl, I was reading the charter about package.
>
> I tried to write the code below:
>
> use strict;
> use Net::Ping;
>
> package A;
>
> sub mytest {
>
>    my $host = shift;
>    my $p = Net::Ping->new();
>    unless ($p->ping($host)) {
>        $p->close();
>        die "can't ping $host";
>    }
> }
>
> 1;
>
> package B;
>
> sub mytest {
>
>    my $host = shift;
>    my $p = Net::Ping->new();
>    unless ($p->ping($host)) {
>        $p->close();
>        return 0;
>    }
> }
>
> 1;
>
> package main;
>
> A::mytest('www.google.com');
>
> print B::mytest('www.google.com');
>
>
>
> When I run it, always get:
> $ perl test.pl
> can't ping www.google.com at test.pl line 12.
>
>
> Shouldn't I return die() in package's method?
> How do I let the caller know what happens when the method fails to run?
>
> Thanks in advance.
>
> Yours
> Maggie
>


-- 

a

Andy Bach,
afbach@gmail.com
608 658-1890 cell
608 261-5738 wk

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

<div dir=3D"ltr">&gt; How do I let the caller know what happens when the me=
thod fails to run?<br><div><br></div><div>As noted, die() ends the program =
at that point, sending the text to stderr and setting an exit code to the s=
hell. If you want your subs are to communicate with their calling code, you=
 can use the return values to show success or failure:</div><div>sub mytest=
 {<br><br>=C2=A0 =C2=A0my $host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::P=
ing-&gt;new();<br>=C2=A0 =C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<b=
r>=C2=A0 =C2=A0}</div><div>=C2=A0=C2=A0 return 1;<br></div><div>}</div><div=
><br></div><div>my $host =3D &#39;<a href=3D"http://www.google.com">www.goo=
gle.com</a>&#39;;<br></div><div>if ( not A::mytest($host) ) {</div><div>=C2=
=A0=C2=A0 print &quot;failed to ping: $host\n&quot;;</div><div>}</div><div>=
else {</div><div>=C2=A0=C2=A0 print &quot;$host is alive\n&quot;;</div><div=
>}</div><div><br></div><div>The choice of returning one or zero is up to yo=
u (or anything else - a range of values for different failures), just needs=
 to be agreed on ahead of time.=C2=A0 You can do something like try/catch b=
locks around code that dies by using &quot;eval&quot;, something like:</div=
><div>package main;<br><br>eval {<br>A::mytest(&#39;<a href=3D"http://www.g=
oogle.com">www.google.com</a>&#39;);<br>};<br>if ( $@ ) {<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 print &quot;A::mytest died, saying $@\n&quot;;<br>}<br></div>=
<div><br></div><div>The Perl var &quot;$@&quot; gets what ever the called c=
ode might have sent to stderr, e.g.:<br></div><div>$ <a href=3D"http://test=
_net_ping.pl">test_net_ping.pl</a><br>A::mytest died, saying can&#39;t ping=
 <a href=3D"http://www.google.com">www.google.com</a> at /usr/local/bin/<a =
href=3D"http://test_net_ping.pl">test_net_ping.pl</a> line 14.</div><div><b=
r></div><div>Just to note, as a package test, normally, your package A and =
B would be separate scripts (say Apingtest.pm and Bpingtest.pm, in the=C2=
=A0<a class=3D"gmail_plusreply" id=3D"plusReplyChip-0">@INC path)</a> and t=
he main code would just have</div><div>#!/usr/bin/perl<br></div><div>use Ap=
ingtest;</div><div>use Bpingtest;</div><div>use strict;</div><div>use warni=
ngs;</div><div></div><div>...<br></div></div><br><div class=3D"gmail_quote"=
><div dir=3D"ltr" class=3D"gmail_attr">On Thu, Oct 31, 2019 at 4:42 AM Magg=
ie Q Roth &lt;<a href=3D"mailto:rothmq@gmail.com">rothmq@gmail.com</a>&gt; =
wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0=
px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=
=3D"ltr"><div>Hello</div><div><br></div><div>Sorry I am new to perl, I was =
reading the charter about package.</div><div><br></div><div>I tried to writ=
e the code below:</div><div><br></div><div>use strict;<br>use Net::Ping;<br=
><br>package A;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $host =3D shift;=
<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =C2=A0unless ($p-=
&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0die &quot;can&#39;t ping $host&quot;;<br>=C2=A0 =C2=A0=
}<br>}<br><br>1;<br><br>package B;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0=
my $host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=
=A0 =C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&g=
t;close(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>=C2=A0 =C2=A0}<br>}<=
br><br>1;<br><br>package main;<br><br>A::mytest(&#39;<a href=3D"http://www.=
google.com" target=3D"_blank">www.google.com</a>&#39;);<br><br>print B::myt=
est(&#39;<a href=3D"http://www.google.com" target=3D"_blank">www.google.com=
</a>&#39;);</div><div><br></div><div><br></div><div><br></div><div>When I r=
un it, always get:</div><div>$ perl <a href=3D"http://test.pl" target=3D"_b=
lank">test.pl</a><br>can&#39;t ping <a href=3D"http://www.google.com" targe=
t=3D"_blank">www.google.com</a> at <a href=3D"http://test.pl" target=3D"_bl=
ank">test.pl</a> line 12.</div><div><br></div><div><br></div><div>Shouldn&#=
39;t I return die() in package&#39;s method?</div><div>How do I let the cal=
ler know what happens when the method fails to run?<br></div><div><br></div=
><div>Thanks in advance.</div><div><br></div><div>Yours</div><div>Maggie<br=
></div></div>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature"><br>a<br><br>Andy Bach,<br><a href=3D"mailto:afbach@gmail.c=
om" target=3D"_blank">afbach@gmail.com</a><br>608 658-1890 cell<br>608 261-=
5738 wk</div>

--000000000000ec100205963b8f80--
0
afbach
10/31/2019 9:30:33 PM
--000000000000f4f0d705963e4ee1
Content-Type: text/plain; charset="UTF-8"

Thank you. how to setup $! in module, and how can I get the $! in caller?

regards.

On Fri, Nov 1, 2019 at 4:56 AM Dermot <paikkos@gmail.com> wrote:

> Hi,
>
> You cannot return `die`, die is a fatal exception that causes`test.pl` to
> exit immediately.
> One option would be to use warn to emit a warning to STDERR and return to
> the caller and let them handle the failure. You may as well add the $! to
> the output so the caller gets a copy of the last error.
>       ...
>        $p->close();
>        warn "can't ping $host: $!";
>        return 0;
>       }
>
>
>
> https://perldoc.perl.org/functions/die.html
> https://perldoc.perl.org/functions/warn.html
> https://perldoc.perl.org/perlvar.html#Error-Variables
>
> On Thu, 31 Oct 2019 at 09:42, Maggie Q Roth <rothmq@gmail.com> wrote:
>
>> Hello
>>
>> Sorry I am new to perl, I was reading the charter about package.
>>
>> I tried to write the code below:
>>
>> use strict;
>> use Net::Ping;
>>
>> package A;
>>
>> sub mytest {
>>
>>    my $host = shift;
>>    my $p = Net::Ping->new();
>>    unless ($p->ping($host)) {
>>        $p->close();
>>        die "can't ping $host";
>>    }
>> }
>>
>> 1;
>>
>> package B;
>>
>> sub mytest {
>>
>>    my $host = shift;
>>    my $p = Net::Ping->new();
>>    unless ($p->ping($host)) {
>>        $p->close();
>>        return 0;
>>    }
>> }
>>
>> 1;
>>
>> package main;
>>
>> A::mytest('www.google.com');
>>
>> print B::mytest('www.google.com');
>>
>>
>>
>> When I run it, always get:
>> $ perl test.pl
>> can't ping www.google.com at test.pl line 12.
>>
>>
>> Shouldn't I return die() in package's method?
>> How do I let the caller know what happens when the method fails to run?
>>
>> Thanks in advance.
>>
>> Yours
>> Maggie
>>
>

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

<div dir=3D"ltr"><div>Thank you. how to setup $! in module, and how can I g=
et the $! in caller?</div><div><br></div><div>regards.<br></div></div><br><=
div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Fri, Nov=
 1, 2019 at 4:56 AM Dermot &lt;<a href=3D"mailto:paikkos@gmail.com">paikkos=
@gmail.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"><div dir=3D"ltr"><div>Hi,</div><div><br></div><div>You cannot re=
turn `die`, die is a fatal exception that causes`<a href=3D"http://test.pl"=
 target=3D"_blank">test.pl</a>` to exit immediately. <br></div><div>One opt=
ion would be to use warn to emit a warning to STDERR and return to the call=
er and let them handle the failure. You may as well add the $! to the outpu=
t so the caller gets a copy of the last error.<br></div><div>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 ...<br></div><div>
=C2=A0 =C2=A0 =C2=A0=C2=A0 $p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0=C2=A0 w=
arn &quot;can&#39;t ping $host: $!&quot;; <br></div><div>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 return 0;</div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br>=
</div><div><br></div><div><br></div><div><br></div><div><a href=3D"https://=
perldoc.perl.org/functions/die.html" target=3D"_blank">https://perldoc.perl=
..org/functions/die.html</a></div><div><a href=3D"https://perldoc.perl.org/f=
unctions/warn.html" target=3D"_blank">https://perldoc.perl.org/functions/wa=
rn.html</a></div><div><a href=3D"https://perldoc.perl.org/perlvar.html#Erro=
r-Variables" target=3D"_blank">https://perldoc.perl.org/perlvar.html#Error-=
Variables</a></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cl=
ass=3D"gmail_attr">On Thu, 31 Oct 2019 at 09:42, Maggie Q Roth &lt;<a href=
=3D"mailto:rothmq@gmail.com" target=3D"_blank">rothmq@gmail.com</a>&gt; wro=
te:<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"><div dir=3D"=
ltr"><div>Hello</div><div><br></div><div>Sorry I am new to perl, I was read=
ing the charter about package.</div><div><br></div><div>I tried to write th=
e code below:</div><div><br></div><div>use strict;<br>use Net::Ping;<br><br=
>package A;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $host =3D shift;<br>=
=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =C2=A0unless ($p-&gt;=
ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0die &quot;can&#39;t ping $host&quot;;<br>=C2=A0 =C2=A0}<br=
>}<br><br>1;<br><br>package B;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $=
host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =
=C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;cl=
ose(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>=C2=A0 =C2=A0}<br>}<br><=
br>1;<br><br>package main;<br><br>A::mytest(&#39;<a href=3D"http://www.goog=
le.com" target=3D"_blank">www.google.com</a>&#39;);<br><br>print B::mytest(=
&#39;<a href=3D"http://www.google.com" target=3D"_blank">www.google.com</a>=
&#39;);</div><div><br></div><div><br></div><div><br></div><div>When I run i=
t, always get:</div><div>$ perl <a href=3D"http://test.pl" target=3D"_blank=
">test.pl</a><br>can&#39;t ping <a href=3D"http://www.google.com" target=3D=
"_blank">www.google.com</a> at <a href=3D"http://test.pl" target=3D"_blank"=
>test.pl</a> line 12.</div><div><br></div><div><br></div><div>Shouldn&#39;t=
 I return die() in package&#39;s method?</div><div>How do I let the caller =
know what happens when the method fails to run?<br></div><div><br></div><di=
v>Thanks in advance.</div><div><br></div><div>Yours</div><div>Maggie<br></d=
iv></div>
</blockquote></div>
</blockquote></div>

--000000000000f4f0d705963e4ee1--
0
rothmq
11/1/2019 12:47:08 AM
--0000000000006da2eb05963e5715
Content-Type: text/plain; charset="UTF-8"

If Net::Ping gets failure and has the special statement for the failure,
how can return this statement to the caller?

Thank you.

On Fri, Nov 1, 2019 at 5:30 AM Andy Bach <afbach@gmail.com> wrote:

> > How do I let the caller know what happens when the method fails to run?
>
> As noted, die() ends the program at that point, sending the text to stderr
> and setting an exit code to the shell. If you want your subs are to
> communicate with their calling code, you can use the return values to show
> success or failure:
> sub mytest {
>
>    my $host = shift;
>    my $p = Net::Ping->new();
>    unless ($p->ping($host)) {
>        $p->close();
>        return 0;
>    }
>    return 1;
> }
>
> my $host = 'www.google.com';
> if ( not A::mytest($host) ) {
>    print "failed to ping: $host\n";
> }
> else {
>    print "$host is alive\n";
> }
>
> The choice of returning one or zero is up to you (or anything else - a
> range of values for different failures), just needs to be agreed on ahead
> of time.  You can do something like try/catch blocks around code that dies
> by using "eval", something like:
> package main;
>
> eval {
> A::mytest('www.google.com');
> };
> if ( $@ ) {
>         print "A::mytest died, saying $@\n";
> }
>
> The Perl var "$@" gets what ever the called code might have sent to
> stderr, e.g.:
> $ test_net_ping.pl
> A::mytest died, saying can't ping www.google.com at /usr/local/bin/
> test_net_ping.pl line 14.
>
> Just to note, as a package test, normally, your package A and B would be
> separate scripts (say Apingtest.pm and Bpingtest.pm, in the @INC path)
> and the main code would just have
> #!/usr/bin/perl
> use Apingtest;
> use Bpingtest;
> use strict;
> use warnings;
> ...
>
> On Thu, Oct 31, 2019 at 4:42 AM Maggie Q Roth <rothmq@gmail.com> wrote:
>
>> Hello
>>
>> Sorry I am new to perl, I was reading the charter about package.
>>
>> I tried to write the code below:
>>
>> use strict;
>> use Net::Ping;
>>
>> package A;
>>
>> sub mytest {
>>
>>    my $host = shift;
>>    my $p = Net::Ping->new();
>>    unless ($p->ping($host)) {
>>        $p->close();
>>        die "can't ping $host";
>>    }
>> }
>>
>> 1;
>>
>> package B;
>>
>> sub mytest {
>>
>>    my $host = shift;
>>    my $p = Net::Ping->new();
>>    unless ($p->ping($host)) {
>>        $p->close();
>>        return 0;
>>    }
>> }
>>
>> 1;
>>
>> package main;
>>
>> A::mytest('www.google.com');
>>
>> print B::mytest('www.google.com');
>>
>>
>>
>> When I run it, always get:
>> $ perl test.pl
>> can't ping www.google.com at test.pl line 12.
>>
>>
>> Shouldn't I return die() in package's method?
>> How do I let the caller know what happens when the method fails to run?
>>
>> Thanks in advance.
>>
>> Yours
>> Maggie
>>
>
>
> --
>
> a
>
> Andy Bach,
> afbach@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
>

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

<div dir=3D"ltr"><div>If Net::Ping gets failure and has the special stateme=
nt for the failure, how can return this statement to the caller?</div><div>=
<br></div><div>Thank you.<br></div></div><br><div class=3D"gmail_quote"><di=
v dir=3D"ltr" class=3D"gmail_attr">On Fri, Nov 1, 2019 at 5:30 AM Andy Bach=
 &lt;<a href=3D"mailto:afbach@gmail.com">afbach@gmail.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"><div dir=3D"ltr">&=
gt; How do I let the caller know what happens when the method fails to run?=
<br><div><br></div><div>As noted, die() ends the program at that point, sen=
ding the text to stderr and setting an exit code to the shell. If you want =
your subs are to communicate with their calling code, you can use the retur=
n values to show success or failure:</div><div>sub mytest {<br><br>=C2=A0 =
=C2=A0my $host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br=
>=C2=A0 =C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$=
p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>=C2=A0 =C2=A0}</=
div><div>=C2=A0=C2=A0 return 1;<br></div><div>}</div><div><br></div><div>my=
 $host =3D &#39;<a href=3D"http://www.google.com" target=3D"_blank">www.goo=
gle.com</a>&#39;;<br></div><div>if ( not A::mytest($host) ) {</div><div>=C2=
=A0=C2=A0 print &quot;failed to ping: $host\n&quot;;</div><div>}</div><div>=
else {</div><div>=C2=A0=C2=A0 print &quot;$host is alive\n&quot;;</div><div=
>}</div><div><br></div><div>The choice of returning one or zero is up to yo=
u (or anything else - a range of values for different failures), just needs=
 to be agreed on ahead of time.=C2=A0 You can do something like try/catch b=
locks around code that dies by using &quot;eval&quot;, something like:</div=
><div>package main;<br><br>eval {<br>A::mytest(&#39;<a href=3D"http://www.g=
oogle.com" target=3D"_blank">www.google.com</a>&#39;);<br>};<br>if ( $@ ) {=
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 print &quot;A::mytest died, saying $@\n&quo=
t;;<br>}<br></div><div><br></div><div>The Perl var &quot;$@&quot; gets what=
 ever the called code might have sent to stderr, e.g.:<br></div><div>$ <a h=
ref=3D"http://test_net_ping.pl" target=3D"_blank">test_net_ping.pl</a><br>A=
::mytest died, saying can&#39;t ping <a href=3D"http://www.google.com" targ=
et=3D"_blank">www.google.com</a> at /usr/local/bin/<a href=3D"http://test_n=
et_ping.pl" target=3D"_blank">test_net_ping.pl</a> line 14.</div><div><br><=
/div><div>Just to note, as a package test, normally, your package A and B w=
ould be separate scripts (say Apingtest.pm and Bpingtest.pm, in the=C2=A0<a=
 class=3D"gmail_plusreply" id=3D"gmail-m_85113940513732727plusReplyChip-0">=
@INC path)</a> and the main code would just have</div><div>#!/usr/bin/perl<=
br></div><div>use Apingtest;</div><div>use Bpingtest;</div><div>use strict;=
</div><div>use warnings;</div><div></div><div>...<br></div></div><br><div c=
lass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, Oct 31, =
2019 at 4:42 AM Maggie Q Roth &lt;<a href=3D"mailto:rothmq@gmail.com" targe=
t=3D"_blank">rothmq@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"=
gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(20=
4,204,204);padding-left:1ex"><div dir=3D"ltr"><div>Hello</div><div><br></di=
v><div>Sorry I am new to perl, I was reading the charter about package.</di=
v><div><br></div><div>I tried to write the code below:</div><div><br></div>=
<div>use strict;<br>use Net::Ping;<br><br>package A;<br><br>sub mytest {<br=
><br>=C2=A0 =C2=A0my $host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&=
gt;new();<br>=C2=A0 =C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0die &quot;can&#=
39;t ping $host&quot;;<br>=C2=A0 =C2=A0}<br>}<br><br>1;<br><br>package B;<b=
r><br>sub mytest {<br><br>=C2=A0 =C2=A0my $host =3D shift;<br>=C2=A0 =C2=A0=
my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =C2=A0unless ($p-&gt;ping($host)) =
{<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0return 0;<br>=C2=A0 =C2=A0}<br>}<br><br>1;<br><br>package main;<br><b=
r>A::mytest(&#39;<a href=3D"http://www.google.com" target=3D"_blank">www.go=
ogle.com</a>&#39;);<br><br>print B::mytest(&#39;<a href=3D"http://www.googl=
e.com" target=3D"_blank">www.google.com</a>&#39;);</div><div><br></div><div=
><br></div><div><br></div><div>When I run it, always get:</div><div>$ perl =
<a href=3D"http://test.pl" target=3D"_blank">test.pl</a><br>can&#39;t ping =
<a href=3D"http://www.google.com" target=3D"_blank">www.google.com</a> at <=
a href=3D"http://test.pl" target=3D"_blank">test.pl</a> line 12.</div><div>=
<br></div><div><br></div><div>Shouldn&#39;t I return die() in package&#39;s=
 method?</div><div>How do I let the caller know what happens when the metho=
d fails to run?<br></div><div><br></div><div>Thanks in advance.</div><div><=
br></div><div>Yours</div><div>Maggie<br></div></div>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr"><br>a<br><=
br>Andy Bach,<br><a href=3D"mailto:afbach@gmail.com" target=3D"_blank">afba=
ch@gmail.com</a><br>608 658-1890 cell<br>608 261-5738 wk</div>
</blockquote></div>

--0000000000006da2eb05963e5715--
0
rothmq
11/1/2019 12:49:31 AM
--000000000000d38575059645b3b2
Content-Type: text/plain; charset="UTF-8"

Hello,

You don't need to setup $!. It is a global variable.

In the documentation I referenced it says:
"When referenced, $! retrieves the current value of the C errno integer
variable. If $! is assigned a numerical value, that value is stored in errno
.. When referenced as a string, $! yields the system error string
corresponding to errno
....
Mnemonic: What just went bang?
"

https://perldoc.perl.org/perlvar.html#Error-Variables


On Fri, 1 Nov 2019 at 00:47, Maggie Q Roth <rothmq@gmail.com> wrote:

> Thank you. how to setup $! in module, and how can I get the $! in caller?
>
> regards.
>
> On Fri, Nov 1, 2019 at 4:56 AM Dermot <paikkos@gmail.com> wrote:
>
>> Hi,
>>
>> You cannot return `die`, die is a fatal exception that causes`test.pl`
>> to exit immediately.
>> One option would be to use warn to emit a warning to STDERR and return to
>> the caller and let them handle the failure. You may as well add the $! to
>> the output so the caller gets a copy of the last error.
>>       ...
>>        $p->close();
>>        warn "can't ping $host: $!";
>>        return 0;
>>       }
>>
>>
>>
>> https://perldoc.perl.org/functions/die.html
>> https://perldoc.perl.org/functions/warn.html
>> https://perldoc.perl.org/perlvar.html#Error-Variables
>>
>> On Thu, 31 Oct 2019 at 09:42, Maggie Q Roth <rothmq@gmail.com> wrote:
>>
>>> Hello
>>>
>>> Sorry I am new to perl, I was reading the charter about package.
>>>
>>> I tried to write the code below:
>>>
>>> use strict;
>>> use Net::Ping;
>>>
>>> package A;
>>>
>>> sub mytest {
>>>
>>>    my $host = shift;
>>>    my $p = Net::Ping->new();
>>>    unless ($p->ping($host)) {
>>>        $p->close();
>>>        die "can't ping $host";
>>>    }
>>> }
>>>
>>> 1;
>>>
>>> package B;
>>>
>>> sub mytest {
>>>
>>>    my $host = shift;
>>>    my $p = Net::Ping->new();
>>>    unless ($p->ping($host)) {
>>>        $p->close();
>>>        return 0;
>>>    }
>>> }
>>>
>>> 1;
>>>
>>> package main;
>>>
>>> A::mytest('www.google.com');
>>>
>>> print B::mytest('www.google.com');
>>>
>>>
>>>
>>> When I run it, always get:
>>> $ perl test.pl
>>> can't ping www.google.com at test.pl line 12.
>>>
>>>
>>> Shouldn't I return die() in package's method?
>>> How do I let the caller know what happens when the method fails to run?
>>>
>>> Thanks in advance.
>>>
>>> Yours
>>> Maggie
>>>
>>

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

<div dir=3D"ltr"><div>Hello,</div><div><br></div><div>You don&#39;t need to=
 setup $!. It is a global variable.</div><div><br></div><div>In the documen=
tation I referenced it says:</div><div>
&quot;<span style=3D"font-family:monospace">When referenced, <code class=3D=
"gmail-inline"><span class=3D"gmail-i">$!</span></code>
 retrieves the current value
of the C <code class=3D"gmail-inline"><span class=3D"gmail-w">errno</span><=
/code>
 integer variable.
If <code class=3D"gmail-inline"><span class=3D"gmail-i">$!</span></code>
 is assigned a numerical value, that value is stored in <code class=3D"gmai=
l-inline"><span class=3D"gmail-w">errno</span></code>
..
When referenced as a string, <code class=3D"gmail-inline"><span class=3D"gm=
ail-i">$!</span></code>
 yields the system error string
corresponding to </span><code class=3D"gmail-inline"><span class=3D"gmail-w=
">errno</span></code></div><div><code class=3D"gmail-inline"><span class=3D=
"gmail-w">...</span></code></div><div><code class=3D"gmail-inline"><span cl=
ass=3D"gmail-w">
Mnemonic: What just went bang?

</span></code></div><div><code class=3D"gmail-inline"><span class=3D"gmail-=
w">&quot;</span></code></div><div><code class=3D"gmail-inline"><span class=
=3D"gmail-w"><br></span></code></div><div><code class=3D"gmail-inline"><spa=
n class=3D"gmail-w"><a href=3D"https://perldoc.perl.org/perlvar.html#Error-=
Variables">https://perldoc.perl.org/perlvar.html#Error-Variables</a></span>=
</code></div><div><code class=3D"gmail-inline"><span class=3D"gmail-w"><br>=
</span></code>

</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_=
attr">On Fri, 1 Nov 2019 at 00:47, Maggie Q Roth &lt;<a href=3D"mailto:roth=
mq@gmail.com">rothmq@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D=
"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(2=
04,204,204);padding-left:1ex"><div dir=3D"ltr"><div>Thank you. how to setup=
 $! in module, and how can I get the $! in caller?</div><div><br></div><div=
>regards.<br></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cl=
ass=3D"gmail_attr">On Fri, Nov 1, 2019 at 4:56 AM Dermot &lt;<a href=3D"mai=
lto:paikkos@gmail.com" target=3D"_blank">paikkos@gmail.com</a>&gt; wrote:<b=
r></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"><div dir=3D"ltr">=
<div>Hi,</div><div><br></div><div>You cannot return `die`, die is a fatal e=
xception that causes`<a href=3D"http://test.pl" target=3D"_blank">test.pl</=
a>` to exit immediately. <br></div><div>One option would be to use warn to =
emit a warning to STDERR and return to the caller and let them handle the f=
ailure. You may as well add the $! to the output so the caller gets a copy =
of the last error.<br></div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ...<br></di=
v><div>
=C2=A0 =C2=A0 =C2=A0=C2=A0 $p-&gt;close(); <br>=C2=A0 =C2=A0 =C2=A0=C2=A0 w=
arn &quot;can&#39;t ping $host: $!&quot;; <br></div><div>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 return 0;</div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br>=
</div><div><br></div><div><br></div><div><br></div><div><a href=3D"https://=
perldoc.perl.org/functions/die.html" target=3D"_blank">https://perldoc.perl=
..org/functions/die.html</a></div><div><a href=3D"https://perldoc.perl.org/f=
unctions/warn.html" target=3D"_blank">https://perldoc.perl.org/functions/wa=
rn.html</a></div><div><a href=3D"https://perldoc.perl.org/perlvar.html#Erro=
r-Variables" target=3D"_blank">https://perldoc.perl.org/perlvar.html#Error-=
Variables</a></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cl=
ass=3D"gmail_attr">On Thu, 31 Oct 2019 at 09:42, Maggie Q Roth &lt;<a href=
=3D"mailto:rothmq@gmail.com" target=3D"_blank">rothmq@gmail.com</a>&gt; wro=
te:<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"><div dir=3D"=
ltr"><div>Hello</div><div><br></div><div>Sorry I am new to perl, I was read=
ing the charter about package.</div><div><br></div><div>I tried to write th=
e code below:</div><div><br></div><div>use strict;<br>use Net::Ping;<br><br=
>package A;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $host =3D shift;<br>=
=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =C2=A0unless ($p-&gt;=
ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;close(); <br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0die &quot;can&#39;t ping $host&quot;;<br>=C2=A0 =C2=A0}<br=
>}<br><br>1;<br><br>package B;<br><br>sub mytest {<br><br>=C2=A0 =C2=A0my $=
host =3D shift;<br>=C2=A0 =C2=A0my $p =3D Net::Ping-&gt;new();<br>=C2=A0 =
=C2=A0unless ($p-&gt;ping($host)) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0$p-&gt;cl=
ose(); <br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return 0;<br>=C2=A0 =C2=A0}<br>}<br><=
br>1;<br><br>package main;<br><br>A::mytest(&#39;<a href=3D"http://www.goog=
le.com" target=3D"_blank">www.google.com</a>&#39;);<br><br>print B::mytest(=
&#39;<a href=3D"http://www.google.com" target=3D"_blank">www.google.com</a>=
&#39;);</div><div><br></div><div><br></div><div><br></div><div>When I run i=
t, always get:</div><div>$ perl <a href=3D"http://test.pl" target=3D"_blank=
">test.pl</a><br>can&#39;t ping <a href=3D"http://www.google.com" target=3D=
"_blank">www.google.com</a> at <a href=3D"http://test.pl" target=3D"_blank"=
>test.pl</a> line 12.</div><div><br></div><div><br></div><div>Shouldn&#39;t=
 I return die() in package&#39;s method?</div><div>How do I let the caller =
know what happens when the method fails to run?<br></div><div><br></div><di=
v>Thanks in advance.</div><div><br></div><div>Yours</div><div>Maggie<br></d=
iv></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>

--000000000000d38575059645b3b2--
0
paikkos
11/1/2019 9:36:27 AM
Reply: