FAQ
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");

#2 test.cgi:

#!/usr/bin/perl -wT
use CGI::Carp qw(fatalsToBrowser);
use CGI ':standard';
use diagnostics;
$xx = new CGI;
%cookhash=cookie('bigcook');
$KK=$cookhash{'KK'};
open (SCRATCH,">>/tmp/scratch") or die "Couldn't open scratch for
appending: $!\n";
chmod 0775,"tmp/scratch";
print $xx->header;
print $xx->start_html(-title=>"test",-bgcolor=>"silver",-
vlink=>"blue"),
$xx->hr, $xx->p,$xx->br;

print $xx->center( $xx->h2("In test.cgi!\n") );
print $xx->center( $xx->h2("\$KK is $KK\n") );
print SCRATCH "In test.cgi!\n";
print SCRATCH "\$KK is $KK\n";

for (keys %{$KK}) {
print SCRATCH "Key: ",$_,"\t";
print SCRATCH "Value from reference: ",${$KK}{$_},"\n";
}

for (keys %{$KK}) {
print $xx->center($xx->h2("Key: ",$_,"\n"),
$xx->h2("value from reference: ",${$KK}{$_},"\n"));
}
print $xx->end_html;


As you can see if you run these two scripts, the dereferencing step
didn't give me back my hash %KK. I used the same synax to dereference
$KK in both programs, but it didn't work for the passed one. Can
anybody help? Thanks in advance.

Mike

Search Discussions

  • Thomas Bätzler at Jan 19, 2010 at 1:50 pm

    mike asked:
    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:
    Assuming that you do traditional CGI, each script invocation starts a new Perl process that exists after executing the script. When that happens, all your data is expunged and the hash reference you passed becomes invalid.

    What you want to do is called session handling and there are oodles of modules for doing that on CPAN.

    HTH,
    Thomas
  • Brad Baxter at Jan 20, 2010 at 1:17 am

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 18, '10 at 7:11p
activeJan 20, '10 at 1:17a
posts3
users3
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase