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

Good day everyone, I have several questions concerning Mechanize. I’ve tried my best, but a lack of… hmm… time forces me to ask someone smarter. So:

1. Is there a way to close/kill/unload an LWP::UserAgent object? If not, maybe there is a way to reset it? I’ve tried to make a new UA under the same name, tried emptying a cooky_jar, but something stays in memory anyway.

2. Actually I’m trying to make a script that uses Mechanize for logic and sockets for transport, so I use $mech->_update_page($request, $response) which updates all the info, except $mech->uri, for some reason, although $mech->base works fine. Is there a way to update it with a base value?

3. I’m using a “$request = $form->make_request; $mech->_make_request( $request ); $mech->_modify_request( $request );” sequence to take a request from Mechanize and send it to sockets (then I take their response, and return and “_update_page($request, $response)” it back to Mech). Question: is there a “normal” way to catch/borrow a request from standard Mech function ($mech->submit_form( ... ), $mech->follow_link(...), redirect etc.) before it executes it, and then block it?

I’ll appreciate any help :) thanks.

Replies are listed 'Best First'.
Re: Mechanize questions (not very obvious)
by Corion (Patriarch) on Jun 15, 2007 at 20:15 UTC

    Use the Source, Luke

    Regarding 1. - no. A WWW::Mechanize object ISA LWP::UserAgent object, so you can only create a new one if you want to start out with a clean slate.

    Regarding 2. - looking at the source, $mech->uri gets updated from the request URI whenever the response indicates success. So either make it that way or overwrite the value yourself:

    $mech->{uri} = $my_new_uri;

    Regarding 3. - I've used Hook::LexWrap to wrap my code around some WWW::Mechanize functions while writing WWW::Mechanize::Shell.

      Thanks for a hyperspace-speed reply! I'm gonna try your advices, and report the results (if I may).