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

Hi all, I'm seeing the following strange behavior that's driving me nuts. I'm doing something standard: in a web application, when someone logs in, I'll load a User object, and store it in a CGI::Session. Next time the session comes back again, I'll simply get the User object from the session. My User object is a simple subclass of Class::DBI (say with columns: name,password, age):
package User; use strict; use base 'Class::DBI'; User->table('user'); User->columns(Primiary=>qw/name/); User->columns(Other=>qw/password age/); 1;
The User object is stored correctly in the session the first time. But after I retrieve it from the session, the call to $user->age() fails, claiming there is no such method. When I do a data dump, $user is given as:
bless({ name=> "test", password=>"password", age=>10},"User") # bless({ name=> "test", password=>"password", age=>10},"User")->age() + gives error.

This only happens within CGI::Session. When used by itself, all is ok:

use User; bless({ name=> "test", password=>"password", age=>10},"User")->age(); # returns 10

Replies are listed 'Best First'.
Re: Strange behavior: Class::DBI with CGI::Application
by mpeters (Chaplain) on Feb 07, 2005 at 23:13 UTC
    Why are you blessing the reference yourself? Class::DBI does a lot of magic behind the scenes so it might be related to that. Why don't you either retrieve() the object or create() it?
      yes, I do use retrieve(), the "bless ..." is a result of Data::Dump after I'm stuck with the problem. Thanks.
Re: Strange behavior: Class::DBI with CGI::Application
by rlb3 (Deacon) on Feb 07, 2005 at 22:50 UTC
    Hello,

    I use you same combination of modules in my web apps. But what I do is only send the primary key into CGI::Session then do User->retrieve() on that value when I need to. Hasn't broke on me yet... :) (Knock on wood).

    rlb3
      I was trying to save one database trip for loading the User object (except the first time). I guess I can use your method also.
        I was trying to save one database trip for loading the User object (except the first time).

        Why? Had you noticed a performance penalty with the extra database trip? Had your users complained? Were you 500'ing requests because your servers were overloaded?

        If the answer to the above questions is no, then don't worry about performance. Performance is something that should be addressed if AND ONLY IF it becomes a problem.

        </soapbox>

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Strange behavior: Class::DBI with CGI::Application
by chb (Deacon) on Feb 08, 2005 at 09:10 UTC
Re: Strange behavior: Class::DBI with CGI::Application
by castaway (Parson) on Feb 08, 2005 at 05:53 UTC
    I doubt thats the problem, but you misspelt "Primary" there.

    C.