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

Currently I have my $mech contructor autosave my cookie jar after I'm done. See below:
my $mech = WWW::Mechanize->new(); my $cookie_jar = HTTP::Cookies->new( autosave => 1, file => "C:\Temp\.cookie.dat"); $mech->cookie_jar($cookie_jar); $mech->agent_alias( 'Windows IE 6' ); $mech->quiet(1);
If I need to change the value of a cookie I'm currently forced to call my $mech destructor, alter the .cookie.dat file, and then start all over by calling my $mech contructor again so it reads the new value from my cookie file.

My question is it possible to do this in memory after the initial run?

Replies are listed 'Best First'.
Re: Mechanize cookie jar update?
by Your Mother (Archbishop) on Apr 04, 2008 at 17:33 UTC

    Check the POD for HTTP::Cookies. It has lots of manipulation hooks. I'm sure mech will save the manipulated object correctly, though I didn't test it. For example-

    print $mech->cookie_jar->as_string, $/; $cookie_jar->scan(sub { print "Cookie from ", $_[4], "\n" }); $cookie_jar->clear(); # Can take args too! # Maybe put a cookie back... $cookie_jar->set_cookie( @your_cookie_data );
      That works perfectly...even better I don't have to set a new cookie I can alter the cookie retrieved from the cookie_jar->as_string method and then pass the variable into the set_cookie method...PERFECT!

      Thanks for that

      Actually I spoke to soon. I was prinitng the contents of the cookie and not from the cookie jar. I was doing somehting like this:
      my $cookie = $mech->cookie_jar->as_string; $cookie =~ s/cookie_for/cookie_bar/; $mech->cookie_jar->set_cookie( $cookie ); print $mech->cookie_jar->as_string;
      This doesn't get updated. How would I update an existing cookie?

      Thanks

        Er, did you read the POD for HTTP::Cookies? I think you'll have to clear that cookie and put it back in the right way; i.e., not as a string but as an array of elements. That said, this turns out to not be so trivial as none of the cookie modules seems to parse strings correctly...? So unless someone has something more terse, this is what I'd do-

        use WWW::Mechanize; use HTTP::Cookies; 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 ); }; # Before our callback. print $cookie_jar->as_string, "\n"; $cookie_jar->scan( $self_enclosed_callback ); # After our callback. print $cookie_jar->as_string, "\n";