Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

CGI::Application::Session and Inheritance

by gman (Friar)
on Apr 19, 2005 at 13:31 UTC ( [id://449350]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Greetings,

I have a problem using CGI::Application::Session with my own packages. I realize that the answer will probably come down to a basic ignorance on my part of OOP, and have been reading perltooc, perltoot and some nodes here on PM. Here is some of my code. It produces no errors, either in the logs or on the browser, but does not work either.

Thanks in advance,


In Index.pm
package myTest::Index; use strict; use DBI; use base 'CGI::Application'; use CGI::Application::Session; use Data::Dumper; .... .... sub home { # $self->start_mode('home'); my $self = shift; my $tmpl = $self->load_tmpl('index.tmpl'); my $session = $self->session; my $data = $session->param_hashref(); use myTest::module1; my $moduleRef = new myTest::module1; $moduleRef->add_item(); # session changes in myTest::module1 +here are not reflected in # main session $session->param("key1","value1"); # Works as expected $tmpl->param(DATA => Dumper($data)); return $tmpl->output; }

From module1.pm
package myTest::module1; use base qw/myTest::Index/; use strict; use warnings; sub add_item { my $self = shift; my @array1 = ('data1','data2'); my $cart = $self->session->param('_CART') || []; push @{ $cart }, @array1; $self->session->param('_CART', $cart); return; } 1;

Replies are listed 'Best First'.
Re: CGI::Application::Session and Inheritance
by dragonchild (Archbishop) on Apr 19, 2005 at 14:02 UTC
    What does your cgi script look like? I suspect you should be creating an instance of myTest::module1 instead of myTest::Index and changing home() to look something like:
    sub home { # $self->start_mode('home'); my $self = shift; my $tmpl = $self->load_tmpl('index.tmpl'); my $session = $self->session; my $data = $session->param_hashref(); $self->add_item(); $session->param("key1","value1"); # Works as expected $tmpl->param(DATA => Dumper($data)); return $tmpl->output; }
      Here is my CGI script. pretty much taken right out of CGI::Application


      #!/usr/bin/perl -w use myTest::Index; use strict; my $web = myTest::Index->new(); $web->run();
        Create an instance of myTest::module1, not myTest::Index, and make the changes to home() that I gave you earlier. Your app will work now.
Re: CGI::Application::Session and Inheritance
by Joost (Canon) on Apr 19, 2005 at 14:07 UTC
    Since your $moduleRef object is not the same as your actual application object, chances are that they will each create their own session (and configuration, and do all kinds of other stuff you'd really only want to do once).

    In general, basic CGI::Application systems only have one controller (i.e. one instance of a CGI::Application-derived object).

    Is there any reason you don't run the myTest::module1 as the application's class instead of creating a new instance of it somewhere during a handler?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://449350]
Approved by moot
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.