in reply to Compressing data structures for cookies

Try using Compress::Zlib . Once compressed, you'll probably want to base64 encode them. This will increase the size of your zip by 4/3, but will allow you to store 'binary' data on the cookie.
use Compress::Zlib ; use Storable ; my %hash = &some_data ; my $data = pack( "u*", Compress::Zlib::MemGzip( freeze \%hash) ) ; # now you can do as you please with $data # to unthaw: my $hashref = thaw( Compress::Zlib::MemGunzip( unpack( "u", $data ) ) ) ;

You'll also want to carefully manage the variables you need stored, as cookies tend to allow a minimal amount of space.

Update: GrandFather is absolutely right, though. The best way to store the data is on the server. This method will work, if you're insistent.

Another Update:
I've been thinking about this, and I strongly advise you not to do it.

If you give a variable to a user, that user may choose to unpack, unzip, and thaw it. Then that user could look at your data structure and do something to the cookie ( for example, set $data{'user'} = 'admin' ) and send it back to you. This could be disastrous.

Peace monks,
Bro. Doug :wq

Replies are listed 'Best First'.
Re^2: Compressing data structures for cookies
by gwg (Beadle) on May 09, 2007 at 08:02 UTC

    Thanks for the advice so far. The data that I want to store isn't anything critical - there are five filters that users can set up for searching / viewing data, and it's these settings that I would like to put in the cookie. The cookie data would be compared against the list of valid parameters, so if users did have the inclination to tinker with the settings, the worst they could do would be to set the maximum possible filters, which you can do validly by setting those filters using the cgi.

    One possibility if the data structure is too big might be to have a couple of cookies, as there are some filters which only apply to certain aspects of the program. I don't really want to be setting huge numbers of cookies if I can help it, though.