Skip to content

Commit ff8f923

Browse files
committed
Use lexical file handles and ensure =cut terminates POD. Only cleanup whitespace on lines within functions that filehandles were updated in
1 parent 3c9ef59 commit ff8f923

File tree

3 files changed

+200
-225
lines changed

3 files changed

+200
-225
lines changed

lib/HTTP/Cookies.pm

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,9 @@ sub save
431431
{
432432
my $self = shift;
433433
my $file = shift || $self->{'file'} || return;
434-
local(*FILE);
435-
open(FILE, ">$file") or die "Can't open $file: $!";
436-
print FILE "#LWP-Cookies-1.0\n";
437-
print FILE $self->as_string(!$self->{ignore_discard});
438-
close(FILE);
434+
open(my $fh, '>', $file) or die "Can't open $file: $!";
435+
print {$fh} "#LWP-Cookies-1.0\n";
436+
print {$fh} $self->as_string(!$self->{ignore_discard});
439437
1;
440438
}
441439

@@ -444,43 +442,47 @@ sub load
444442
{
445443
my $self = shift;
446444
my $file = shift || $self->{'file'} || return;
447-
local(*FILE, $_);
445+
448446
local $/ = "\n"; # make sure we got standard record separator
449-
open(FILE, $file) or return;
450-
my $magic = <FILE>;
451-
unless ($magic =~ /^\#LWP-Cookies-(\d+\.\d+)/) {
452-
warn "$file does not seem to contain cookies";
453-
return;
447+
open(my $fh, '<', $file) or return;
448+
449+
# check that we have the proper header
450+
my $magic = <$fh>;
451+
chomp $magic;
452+
unless ($magic =~ /^#LWP-Cookies-\d+\.\d+/) {
453+
warn "$file does not seem to contain cookies";
454+
return;
454455
}
455-
while (<FILE>) {
456-
next unless s/^Set-Cookie3:\s*//;
457-
chomp;
458-
my $cookie;
459-
for $cookie (_split_header_words($_)) {
460-
my($key,$val) = splice(@$cookie, 0, 2);
461-
my %hash;
462-
while (@$cookie) {
463-
my $k = shift @$cookie;
464-
my $v = shift @$cookie;
465-
$hash{$k} = $v;
466-
}
467-
my $version = delete $hash{version};
468-
my $path = delete $hash{path};
469-
my $domain = delete $hash{domain};
470-
my $port = delete $hash{port};
471-
my $expires = str2time(delete $hash{expires});
472-
473-
my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
474-
my $secure = exists $hash{secure}; delete $hash{secure};
475-
my $discard = exists $hash{discard}; delete $hash{discard};
476-
477-
my @array = ($version,$val,$port,
478-
$path_spec,$secure,$expires,$discard);
479-
push(@array, \%hash) if %hash;
480-
$self->{COOKIES}{$domain}{$path}{$key} = \@array;
481-
}
456+
457+
# go through the file
458+
while (my $line = <$fh>) {
459+
chomp $line;
460+
next unless $line =~ s/^Set-Cookie3:\s*//;
461+
my $cookie;
462+
for $cookie (_split_header_words($line)) {
463+
my($key,$val) = splice(@$cookie, 0, 2);
464+
my %hash;
465+
while (@$cookie) {
466+
my $k = shift @$cookie;
467+
my $v = shift @$cookie;
468+
$hash{$k} = $v;
469+
}
470+
my $version = delete $hash{version};
471+
my $path = delete $hash{path};
472+
my $domain = delete $hash{domain};
473+
my $port = delete $hash{port};
474+
my $expires = str2time(delete $hash{expires});
475+
476+
my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
477+
my $secure = exists $hash{secure}; delete $hash{secure};
478+
my $discard = exists $hash{discard}; delete $hash{discard};
479+
480+
my @array = ($version, $val, $port, $path_spec, $secure, $expires,
481+
$discard);
482+
push(@array, \%hash) if %hash;
483+
$self->{COOKIES}{$domain}{$path}{$key} = \@array;
484+
}
482485
}
483-
close(FILE);
484486
1;
485487
}
486488

@@ -825,3 +827,4 @@ Copyright 1997-2002 Gisle Aas
825827
This library is free software; you can redistribute it and/or
826828
modify it under the same terms as Perl itself.
827829
830+
=cut

0 commit comments

Comments
 (0)