data/TWiki/CGISessionDriverDotPm.txt,v
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
head	1.1;
colas@0
     2
access;
colas@0
     3
symbols;
colas@0
     4
locks; strict;
colas@0
     5
comment	@# @;
colas@0
     6
colas@0
     7
colas@0
     8
1.1
colas@0
     9
date	2008.01.22.03.21.33;	author TWikiContributor;	state Exp;
colas@0
    10
branches;
colas@0
    11
next	;
colas@0
    12
colas@0
    13
colas@0
    14
desc
colas@0
    15
@buildrelease
colas@0
    16
@
colas@0
    17
colas@0
    18
colas@0
    19
1.1
colas@0
    20
log
colas@0
    21
@buildrelease
colas@0
    22
@
colas@0
    23
text
colas@0
    24
@---+ Package ==
colas@0
    25
*extends* <tt>CGI::Session::ErrorHandler</tt>
colas@0
    26
colas@0
    27
colas@0
    28
%TOC%
colas@0
    29
=head1 NAME
colas@0
    30
colas@0
    31
CGI::Session::Driver - CGI::Session driver specifications
colas@0
    32
colas@0
    33
=head1 WARNING
colas@0
    34
colas@0
    35
Version 4.0 of CGI::Session's driver specification is B<NOT> backward compatible with previous specification. If you already have a driver developed to work with the previous version you're highly encouraged to upgrade your driver code to make it compatible with the current version. Fortunately, current driver specs are a lot easier to adapt to.
colas@0
    36
colas@0
    37
If you need any help converting your driver to meet current specs, send me an e-mail. For support information see
colas@0
    38
L<CGI::Session|CGI::Session>
colas@0
    39
colas@0
    40
=head1 SYNOPSIS
colas@0
    41
colas@0
    42
    require CGI::Session::Driver;
colas@0
    43
    @@ISA = qw( CGI::Session::Driver );
colas@0
    44
colas@0
    45
=head1 DESCRIPTION
colas@0
    46
colas@0
    47
CGI::Session::Driver is a base class for all CGI::Session's native drivers. It also documents driver specifications for those willing to write drivers for different databases not currently supported by CGI::Session.
colas@0
    48
colas@0
    49
=head1 WHAT IS A DRIVER
colas@0
    50
colas@0
    51
Driver is a piece of code that helps CGI::Session library to talk to specific database engines, or storage mechanisms. To be more precise, driver is a F<.pm> file that inherits from CGI::Session::Driver and defines L<retrieve()|/"retrieve($self, $sid)">, L<store()|/"store($self, $sid, $datastr)"> and L<remove()|/"remove($self, $sid)"> methods.
colas@0
    52
colas@0
    53
=head2 BLUEPRINT
colas@0
    54
colas@0
    55
The best way of learning the specs is to look at a blueprint of a driver:
colas@0
    56
colas@0
    57
    package CGI::Session::Driver::your_driver_name;
colas@0
    58
    use strict;
colas@0
    59
    use base qw( CGI::Session::Driver CGI::Session::ErrorHandler );
colas@0
    60
colas@0
    61
    sub init {
colas@0
    62
        my ($self) = @@_;
colas@0
    63
        # optional
colas@0
    64
    }
colas@0
    65
colas@0
    66
    sub DESTROY {
colas@0
    67
        my ($self) = @@_;
colas@0
    68
        # optional
colas@0
    69
    }
colas@0
    70
colas@0
    71
    sub store {
colas@0
    72
        my ($self, $sid, $datastr) = @@_;
colas@0
    73
        # Store $datastr, which is an already serialized string of data.
colas@0
    74
    }
colas@0
    75
colas@0
    76
    sub retrieve {
colas@0
    77
        my ($self, $sid) = @@_;
colas@0
    78
        # Return $datastr, which was previously stored using above store() method.
colas@0
    79
        # Return $datastr if $sid was found. Return 0 or "" if $sid doesn't exist
colas@0
    80
        }
colas@0
    81
colas@0
    82
    sub remove {
colas@0
    83
        my ($self, $sid) = @@_;
colas@0
    84
        # Remove storage associated with $sid. Return any true value indicating success,
colas@0
    85
        # or undef on failure.
colas@0
    86
    }
colas@0
    87
colas@0
    88
    sub traverse {
colas@0
    89
        my ($self, $coderef) = @@_;
colas@0
    90
        # execute $coderef for each session id passing session id as the first and the only
colas@0
    91
        # argument
colas@0
    92
    }
colas@0
    93
colas@0
    94
    1;
colas@0
    95
colas@0
    96
All the attributes passed as the second argument to CGI::Session's new() or load() methods will automatically
colas@0
    97
be made driver's object attributes. For example, if session object was initialized as following:
colas@0
    98
colas@0
    99
    $s = CGI::Session->new("driver:your_driver_name", undef, {Directory=>'/tmp/sessions'});
colas@0
   100
colas@0
   101
You can access value of 'Directory' from within your driver like so:
colas@0
   102
colas@0
   103
    sub store {
colas@0
   104
        my ($self, $sid, $datastr) = @@_;
colas@0
   105
        my $dir = $self->{Directory};   # <-- in this example will be '/tmp/sessions'
colas@0
   106
    }
colas@0
   107
colas@0
   108
Optionally, you can define C<init()> method within your driver to do driver specific global initialization. C<init()> method will be invoked only once during the lifecycle of your driver, which is the same as the lifecycle of a session object.
colas@0
   109
colas@0
   110
For examples of C<init()> look into the source code of native CGI::Session drivers.
colas@0
   111
colas@0
   112
=head1 METHODS
colas@0
   113
colas@0
   114
This section lists and describes all driver methods. All the driver methods will receive driver object ($self) as the first argument. Methods that pertain to an individual session (such as C<retrieve()>, C<store()> and C<remove()>) will also receive session id ($sid) as the second argument.
colas@0
   115
colas@0
   116
Following list describes every driver method, including its argument list and what step of session's life they will be invoked. Understanding this may help driver authors.
colas@0
   117
colas@0
   118
=over 4
colas@0
   119
colas@0
   120
=item retrieve($self, $sid)
colas@0
   121
colas@0
   122
Called whenever a specific session is requested either via C<< CGI::Session->new() >> or C<< CGI::Session->load() >> syntax. Method should try to retrieve data associated with C< $sid > and return it. In case no data could be retrieved for C< $sid > 0 (zero) or "" should be returned. undef must be returned only to signal error. Error message should be set via set_error(), which can be inherited from L<CGI::Session::ErrorHandler|CGI::Session::ErrorHandler>. 
colas@0
   123
colas@0
   124
Tip: set_error() always returns undef. Use it for your advantage.
colas@0
   125
colas@0
   126
=item store($self, $sid, $datastr)
colas@0
   127
colas@0
   128
Called whenever modified session data is to be stored back to disk. This happens whenever CGI::Session->flush() is called on modified session. Since CGI::Session->DESTROY() calls flush(), store() gets requested each time session object is to be terminated.
colas@0
   129
colas@0
   130
C< store() > is called both to store new sessions and to update already stored sessions. It's driver author's job to figure out which operation needs to be performed.
colas@0
   131
colas@0
   132
$datastr, which is passed as the third argument to represents B<already serialized> session data that needs to be saved.
colas@0
   133
colas@0
   134
store() can return any true value indicating success or undef on failure. Error message should be passed to set_error()
colas@0
   135
colas@0
   136
=item remove($self, $sid)
colas@0
   137
colas@0
   138
Called whenever session data is to be deleted, which is when CGI::Session->delete() is called. Should return any true value indicating success, undef on failure. Error message should be logged in set_error().
colas@0
   139
colas@0
   140
=item traverse($self, \&coderef)
colas@0
   141
colas@0
   142
Called only from within CGI::Session->find(). Job of traverse() is to call \&coderef for every single session stored in disk passing session's id as the first and only argument: C<< $coderef->( $sid ) >>
colas@0
   143
colas@0
   144
=item init($self)
colas@0
   145
colas@0
   146
Optional. Called whenever driver object is to be initialized, which happens only once during the lifecycle of CGI::Session object. Here you can do driver-wide initialization, such as to open connection to a database server.
colas@0
   147
colas@0
   148
=item DESTROY($self)
colas@0
   149
colas@0
   150
Optional. Perl automatically calls this method on objects just before they are to be terminated. This gives your driver chance to close any database connections or close any open file handles.
colas@0
   151
colas@0
   152
=back
colas@0
   153
colas@0
   154
=head2 NOTES
colas@0
   155
colas@0
   156
=over 4
colas@0
   157
colas@0
   158
=item *
colas@0
   159
colas@0
   160
All driver F<.pm> files must be lowercase!
colas@0
   161
colas@0
   162
=item *
colas@0
   163
colas@0
   164
DBI-related drivers are better off using L<CGI::Session::Driver::DBI|CGI::Session::Driver::DBI> as base, but don't have to.
colas@0
   165
colas@0
   166
=back
colas@0
   167
colas@0
   168
=head1 LICENSING
colas@0
   169
colas@0
   170
For support and licensing see L<CGI::Session|CGI::Session>.
colas@0
   171
colas@0
   172
@