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,
| [reply] [d/l] |
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
| [reply] |
The correct syntax is this:
$session->param("access", \%access);
Please read the tutorial for more. | [reply] [d/l] |
Forgive me for asking, but where can I find the tutorial on it?
| [reply] |
You know how to use perldoc, right? Just run perldoc CGI::Session::Tutorial on the command line.
This particular information is in the section headed STORING DATA IN THE SESSION.
HTH HAND.
| [reply] [d/l] |
I have also tried to do it your way and it is very hard...Read this and you'll have it simpler. Perl is not so difficult as it looks. But you need to know first all the basics. So many tricks, tokens, ways of doing things! After reading a good perl book (ask others), suddenly, everything makes sense! At last! Next step: you love perl forever :) and code everything as a snap.
.{\('v')/}
_`(___)' __________________________
| [reply] |
I'll start going through it as I have time.
Thx for the link.
For now I'll have to put development on hold, until I can figure out why this isn't working.
Thx again for the feedback guys.
Stenyj
| [reply] |