A friend and I were reflecting on how we almost always call 
LWP::UserAgent's get() method with a following line that says die...unless 
$response->is_success;.  So I thought, sounds like a job for LWP-SUBCLASS MAN!

So here, either drop this into your own programs, or make it a file 
LWP/Croaky.pm in your @INC ...

{
   package LWP::Croaky;
   use Carp();
   use base qw(LWP::UserAgent);
   sub request {
     my $self = shift;
     my $resp = $self->SUPER::request(@_);
     Carp::croak(join "",
       "Couldn't get ", $resp->request->uri, "\n ",
       $resp->status_line,
       "\n Aborting"
      ) unless $resp->is_success;
     return $resp;
   }
   1;
}



Example usage:

my $browser = LWP::Croaky->new;
my $resp = $browser->get('http://www.perl.int/');
....proces $resp...

That'll fail with a die, because it can't get that URL.


I don't have any examples of subclassing in /Perl and LWP/, so I figure 
this should hold you all!

--
Sean M. Burke    http://www.spinn.net/~sburke/

Reply via email to