@@ -431,11 +431,9 @@ sub save
431
431
{
432
432
my $self = shift ;
433
433
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 });
439
437
1;
440
438
}
441
439
@@ -444,43 +442,47 @@ sub load
444
442
{
445
443
my $self = shift ;
446
444
my $file = shift || $self -> {' file' } || return ;
447
- local (*FILE, $_ );
445
+
448
446
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 ;
454
455
}
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
+ }
482
485
}
483
- close (FILE);
484
486
1;
485
487
}
486
488
@@ -825,3 +827,4 @@ Copyright 1997-2002 Gisle Aas
825
827
This library is free software; you can redistribute it and/or
826
828
modify it under the same terms as Perl itself.
827
829
830
+ =cut
0 commit comments