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

I'm testing something on our server and would like to add my own valid cookie value to do some negative testing using WWW::Mechanize. Here is what I tried but it doesn't seem to save the new value:
... my $cookie_jar = HTTP::Cookies->new(); $mech->cookie_jar($cookie_jar); my $file = 'valid_cookies.dat'; if (-f $file ) { $cookie_jar->clear; $cookie_jar->load( $file ); } ... # Add new valid cookie my $key="INFO"; my $val="36D93C2F9E23D30EBD4B1F1A6D6E6871A5FA7184058AD675"; my $domain="foo.com"; my $maxage = 100000; $cookie_jar->set_cookie($key, $val, $domain, $maxage); $cookie_jar->save( $file ); $cookie_jar->clear; $cookie_jar->load( $file );
Any idea why it's not saving the new cookie?

Replies are listed 'Best First'.
Re: Adding My Own Cookie Value ?
by lostjimmy (Chaplain) on Sep 10, 2010 at 13:29 UTC
    Some details are missing, but if your program doesn't die, then it appears that the save and load are at least "working" in some way. Looking at the source of HTTP::Cookies shows those calls would indeed die if something were to go wrong with I/O.

    It looks like you aren't passing the correct parameters to set_cookie. According to the docs, it takes $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest, so in your case, $domain is going to be undef. When the module assigns the cookie to its hash, it's going to do something funky: $self->{COOKIES}{$domain}{$path}{$key} = \@array;

    Try this:

    my $version = 0; my $key="INFO"; my $val="36D93C2F9E23D30EBD4B1F1A6D6E6871A5FA7184058AD675"; my $path = "/"; my $domain="foo.com"; my $maxage = 100000; $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, undef, undef, undef, $maxage);
Re: Adding My Own Cookie Value ?
by Anonymous Monk on Sep 11, 2010 at 06:09 UTC
    Any idea why it's not saving the new cookie?

    Your program does not demonstrate that a cookie isn't being saved