Skip to content

Commit 33c7ddb

Browse files
Lxgenio
authored andcommitted
Add ignore_discard argument to save()
1 parent b33998b commit 33c7ddb

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Revision history for HTTP-Cookies. The HTTP::Cookies module used to be bundled
22
with the libwww-perl distribution.
33

44
{{$NEXT}}
5+
- Add "ignore_discard" argument to save() methods
56

67
6.07 2019-11-15 18:11:42Z
78
- Fix t/issue32.t on old perl versions (GH#59) (Bernhard M. Wiedemann)

lib/HTTP/Cookies.pm

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,16 @@ sub set_cookie
426426
sub save
427427
{
428428
my $self = shift;
429-
my $file = shift || $self->{'file'} || return;
429+
my %args = (
430+
file => $self->{'file'},
431+
ignore_discard => $self->{'ignore_discard'},
432+
@_ == 1 ? ( file => $_[0] ) : @_
433+
);
434+
Carp::croak('Unexpected argument to save method') if keys %args > 2;
435+
my $file = $args{'file'} || return;
430436
open(my $fh, '>', $file) or die "Can't open $file: $!";
431437
print {$fh} "#LWP-Cookies-1.0\n";
432-
print {$fh} $self->as_string(!$self->{ignore_discard});
438+
print {$fh} $self->as_string(!$args{'ignore_discard'});
433439
close $fh or die "Can't close $file: $!";
434440
1;
435441
}
@@ -783,11 +789,14 @@ attributes like "Comment" and "CommentURL".
783789
784790
=item $cookie_jar->save( $file )
785791
792+
=item $cookie_jar->save( file => $file, ignore_discard => $ignore_discard )
793+
786794
This method file saves the state of the $cookie_jar to a file.
787795
The state can then be restored later using the load() method. If a
788796
filename is not specified we will use the name specified during
789-
construction. If the attribute I<ignore_discard> is set, then we
790-
will even save cookies that are marked to be discarded.
797+
construction. If the $ignore_discard value is true (or not specified,
798+
but attribute I<ignore_discard> was set at cookie jar construction),
799+
then we will even save cookies that are marked to be discarded.
791800
792801
The default is to save a sequence of "Set-Cookie3" lines.
793802
"Set-Cookie3" is a proprietary LWP format, not known to be compatible

lib/HTTP/Cookies/Netscape.pm

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ sub load
3636

3737
sub save
3838
{
39-
my($self, $file) = @_;
40-
$file ||= $self->{'file'} || return;
39+
my $self = shift;
40+
my %args = (
41+
file => $self->{'file'},
42+
ignore_discard => $self->{'ignore_discard'},
43+
@_ == 1 ? ( file => $_[0] ) : @_
44+
);
45+
Carp::croak('Unexpected argument to save method') if keys %args > 2;
46+
my $file = $args{'file'} || return;
4147

4248
open(my $fh, '>', $file) || return;
4349

@@ -53,7 +59,7 @@ EOT
5359
my $now = time - $HTTP::Cookies::EPOCH_OFFSET;
5460
$self->scan(sub {
5561
my ($version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard, $rest) = @_;
56-
return if $discard && !$self->{ignore_discard};
62+
return if $discard && !$args{'ignore_discard'};
5763
$expires = $expires ? $expires - $HTTP::Cookies::EPOCH_OFFSET : 0;
5864
return if $now > $expires;
5965
$secure = $secure ? "TRUE" : "FALSE";

t/cookies.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!perl -w
22

33
use Test;
4-
plan tests => 79, todo => [78, 79];
4+
plan tests => 79;
55

66
use HTTP::Cookies;
77
use HTTP::Request;

0 commit comments

Comments
 (0)