Thread: [htmltmpl] H-T strangeness with CGI-Session
Brought to you by:
samtregar
From: Puneet K. <pk...@ei...> - 2004-03-26 20:24:43
|
Two apologies -- 1. goofy subject line, because I couldn't think of a concise, precise description. 2. might be considered OT, but it seems H-T is a part of this strangeness (and there seems to be no active mailing list for CGI-Session), so... I am using CGI-Session to write to a session file a record of pages visited by a user. The application is made with H-T with loop_context_vars turned on. As a page is visted, its identifying info and a descriptive text is stored in an AoH that is used to build a widget so the user may visit recently seen pages, etc. The problem is thus -- I am storing only the identifying info and a descriptive text. But the AofH in the Session file is showing additional info that looks suspiciously like the loop_context_vars. Here is an excerpt from my Session file -- "rolodex" => [{"ROLODEXVAL" => 3330,"__last__" => 0,"ROLODEXTEXT" => "Puneet Kishor (My Org, Inc.)","__counter__" => undef,"__first__" => 0,"__odd__" => 0,"__inner__" => 0}], See... I only meant to store ROLODEXVAL and ROLODEXTEXT. Where the heck did the __counter__, __first__, etc. come from? Here is the relevant code -- # Create rolodexval my $rolodexval = $res->[0]->{'contact_id'}; # Create rolodextext my $fn= $res->[0]->{'firstname'}; my $ln= $res->[0]->{'lastname'}; my $fullname = ($fn or $ln) ? "$fn $ln" : 'unnamed'; my $org = $res->[0]->{'org'}; my $rolodextext = "$fullname ($org)"; # A hash to hold the current rolodex entry my %rolodexentry = ( ROLODEXVAL => $rolodexval, ROLODEXTEXT => $rolodextext ); # Add the current rolodexentry to the existing rolodex push(@rolodex, \%rolodexentry); # Store the entire AoH in the session $session->param("rolodex", \@rolodex); |
From: <m.s...@gm...> - 2004-03-26 21:15:39
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Puneet, hi list You are on the right list, it's an H::T issue and has nothing to with CGI::Session. I discovered the same some weeks ago. It seems than H::T can contaminate your data structure - for the sake of speed, no copying is done in H::T. If it really bothers you, you might consider using a clone package (try http://search.cpan.org/search?query=clone&mode=all) to first copy your data structure, then let H::T work on the clone and discard it after displaying. Kind regards - Markus -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: Processed by Mailcrypt 3.5.6 and Gnu Privacy Guard <http://www.gnupg.org/> iD8DBQFAZJ1txxUzQSse11ARAt0yAJ40Ttc/pymQYuzsYuSi/yHesvoK8QCeOEe6 OuQqmIfNBzj6FhPNStx9+TU= =1d+T -----END PGP SIGNATURE----- |
From: Puneet K. <pk...@ei...> - 2004-03-26 21:20:08
|
m.s...@gm... wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi Puneet, hi list > > You are on the right list, it's an H::T issue and has nothing to with > CGI::Session. I discovered the same some weeks ago. > > It seems than H::T can contaminate your data structure - for the sake > of speed, no copying is done in H::T. If it really bothers you, you > might consider using a clone package (try > http://search.cpan.org/search?query=clone&mode=all) to first copy > your data structure, then let H::T work on the clone and discard it > after displaying. > Markus, Thanks for the reply. This is very informative, and I will follow your suggestion (and hope the H-T developers will consider this as a problem to be solved). But, my question is -- where on earth did I let H-T "contaminate" my AofH? I am hand-crafting my own AofH -- # A hash to hold the current rolodex entry my %rolodexentry = ( ROLODEXVAL => $rolodexval, ROLODEXTEXT => $rolodextext ); # Add the current rolodexentry to the existing rolodex push(@rolodex, \%rolodexentry); Where, o, where is H-T even touching all this? Thanks, Puneet. |
From: Cees H. <ce...@si...> - 2004-03-26 21:27:06
|
Puneet Kishor wrote: > The problem is thus -- I am storing only the identifying info and a > descriptive text. But the AofH in the Session file is showing additional > info that looks suspiciously like the loop_context_vars. Here is an > excerpt from my Session file -- > > "rolodex" => [{"ROLODEXVAL" => 3330,"__last__" => 0,"ROLODEXTEXT" => > "Puneet Kishor (My Org, Inc.)","__counter__" => undef,"__first__" => > 0,"__odd__" => 0,"__inner__" => 0}], This is probably because you are passing a reference to a data structure to both CGI::Session and HTML::Template. In other words they both get the exact in memory data structure. HTML::Template is adding things to the hashes that are stored in the array ref, and CGI::Session doesn't save it's parameters to the session until it goes out of scope (probably at the end of the request) so it sees the changes that HTML::Template made to the data. There are a couple of things you can do to solve it. - Don't pass the same data structure to both CGI::Session and HTML::Template. Make a copy of the data before passing it by dereferencing the data (see the dclone() method in the Storable module for copying complex data structures if you have a deep data structure). - Or you can make CGI::Session save it's data right away by making it go out of scope, or calling close or flush to sync the data to the session store. Although HTML::Template is causing this problem, I don't think you cna call it a bug. Although many modules will make a copy of the data that is passed in if they intend to make any changes, that can be very expensive if there is a lot of data (which there tends to be when dealing with templates). Cheers, Cees |
From: Puneet K. <pk...@ei...> - 2004-03-26 21:30:28
|
Cees Hek wrote: > Puneet Kishor wrote: > >> The problem is thus -- I am storing only the identifying info and a >> descriptive text. But the AofH in the Session file is showing >> additional info that looks suspiciously like the loop_context_vars. >> Here is an excerpt from my Session file -- >> >> "rolodex" => [{"ROLODEXVAL" => 3330,"__last__" => 0,"ROLODEXTEXT" => >> "Puneet Kishor (My Org, Inc.)","__counter__" => undef,"__first__" => >> 0,"__odd__" => 0,"__inner__" => 0}], > > > This is probably because you are passing a reference to a data structure > to both CGI::Session and HTML::Template. In other words they both get > the exact in memory data structure. HTML::Template is adding things to > the hashes that are stored in the array ref, and CGI::Session doesn't > save it's parameters to the session until it goes out of scope (probably > at the end of the request) so it sees the changes that HTML::Template > made to the data. > > There are a couple of things you can do to solve it. > > - Don't pass the same data structure to both CGI::Session and > HTML::Template. Make a copy of the data before passing it by > dereferencing the data (see the dclone() method in the Storable module > for copying complex data structures if you have a deep data structure). > > - Or you can make CGI::Session save it's data right away by making it go > out of scope, or calling close or flush to sync the data to the session > store. > > Although HTML::Template is causing this problem, I don't think you cna > call it a bug. Although many modules will make a copy of the data that > is passed in if they intend to make any changes, that can be very > expensive if there is a lot of data (which there tends to be when > dealing with templates). > That explains it. You hit it right on the nose, Cees. It is indeed happening because I am sending the same ref to the AofH. Thanks much for you advice as I now know how to solve it. Puneet. |