ryo0ka has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I just registered. Thanks in advance! I want to use WWW::Mechanize::get in an inherited class's "get" subroutine, but it does not export any error (like "GETting...") even while the Internet access is out. See my code:
package MyMech; use base qw(WWW::Mechanize); sub new { my $class = shift or die $!; my $self = $class->SUPER::new; return bless $self, $class; } sub get { my $self = shift; $self->SUPER::get(@_); }
I think the get routine should work just like the base class' one, but it never die when it fails GETting. When I call the class and use the get subroutine while the Internet access is out, it just pass through the GETting error without saying anything and $mech->title produces "Uninitialized..." error. It works perfectly when the access is not out. Does anybody know why it happens? Thank you so much!

Replies are listed 'Best First'.
Re: Question about inheritance of WWW::Mechanize
by runrig (Abbot) on May 22, 2013 at 17:35 UTC
    See the docs regarding the autocheck option.

      Thanks for replying guys,

      >See the docs regarding the autocheck option.

      I just checked the doc and modified my code to turn it on, but it still does not work:

      package MyMech; use base qw(WWW::Mechanize); sub new { my $class = shift or die $!; my $self = $class->SUPER::new( autocheck => 1, ); return bless $self, $class; } sub get { my $self = shift or die $!; $self->SUPER::get(@_); }

      >Does the superclass die on error?

      Yeah, it dies when the server is not responding:

      Error GETing (URL): Can't connect to (URL) (Bad hostname) at ***.pl line **

      >Have you tried the $self->success() method?

      Oh God it works... I changed the tester code from:

      my $mech = MyMech->new; $mech->get("http://google.com/"); print $mech->title."\n";

      to:

      my $mech = MyMech->new; $mech->get("http://google.com/"); print $mech->title."\n" if $mech->success;

      Now it displays the error code. Thank you so much...

      But I don't get why it came to work by putting the if statement. In addition now it works only with 'autocheck' even though it didn't work before i put the if statement.

      Anyway... Thank you guys so much, now I can move on :)

Re: Question about inheritance of WWW::Mechanize
by MidLifeXis (Monsignor) on May 22, 2013 at 17:51 UTC

    Does the superclass die on error? I don't remember that behavior (at least not the way it appears you have it configured). Have you tried the $self->success() method?

    --MidLifeXis