in reply to Storing a Hash inside a CGI::Session Param?

What you are talking about is serializing data ie take a hash, turn it into a string for storage/transport, reconstitute the hash. I tend to use Storable but here are lots of other serialisation options. See FreezeThaw, Data::Serializer, Data::Dumper and friends to name a few.

However CGI::Session will handle serialisation for you (you can select the Module to use for it as well). All you need to do is save a hash_ref, then you can restore a hash ref

use CGI::Session; use Data::Dumper; my %hash = ( foo => 1, bar => 2 ); my $session = new CGI::Session("driver:File", undef, {Directory=>'/tem +p'}); $session->param(-name=>'hash', -value=>\%hash); my $hash_ref = $session->param('hash'); print Dumper $hash_ref,

cheers

tachyon

Replies are listed 'Best First'.
Re^2: Storying a Hash inside a CGI::Session Param?
by Stenyj (Beadle) on Jul 04, 2004 at 01:28 UTC
    Actually, I'm using session so that the user never has the data.

    I store the data in the session (which the client can't access/modify) and then reference the session via a cookie stored in the client's browser (which is standard practice, I believe).

    I'm trying to set it up so that the session can contain data that will otherwise require me to do a number of different database queries for every single one of my scripts.

    It comes down to:
    - Either I do it via sessions, if possible
    OR
    - Every script of mine will have to do a number of db queries every time they're executed.

    I'm assuming sessions is a more efficient way of doing it, if I can store the necessary data.

    Theoretically I could store it into the session using several dozen params, but for simplicity in the scripting, I was hoping to store it into the session in the form of a hash, so that I don't have to have 20-30 lines of param retrieval for every script. And it would also allow for much simpler coding since a lot of my scripts react differently, based on the data passed to them from the session.

    One line making reference to a hash within a hash...
    ie.
    %access = $session->param("access");
    $loggedIn = $access{sports}{soccer};
    vs.
    taking in the param that was broken into an array, rebuilding the hash from the arrah contents, then doing that line.

    is a lot easier to manage, and a lot nicer coding.

    I'm assuming, from your response, though that it's not possible to store a param in a manner which it will easily be retrievable as a hash.

    I suppose I'll just have to write a subroutine in a library and then call it at the beginning of all of my scripts then.

    *sigh*

    Thanks for the info. I appreciate the response, even though I'm dissapointed to hear the answer... heh heh. I can dream, can't I? LOL


    Stenyj