barrycarlyon has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks

After seeking advice in the CB on baking cookies, i need some information eating them (ie fetching values), as the CPAN page is'nt really clear

I can fecth the value from a cookie which has only one value, but I am now using a cookie which has two values

$admincookie = new CGI::Cookie(-name=>'LSRfm_admin', -value=>{ username => barrycarlyon, password => md5_hex_value } );

Using the fetch script I wrote which finds the cookie of the given name and returns the value, i get:

#fetch the (cgi)type cookie sub _cookie { #read in the lsrfm cookie to test my $self = shift; # my $cgi = CGI->new; # my $type = $cgi->param('type'); my $type = shift; #lets check to see if the lsrfm cookie exits #fecth the domains cookie my %cookies = fetch CGI::Cookie; my $cookies; my $lsrfm_cookie; if (!%cookies) { #a domain cookie doesnt exist #leave as blank } else { #a domain cookie exists #fetch the type value my $n = 0; my $this_cookie; my @cookies = (keys %cookies); foreach (keys %cookies) { $this_cookie = $cookies[$n]; if ($this_cookie eq ('LSRfm'.$type)) { $lsrfm_cookie = $cookies{'LSRfm'.$type}->value; } $n ++; } } return $lsrfm_cookie; }
Using: my $cookie = LSRfm::Application::Cookie->_cookie('_admin'); to fetch the cookie, I dont think you need the Cookie Create Script it is the same as that in CPAN, except the relevant items of date are swapped for $whatever, fetched using my $whatever = shift;, but I can add it if people want it

{ username => barrycarlyon, password => 025a38ae929ace0637824f7728da371d }

So i would like to put the username into $username and the password md5_hex hash into $password

Hope you can help

Bazza
Barry Carlyon barry@barrycarlyon.co.uk

Replies are listed 'Best First'.
Re: Baking Cookies and Eating them
by merlyn (Sage) on Sep 30, 2006 at 23:38 UTC
Re: Baking Cookies and Eating them
by grep (Monsignor) on Sep 30, 2006 at 21:10 UTC
    You question is not worded very clearly. So I am going to assume that from this statement, 'I can fecth the value from a cookie which has only one value, but I am now using a cookie which has two values' that you are asking how to retrieve multi-valued cookies. Straight from the CGI::Cookie docs:
    value() Get or set the cookie's value. Example: $value = $c->value; @new_value = $c->value(['a','b','c','d']); value() is context sensitive. In a list context it will return the + current value of the cookie as an array. In a scalar context it will + return the first value of a multivalued cookie.
    So you'll need retrieve the data as a hash (array) - the same way you put it in.

    UNTESTED

    my %cookie_value = $cookies{'LSRfm'.$type}->value;


    grep
    Mynd you, mønk bites Kan be pretti nasti...
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Baking Cookies and Eating them
by Cody Pendant (Prior) on Sep 30, 2006 at 22:45 UTC
    Call me crazy, but why do you need a cookie with two values? Why not just two cookies? And if you really want to put two values in there, just make them a string with a useful separator and split() the string to make it back into a hash?


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print