Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Hashes and Cookies (or more to the point, strings)

by Snuggle (Friar)
on Jul 19, 2001 at 19:16 UTC ( #98103=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I bow to your buddha nature.... I ran into an interesting problem today. I am recieveing information from a cookie that is formatted in the following way:
this looks like a hash to me so I attempted to copy the cookie string to a variable like so:
my $rep= new CGI; my $cookie = $rep->cookie("cookiename");
so now $cookie has one long string with all the data separated by commas. Now, I append ()'s and quotes in all the right places, and I have a string that says something like this:
Setting my hash equal to this never worked right, I suspect that it has something to with the fact that the equal operator does not take the string as a literal and instead put the whole string in the first key. Is there any way to have a hash look at a string in the literal sense, or do I have to split the string into an array and hash it from there? Thanks


--P. J. O'Rourke

Replies are listed 'Best First'.
Re: Hashes and Cookies (or more to the point, strings)
by bikeNomad (Priest) on Jul 19, 2001 at 19:23 UTC
    The problem is that the assignment isn't parsed as it would be in your source text. The fastest thing to do is to split it:

    my $cookie = 'name1,value1,name2,value2'; my %hash = split(',', $cookie);

    Or you can use string eval on your quoted string (but why?):

    my $cookie = '("Name1","Value1","Name2","Value2")'; my %hash = eval $cookie;

    update: But don't do that. As crazyinsomniac points out, it's not a really good idea to eval cookie contents...

by lhoward (Vicar) on Jul 19, 2001 at 19:23 UTC
    Just split into a hash.
    my $rep= new CGI; my $cookie = $rep->cookie("cookiename"); my %kvpairs = split /,/,$cookie;
by mikeB (Friar) on Jul 19, 2001 at 19:32 UTC
    You are correct. You cannot (usefully) assign that string to a hash. What you need to do is split the string into individual items and assign them to the hash.

    Fortunately, an array of key/value pairs can be assigned to a hash with the desired result. This often appears in code as

    %hash = (key1 => value1, key2 => value2);
    where the => operator is a pretty replacement for a comma.

    In your specific case, you want to split() the cookie string on commas, and assign the resulting array to your hash variable. Check the perldocs for details :)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://98103]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-21 03:38 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.