use WWW::Mechanize; use HTTP::Cookies; use Data::Dumper; my $mech = WWW::Mechanize->new(); my $cookie_jar = HTTP::Cookies->new( autosave => 1, file => "/your/cookies.txt" ); # $cookie_jar->clear(); $mech->cookie_jar($cookie_jar); $mech->agent_alias('Windows IE 6'); $mech->get("http://www.amazon.com"); my $self_enclosed_callback = sub { my ( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $extra ) = @_; # Remove the currently iterating cookie from the jar. # NB: this might be dangerous! Seems to work though. $cookie_jar->clear( $domain, $path, $key ); # Now change domain, just for example. $domain =~ s/\.com\z/.org/; $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $extra ); }; # Original state. print $cookie_jar->as_string, "\n"; #First solution. foreach my $site (map( values(%{$_}), map( values(%{$_}),values %{$cookie_jar->{'COOKIES'}}) )) { $site->[1] =~ s/no/yes/; $site->[1] =~ s/\d+/time/; $site->[1] =~ s/\d+/blarf/g; } print $cookie_jar->as_string, "\n"; #Second solution. my $cookies = $cookie_jar->{'COOKIES'}; foreach my $site (keys %{$cookies}) { foreach my $path (keys %{$cookies->{$site}}) { foreach my $value (keys %{$cookies->{$site}{$path}}) { $cookies->{$site}{$path}{$value}[1] =~ s/time-blarf-blarf/$site/; $cookies->{$site}{$path}{$value}[1] =~ s/.*l/$path/; $cookies->{$site}{$path}{$value}[1] =~ s/yesskin/$value/g; } } } print $cookie_jar->as_string, "\n";