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 18: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 18: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?