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

my $ua = LWP::UserAgent->new; $ua ->cookie_jar({ file => $full_path_to_script_dir."/cookies", hide_c +ookie2 => 1, autosave => 1, ignore_discard => 1}); #... do serfing....

I'm working with LWP::UserAgent module, and want to accept all cookies, and don't discard them... The code above works fine...

But under some conditions, while script is working i want to discard all expired cookies and keep only those which are valid.

I know that you can do this with HTTP::Cookies object, like $cookie_jar->clear_temporary_cookies, it works, but how can i access to my cookie_jar object, if it was created like this, on the fly from UserAgent?

Thank you.

Replies are listed 'Best First'.
Re: LWP::UserAgent cookies control
by Corion (Patriarch) on Jul 10, 2009 at 07:12 UTC

    Searching LWP::UserAgent for jar points out $ua->cookie_jar - what problems did you have using this method that you want another?

      my bad, i even didn't think that i can use constructions like $ua->cookie_jar->clear_temporary_cookies and they will work :) Thank you very much!
        Looking at your original snippet, you can grab the output of your call to $ua->cookie_jar(...)
        my $ua = LWP::UserAgent->new; my $jar = $ua->cookie_jar({ file => $full_path_to_script_dir."/cookies", hide_cookie2 => 1, autosave => 1, ignore_discard => 1}); #... do surfing.... #edit jar $jar->clear_temporary_cookies
        If you happen to already have a cookie_jar object or want a non-standard one, you can create a cookie_jar and then add it to your UserAgent. Also, these tips also work for things that subclass LWP::UserAgent, like WWW::Mechanize.
        my $jar = MyCookieJar->new({ file => $path_to_cookies, %options }); # directly at new: my $ua = LWP::UserAgent->new( cookie_jar => $jar); # or later, via cookie_jar $ua->cookie_jar( $jar );