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.
In reply to Re^2: LWP handlers examples?
by vitoco
in thread LWP handlers examples?
by vitoco
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |