Hi, I have just started learning OOPerl a couple months ago and I'm still a little bit confused as to how some stuff works. Let's say I set up a calendar class that is tied to a file on disk using MLDBM::Sync. I need to use MLDBM::Sync, because I need concurrent access to this file by multiple users (through a web interface). Below is how the beginning of my calendar class looks like including the constructor:

package Calendar; # class variables my $sync_dbm_obj; # constructor sub new { my $class = shift; my %args = @_; my $calendar = {}; $sync_dbm_obj = tie(%{$calendar}, 'MLDBM::Sync', "$args{DIR}\\$arg +s{year}.db", O_CREAT|O_RDWR, 0640) or die "can't open tie to $args{year}.db: $!"; # if just created, initialize hash with default values if( !$calendar->{months} ) { $sync_dbm_obj->Lock; $calendar->{year} = $args{year}; $calendar->{months} = {}; my $aref = $calendar->{months}; foreach my $month (1..12) { $aref->{$month} = _init_days($calendar->{year}, $month); } $calendar->{months} = $aref; $sync_dbm_obj->UnLock; } return bless $calendar, $class; }

Now, if I need to create two calendar objects I would do something like:

my $calendar1 = Calendar->new(year => "2002", DIR => "c:\a_dir"); my $calendar2 = Calendar->new(year => "2003", DIR => "c:\a_dir");

Once I create the second object, the $sync_dbm_obj class variable points to the second file (2003.db in this example), so if I call any methods for $calendar1 object, file 2002.db is not going to be locked. And if I were to destroy $calendar1 by either letting it go out of scope or calling undef $calendar1, then $sync_dbm_obj becomes undefined and methods for $calendar2 object stop functioning, because they're trying to lock 2003.db using $sync_dbm_obj, but it is now undefined.

Can you think of how to fix this or suggest a nice workaround? One workaround would, of course, be to use just one calendar object at a time, but there's a part in my program where I need to have two calendar objects active at the same time. And I do need fine-grained persistant and syncronized access to files offered by MLDBM and MLDBM::Sync to prevent concurrency issues. Any input is welcome!


In reply to Need a workaround: Class variables with MLDBM::Sync 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.