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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.