I'm having problems with Sessions in CGI:Application
I have a short example site setup here, my code is below
When I navigate I see my main site menu repopulated from session, and normal site contents (links) are displayed.
It works ok accessed as index.pl with no parameters, untill I sent a url to someone with parameters, I think I'm not dealing with sessions properly or something like that.
The apache log says "error execurin runmode, attempt to set parameter 'Main_navigation_menu' with a scalar". Its not getting populated automatically.
Index.pl
I took the advice, cut out all the fat to the minimum to show the problem.
index.pl #!/usr/bin/perl use MyTestApp; my $menu = MyTestApp->new(PARAMS => { cfg_file => 'menu.conf' }); $menu->run();
MyTestApp.pm package MyTestApp; use strict; use warnings; use DBI; use HTML::Template; use CGI::Application::Plugin::Session; use CGI::Application::Plugin::ConfigAuto (qw/cfg/); use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); sub setup { my $self = shift; $self->start_mode('Startup'); $self->tmpl_path('Templates/'); $self->run_modes( 'Startup' => 'Startup', 'Page' => 'BuildPage', ); }; sub cgiapp_init { my $self = shift; $ENV{'ORACLE_HOME'} = $self->cfg('Oracle_Home'); $self->dbh_config( $self->cfg('dsn'),$self->cfg('dbuser'),$self->cfg +('dbpass') ); $self->session_config( CGI_SESSION_OPTIONS => [ "driver:File", +$self->query, {Directory => $self->cfg('Session_Path')} ] ); } sub Startup { my $self = shift; my $q = $self->query(); my $template = $self->load_tmpl('index.tmpl'); $template->param( SiteName => $self->cfg('SiteName') ); my $sth = $self->dbh->prepare("SELECT SUB_PAGE_ID, DESCRIPTION, LI +NK_TYPE FROM MYTABLE WHERE CODE=? ORDER BY ITEM") or die "Can't prepair statement: $self->DBI::errstr"; $sth->execute("1") or die "Can't execute Statement: $DBI::errstr"; + # Main menu Menu is always code 1. my ( $Sub_page_ID, $Description, $link_type, @DBRows ); $sth->bind_columns(\($Sub_page_ID, $Description, $link_type, )); while ($sth->fetch) { if(uc($link_type) eq 'SUBM'){ my $url = "index.pl?rm=Page&code=".$Sub_page_ID; push @DBRows, { LINK=>$url, DESCRIPTION=>$Description }; } } $template->param( Main_navigation_menu => \@DBRows ); $self->session->param( MAINMENU => \@DBRows ); return $template->output(); } sub BuildPage { my $self = shift; my $dbh = $self->dbh(); my $q = $self->query(); my $RequestCode = $q->param("code"); my $RequestItem = $q->param("item"); my $template = $self->load_tmpl('index.tmpl'); $template->param( SiteName => $self->cfg('SiteName') ); my $sth; if ( $RequestItem ){ $sth = $self->dbh->prepare("SELECT SUB_PAGE_ID, LINK_TYPE FROM + MYTABLE WHERE CODE=? AND ITEM=? ORDER BY ITEM") or die "Can't prepai +r statement: $self->DBI::errstr"; $sth->execute($RequestCode, $RequestItem) or die "Can't execut +e Statement: $self->DBI::errstr"; }else{ $sth = $self->dbh->prepare("SELECT SUB_PAGE_ID,LINK_TYPE FROM +MYTABLE WHERE CODE=? ORDER BY ITEM") or die "Can't prepair statement: + $self->DBI::errstr"; $sth->execute($RequestCode) or die "Can't execute Statement: $ +self->DBI::errstr"; } my ( $Sub_page_ID,$link_type ); $sth->bind_columns(\($Sub_page_ID, $link_type)); my @DBRows; while ( $sth->fetch ) { if( uc($link_type) eq 'SUBM' ){ my $url; if( defined($Sub_page_ID) ){ $url = "index.pl?rm=Page&code=".$Sub_page_ID; }else{ $url = "javascript:history.go(-1)"; } push @DBRows, { LINK=>$url }; } $template->param( RESULTS => \@DBRows ); } $template->param( Main_navigation_menu => $self->session->param('M +ain_Main_navigation_menu_menu') ); return $template->output(); } sub teardown { my $self = shift; $self->dbh->disconnect(); $self->session->flush if $self->session_loaded; } 1;
Am I not understanding sessions properly? If not can someone help me to fix this by showing an example of how to use sessions to achieve similar?
How Can I recreate variables from session of from new if session does not pre exist?

In reply to CGI::App + Session value question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.