[Perl]一段cookie操作的封裝代碼

順手寫下,防止忘掉。
草稿,估計定稿不會是這個 :D
原因: 網站下同時存在多個相同的程序的副本,但是并不想 cookie 相互影響;
代碼如下

###---- cookie By EasunLee 2012-03-11--------###
sub get_cookie_path
{
   if ($esB::INFO->{'COOKIE_USED'} eq 1) { $esB::INFO->{'cookiepath'}  ='';}
   else {
      my $boardurltemp = $esB::INFO->{'BBS_URL'};
             $boardurltemp =~ s/http\:\/\/(\S+?)\/(.*)/\/$2/;
      $esB::INFO->{'cookiepath'} = $boardurltemp;
      $esB::INFO->{'cookiepath'} =~ s/\/$//;
     }
    $esB::INFO->{'cookiepath'} ; 
 }
 
# 設定cookie 
sub set_cookie_str
{
	my ($name,$value,$expires) [email protected]_;
	&get_cookie_path() unless ( defined ( $esB::INFO->{'cookiepath'} ) ) ;
	my $prefix = $esB::INFO->{'cookie_prefix'} ;
	$esB::CGI->cookie(-name => $prefix.$name, -value => $value, -path => $esB::INFO->{'cookiepath'}.'/', -expires => $expires);
}
# 讀取cookie 
sub get_cookie_str
{
	my $name =shift;
	&get_cookie_path() unless ( defined ( $esB::INFO->{'cookiepath'} ) ) ;
	my $prefix = $esB::INFO->{'cookie_prefix'} ;
	$esB::CGI->cookie (-name => $prefix.$name) ;#, -path => $esB::INFO->{'cookiepath'}.'/');
}
#### End cookie subs
Update , 最後定稿:
sub esbcookie
{
my ($self,$name,$value,$expires) [email protected]_;
$self->get_cookie_path() unless ( defined ( $esB::INFO->{'cookiepath'} ) ) ;
my $prefix = $esB::INFO->{'cookie_prefix'} ;
my %infos=() ;
$infos{'-path'} = $esB::INFO->{'cookiepath'}.'/' ;
$infos{'-name'} = $esB::INFO->{'cookie_prefix'}.$name;
$infos{'-value'} = $value if defined($value);
$infos{'-expires'} = $expires if defined($expires);
# $esB::CGI->cookie(-name => $prefix.$name, -value => $value, -path => $esB::INFO->{'cookiepath'}.'/', -expires => $expires);
$esB::CGI->cookie(%infos);
}