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 <rot...@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
>

Reply via email to