Hi everyone, I'm writing a small module for creating/managing sessions using MLDBM module. I have a method for adding a new pair of key-value to the session, but for some reason this change is not reflected on disk, so next time I open the session, that pair of key-value is not there. What am I doing wrong? The relevant code is below. I would appreciate any input. Also, if something in the code doesn't make sense please tell me as I'm only learning...
# # constructor # sub new { my $class = shift; my (%args) = @_; my $sessions = {}; my $session = {}; # settings / defaults my $session_id = $args{SESSION_ID} || ""; my $full_db_path = $args{FILE} || die "session db filename not def +ined"; my $timeout = ( $args{TIMEOUT} || $DEFAULT_TIMEOUT ) * 60; # and c +onvert to seconds $sync_dbm_obj = tie(%{$sessions}, 'MLDBM::Sync', $full_db_path, O_ +CREAT|O_RDWR, 0640) or die "could not create / open db file $full_db_path: $!"; for ( $session_id ) { if( $session_id ) { $sync_dbm_obj->Lock; my $aref = $sessions->{$session_id}; if( $aref ) { my $timestamp = $aref->{timestamp}; if( time() - $timestamp > $timeout ) { # session expired and has to be removed delete( $sessions->{$session_id} ); $sync_dbm_obj->UnLock; $session_id = ""; redo; } else { # update last access timestamp $aref->{timestamp} = time(); $aref->{a_session}->{_timestamp} = time(); #debug +only $sessions->{$session_id} = $aref; $sync_dbm_obj->UnLock; $session = $aref->{a_session}; } } else { $sync_dbm_obj->UnLock; $session_id = ""; redo; } } else { $session_id = create_session_id(); $sync_dbm_obj->Lock; $sessions->{$session_id} = { a_session => { _session_id => +$session_id, _timestamp => +time() }, # debug only timestamp => time() }; $session = $sessions->{$session_id}->{a_session}; $sync_dbm_obj->UnLock; } } return bless $session, $class; } # # # sub add_key { my $self = shift; my ($key, $value) = @_; $sync_dbm_obj->Lock; my $aref = $self; $aref->{$key} = $value; $self = $aref; $sync_dbm_obj->UnLock; } # # general destructor # sub DESTROY { my $self = shift; undef $sync_dbm_obj; untie(%{$self}); };
Here's how I'm calling the method:
my $session = MLDBMSession->new(FILE => "c:\\perl_user\sessions.db", S +ESSION_ID => $session_id, TIMEOUT => 100); $session->add_key( value1 => 20 );
Here's what I get when I do print Dumper $session and then print Dumper everything in sessions.db file:
$VAR1 = bless( { '_timestamp' => 1034001952, 'value1' => 20, '_session_id' => '1ca9437eec103d4d533dc22b5b2c45ab' }, 'MLDBMSession' ); Time now: Mon Oct 7 10:45:52 2002 $VAR1 = { 'a_session' => { '_timestamp' => 1034001952, '_session_id' => '1ca9437eec103d4d533dc22b5 +b2c45ab' }, 'timestamp' => 1034001952 }; Created/Last Accessed: Mon Oct 7 10:45:52 2002
Thank you, Alex

In reply to Problem storing value in persistent hash with MLDBM by relax99

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.