Fellow monks, I seek your guidance - I am having a great deal of trouble trying to sort out some problems which I am experiencing with tied hashes within the Apache::Session and CGI::Application frameworks. The specific problem is that information stored in the payments hash key, created in the default input_display run-mode, not being available in subsequent run modes - I am at a loss to explain this, although I am sure it must be something simple that I have overlooked.

The code in question follows:

#!/usr/bin/perl use strict; my $app = Self->new( 'PARAMS' => { 'mode_param' => 'action', 'run_modes' => { 'AUTOLOAD' => 'input_display', 'display' => 'input_display', 'finalise' => 'input_finalise', 'update' => 'input_update' }, 'start_mode' => 'display' } ); $app->run; exit 0; package Self; BEGIN { unshift @INC, '.'; } use Apache::Session::DB_File; use Business::CardAccess; use CGI::Carp; use Data::Dumper; use Template; use base 'CGI::Application'; use strict; sub setup { my $self = shift; $self->run_modes( $self->param('run_modes') || { 'start' => 'dump_ +html' } ); $self->mode_param( $self->param('mode_param') || 'rm' ); $self->start_mode( $self->param('start_mode') || 'start' ); $self->param( 'template' => Template->new({ 'INCLUDE_PATH' => $sel +f->param('tmpl_path') || '../templates' }) ); } sub input_display { my $self = shift; my $cgi = $self->query; my %session; eval { my $session_id = $cgi->cookie( -name => 'ecom', -path => '/' ) +; tie %session, 'Apache::Session::DB_File', $session_id, { 'File +Name' => '.sessions' }; }; tie %session, 'Apache::Session::DB_File', undef, { 'FileName' => ' +.sessions' } if $@; $session{'payments'} ||= [ { 'index' => 0, 'description' => 'One-Time Payment', 'amount' => 0 } ]; $self->header_props( -cookie => $cgi->cookie( -name => 'ecom', -path => '/', -value => $session{'_session_id'} ) ); my $html = ''; $self->param('template')->process('display.tt2', \%session, \$html +); untie %session; return $html; } sub input_update { my $self = shift; my $cgi = $self->query; my %session; eval { my $session_id = $cgi->cookie( -name => 'ecom', -path => '/' ) +; tie %session, 'Apache::Session::DB_File', $session_id, { 'File +Name' => '.sessions' }; }; croak( 'Cannot retrieve user session ID from client-side cookie' ) + if $@; $session{'payments'} ||= []; my @payments = @{$session{'payments'}}; foreach (0..$#payments) { $cgi->param('description_'.$_, $1) if $cgi->param('description +_'.$_) =~ /^([\w\-\ ]+)$/; $cgi->param('amount_'.$_, $1) if $cgi->param('amount_'.$_) =~ +/^\$?(\d+(?:|\.\d{1,2}))$/; splice( @payments, $_, 1, { 'index' => $_, 'description' => $cgi->param('description_'.$_), 'amount' => $cgi->param('amount_'.$_) } ); splice(@payments, $_, 1) if defined $cgi->param('delete_'.$_); } $session{'payments'} = \@payments; my $html = ''; $self->param('template')->process('display.tt2', \%session, \$html +); untie %session; return $html; } 1; __END__

 

Edited: ~Wed Jul 31 23:11:29 2002 (GMT) by footpad: Added <readmore> tag, per Consideration.


In reply to Persistency issues with tied hashes by rob_au

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.