Subject: | Subclass the LWP::UA clone() method in WWW::Mechanize |
Someone on a mailing list wanted to simulate pop-up windows with a clone
of the Mech, but noticed that clone() didn't copy the cookie jar.
I found out that the current WWW::Mechanize clone method is inherited
from LWP::UA, and here is a patch so that WWW::Mechnanize clones share
the cookie jar (but start with a fresh history).
More tests are probably needed, especially after a browsing session.
Subject: | clone.patch |
Index: lib/WWW/Mechanize.pm
===================================================================
--- lib/WWW/Mechanize.pm (révision 3706)
+++ lib/WWW/Mechanize.pm (copie de travail)
@@ -1628,6 +1628,30 @@
=head1 OVERRIDDEN LWP::UserAgent METHODS
+=head2 $mech->clone()
+
+An overloaded version of C<clone()> in L<LWP::UserAgent>.
+This method return a clone of the Mech object.
+
+The clone and the original will share the same L<HTTP::Cookies>
+object, but the clone will start with a blank history.
+
+=cut
+
+sub clone {
+ my $self = shift;
+
+ my $clone = $self->SUPER::clone();
+
+ # cookie_jar and conn_cache are not copied by LWP::UserAgent::clone
+ $clone->{cookie_jar} = $self->{cookie_jar};
+
+ # do not copy the page stack
+ $clone->{page_stack} = [];
+
+ return $clone;
+}
+
=head2 $mech->redirect_ok()
An overloaded version of C<redirect_ok()> in L<LWP::UserAgent>.
@@ -2078,8 +2102,6 @@
$self->{page_stack} = [];
my $clone = $self->clone;
- # Huh, LWP::UserAgent->clone() ditches cookie_jar? Copy it over now.
- $clone->{cookie_jar} = $self->cookie_jar;
push( @$save_stack, $clone );
if ( $self->stack_depth > 0 ) {
Index: t/clone.t
===================================================================
--- t/clone.t (révision 3706)
+++ t/clone.t (copie de travail)
@@ -1,7 +1,7 @@
#!perl -Tw
use strict;
-use Test::More tests => 3;
+use Test::More tests => 4;
BEGIN {
use_ok( 'WWW::Mechanize' );
@@ -12,3 +12,5 @@
my $clone = $mech->clone();
isa_ok( $clone, 'WWW::Mechanize' );
+
+is_deeply( $clone, $mech, 'Object and clone identical' );