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

Can someone point me where can I get some working examples on the use of handlers of LWP:UserAgent? From the docs:

$ua->remove_handler( $phase, %matchspec ) $ua->remove_handler( $phase, %matchspec )

I want to understand what are %matchspec in this context and how to successfully remove the ones I've added.

I'm using Mechanize to read some HTML pages and get some links to binary files. To trace the binary downloads, I added a simple handler using:

  $mech->add_handler(response_header => \&mytrace);

but I couldn't successfully remove it after the $mech->get() by analyzing the hashes from the following code after de add, in the middle and at the remove:

  $mech->handlers('response_header', $mech->response())

If required, I can post some code and it's output...

Replies are listed 'Best First'.
Re: LWP handlers examples?
by ikegami (Patriarch) on Aug 12, 2009 at 18:02 UTC
    I didn't have any problems.
    use strict; use warnings; use LWP::UserAgent qw( ); my $ua = LWP::UserAgent->new(); my $url = 'http://www.google.com/'; print("Added:\n"); $ua->add_handler(response_header => sub { print "HANDLER\n"; }); print($ua->get($url)->status_line(), "\n"); print("Removed:\n"); $ua->remove_handler('response_header'); print($ua->get($url)->status_line(), "\n");
    Added: HANDLER HANDLER 200 OK Removed: 200 OK

      It's strange that the message is printed twice since it is being called once!!! Why?

      I added some code lines to trace the handlers at any moment, but as $ua->handlers() require an HTTP::Response object, I had to split the $ua->get() calls, and an extra call must be added at the beginning, giving:

      #!perl use strict; use warnings; use LWP::UserAgent qw( ); my $ua = LWP::UserAgent->new(); my $url = 'http://www.google.com/'; my $resp; $resp = $ua->get($url); print($resp->status_line(), "\n"); show(1); print("Added:\n"); $ua->add_handler(response_header => sub { print "HANDLER\n"; }); show(2); $resp = $ua->get($url); print($resp->status_line(), "\n"); show(3); print("Removed:\n"); $ua->remove_handler('response_header'); show(4); $resp = $ua->get($url); print($resp->status_line(), "\n"); show(5); sub show { print(join(" ", @_, $ua->handlers('response_header', $resp)), "\n"); }

      Output:

      200 OK 1 HASH(0x183657c) Added: 2 HASH(0x183657c) HASH(0x1b79944) HANDLER HANDLER 200 OK 3 HASH(0x183657c) HASH(0x1b79944) Removed: 4 200 OK 5

      It seems that there is a default handler installed, and I would expect that trace lines 4 and 5 to show the same hash as line 1, not an empty list of handlers.

      BTW, I changed your original test program to use a WWW::Mechanize object, and got the same output as you.

        After a bit of RTFM-ing, reading the source and experimenting, I think the %matchspec is supposed to be used something like this:

        use LWP::UserAgent qw( ); my $ua = LWP::UserAgent->new(); my $url = 'http://www.google.com/'; my $resp; $resp = $ua->get($url); print($resp->status_line(), "\n"); show(1); print("Added:\n"); $ua->add_handler( response_header => sub { print "HANDLER\n"; }, owner + => "myfunc"); show(2); $resp = $ua->get($url); print($resp->status_line(), "\n"); show(3); print("Removed:\n"); $ua->remove_handler('response_header', owner => "myfunc"); show(4); $resp = $ua->get($url); print($resp->status_line(), "\n"); show(5); sub show { print(join(" ", @_, $ua->handlers('response_header', $resp)), "\n"); } __END__ 200 OK 1 HASH(0x8c2440) Added: 2 HASH(0x8c2440) HASH(0x93b0e0) HANDLER HANDLER 200 OK 3 HASH(0x8c2440) HASH(0x93b0e0) Removed: 4 HASH(0x8c2440) 200 OK 5 HASH(0x8c2440)

        where "myfunc" is some self-chosen name that you use to identify the callback for removal.

        (When you dump the hashes, you can see that the one associated with the internal handler (HASH(0x8c2440)) has owner => 'LWP::UserAgent::parse_head')

        As you can see, the internal callback remains intact this way.

        It's strange that the message is printed twice since it is being called once!!! Why?

        I suspect two responses were received, the first being a redirect.

Re: LWP handlers examples?
by Anonymous Monk on Aug 13, 2009 at 03:54 UTC