Listing 3.
the PageSession module
Lincold Stein and Doug MacEachern

"Stately Scripting with mod_perl"
The Perl Journal, Spring 1998
 
0 package PageSession;
1
2 use IPC::Shareable;
3 use vars qw($NEXTID $MAXSESSIONS %SESSIONS);
4 $MAX_SESSIONS = 100;
5
6 tie $NEXTID, IPC::Shareable,'S000',{create=>1,mode=>0600};
7 tie %SESSIONS,IPC::Shareable,'S001',{create=>1,mode=>0600};
8
9 $NEXTID = 0 if $NEXTID eq '';
10
11 # Find a new ID to use by cycling through a
12 # a list. In a real application, the ID should
13 # be unique and kept in a most-frequently-used cache.
14 sub new {
15     my($package) = @_;
16     tied($NEXTID)->shlock;
17     $NEXTID=0 if $NEXTID > $MAX_SESSIONS;
18     my $self = bless {
19         name => '',
20         article => '',
21         page => 0,
22         id => $NEXTID++
23     },$package;
24     tied($NEXTID)->shunlock;
25     return $self;
26 }
27
28 sub fetch {
29     my ($package,$id) = @_;
30     return undef if $id eq '';
31     # Storeable makes this a PageSession object
32     return $SESSIONS{$id};
33 }
34
35 sub save {
36     my $self = shift;
37     # store the object
38     tied(%SESSIONS)->shlock;
39     $SESSIONS{$self->{id}} = $self;
40     tied(%SESSIONS)->shunlock;
41 }
42
43 sub id { $_[0]->{id}; }
44 sub name { $_[0]->{name} = $_[1] if defined($_[1]);
                            $_[0]->{name}; }
45 sub article {
46     $_[0]->{article} = $_[1] if defined($_[1]);
47     $_[0]->{article};
48 }
49 sub page {
50     $_[0]->{page} = $_[1] if defined($_[1]);
51     $_[0]->{page} = 0 if $_[0]->{page} < 0;
52     $_[0]->{page};
53 }
54
55 1;