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

I am probably overlooking something here:
I am using CGI::Application framework and CGI::Session
I need to set some init values for my session variables. These values will ofcourse change over the session. How to I prevent the values from being initiated everytime the user reloads the page?

Is there a way to init my variables and then if they change they are changed but if not, they are left alone?

here are some code to achieve the above:
this code works but there must be a better way of doing this!

# application object my $self = shift; # get cgi query object my $q = $self->query(); my %post = $q->Vars; # if entries_per_page is not empty: set value, if it's empty and sessi +on value is empty: init session value if($post{'entries_per_page'}){ $self->session->param('entries_per_page' => $post{'entries_per_pag +e'}); } elsif( (!$post{'entries_per_page'}) && (!$self->session->param('entr +ies_per_page')) ) { $self->session->param('entries_per_page' => 10); }

2) In the CGI::Application framework is all the functions such as: setup(), cgiapp_init(), teardown() etc. executed every time the application is run? Or is there anywhere I can init stuff just once?

3) In my run_modes function:

$self->run_modes( 'static_page' => 'printStaticPage', 'AUTOLOAD' => 'showErrorPage' );
is there any way I can pass in parameters to the called function? for example a static page argument on which page to print to the printStaticPage function?

I don't want to use code like:

# page if($post{'page'} eq "about"){ # print page about } elsif($post{'page'} eq "purchase"){ # print page purchase } elsif($post{'page'} eq "contact"){ # print page contact } else { # print deault page }

Replies are listed 'Best First'.
Re: init session variables
by PodMaster (Abbot) on Feb 08, 2005 at 07:34 UTC
    In the CGI::Application framework is all the functions such as: setup(), cgiapp_init(), teardown() etc. executed every time the application is run? Or is there anywhere I can init stuff just once?
    See CGI::Application::Loop.
    is there any way I can pass in parameters to the called function? for example a static page argument on which page to print to the printStaticPage function?
    CGI::Application does have a param method ...

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: init session variables
by ikegami (Patriarch) on Feb 08, 2005 at 07:14 UTC

    At the very least, you could do this:

    if (!$self->session->param('initialized')) { $self->session->param(initialized => 1); # Initialization code. $self->session->param(entries_per_page => 10); } # Overrides. $self->session->param(entries_per_page => $post{entries_per_page}) if defined $post{entries_per_page};