On 1/18/2010 2:10 PM, mike wrote:
Hello everyone:
I am trying to pass a hash of hashes from one script to another via a
cookie.  I can get the cookie to pass, but when I try to get the inner
hash keys and values using what I think is the correct method, I
cannot get them.  Here are two scripts which should do it, but don't:

#1: pretest.cgi

#!/usr/bin/perl -wT
use CGI ':standard';
my $xx = new CGI;
%KK = (a=>"A",b=>"B");
open (SCRATCH,">/tmp/scratch") or die "Couldn't open scratch for
writing: $!\n";
chmod 0755,"tmp/scratch";
print SCRATCH  "In pretest.cgi.\n";
for (keys %{KK}) {
         print SCRATCH  "Key: ",$_,"\t",
         "Value from reference: ",${KK}{$_},"\n";
}
print SCRATCH "\n\n";
my $kk=\%KK;
print $xx->redirect(-COOKIE=>$kk,-URL=>"test.cgi");

Short answer: you can't do that (Cookies are not Perl), e.g.,

     1  #!/usr/bin/perl -T
     2
     3  use warnings;
     4  use strict;
     5
     6  use CGI ':standard';
     7  my $cgi = new CGI;
     8  my %hash = (a=>"A",b=>"B");
     9  my $href = \%hash;
    10  print $cgi->redirect(-COOKIE=>$href,-URL=>"test.cgi");
    11
    12
    13  __END__
    14  Status: 302 Moved^M
    15  Set-Cookie: HASH(0x93c24)^M
    16  Date: Wed, 20 Jan 2010 01:00:18 GMT^M
    17  Location: test.cgi^M
    18  ^M

You can wrap your hash (hash ref) using the cookie() routine,
e.g.,

     1  #!/usr/bin/perl -T
     2
     3  use warnings;
     4  use strict;
     5
     6  use CGI ':standard';
     7  my $cgi = new CGI;
     8  my %hash = (a=>"A",b=>"B");
     9  my $href = \%hash;
    10  my $cookie = $cgi->cookie(-name=>'myhash',-value=>$href);
    11  print $cgi->redirect(-COOKIE=>$cookie,-URL=>"test.cgi");
    12
    13
    14  __END__
    15  Status: 302 Moved^M
    16  Set-Cookie: myhash=a&A&b&B; path=/^M
    17  Date: Wed, 20 Jan 2010 01:09:35 GMT^M
    18  Location: test.cgi^M
    19  ^M

Then do the opposite at the other end.

See http://search.cpan.org/dist/CGI.pm/lib/CGI.pm#HTTP_COOKIES

--
Brad

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to