SPUG: Trapping LWP exceptions

Adrian Hands aeh at akc.org
Mon Sep 11 12:05:08 CDT 2000


I'd use eval().
It gives you better control over what section of code you're handling.
If you use a __DIE__ handler, keep in mind that it will get invoked for every die,
 even those that are handled by eval().
(Tom Christiensen pointed out this messy behaviour in one of his Perl tutorials at Usenix.)

>>> Sanford Morton <smorton at pobox.com> 09/11 11:32 AM >>>
I need to trap certain LWP exceptions. Is it more appropriate to
handle the __DIE__ signal or use an eval block on $ua->request($req)?

I'm using LWP and a user agent to repeatedly poll a web page looking
for updates:

    while (sleep 900) {
      $resp = $ua->request($req);
      &process_page( $resp->as_string );  

Occasionally the script dies with errors like

   500 (Internal Server Error) Can't connect to www.yahoo.com:80 
   (Transport endpoint is not connected) Client-Date: Mon, 11 Sep 2000
   14:43:28 GMT

I think these are server hiccups, going offline for seconds or a few
minutes, so that the socket fails to connect. The user agent is
die'ing deep within LWP, maybe LWP::Protocol::http or IO::Socket.

I'd like to trap these exceptions, to go to sleep and try again later,
instead of die'ing. What's the best way to do that? Shall I handle the
__DIE__ signal or use an eval block on $ua->request($req)? It's not
clear to me what the difference is between these or which is more
appropriate in this instance.

Thx,
Sandy Morton


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org 
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/ 


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list