File Coverage

blib/lib/WWW/Session.pm
Criterion Covered Total %
statement 161 184 87.5
branch 47 68 69.1
condition 14 23 60.8
subroutine 29 34 85.2
pod 16 16 100.0
total 267 325 82.1


line stmt bran cond sub pod time code
1             package WWW::Session;
2              
3 3     3   51614 use 5.006;
  3         19  
  3         126  
4 3     3   15 use strict;
  3         7  
  3         99  
5 3     3   16 use warnings;
  3         13  
  3         8076  
6              
7             =head1 NAME
8              
9             WWW::Session - Generic session management engine for web applications
10              
11             =head1 DESCRIPTION
12              
13             Generic session management engine for web applications with multiple backends,
14             object serialization and data validation
15              
16             =head1 VERSION
17              
18             Version 0.10
19              
20             =cut
21              
22             our $VERSION = '0.10';
23              
24             =head1 SYNOPSIS
25              
26             This module allows you to easily create sessions , store data in them and later
27             retrieve that information, using multiple storage backends
28              
29             Example:
30              
31             use WWW::Session;
32            
33             #set up the storage backends
34             WWW::Session->add_storage( 'File', {path => '/tmp/sessions'} );
35             WWW::Session->add_storage( 'Memcached', {servers => ['127.0.0.1:11211']} );
36            
37             #Set up the serialization engine (defaults to JSON)
38             WWW::Session->serialization_engine('JSON');
39            
40             #Set up the default expiration time (in seconds or -1 for never)
41             WWW::Session->default_expiration_time(3600);
42              
43             #Turn on autosave
44             WWW::Session->autosave(1);
45            
46             #and than ...
47            
48             #Create a new session
49             my $session = WWW::Session->new($sid,$hash_ref);
50             ...
51             $session->sid(); #returns $sid
52             $session->data(); #returns $hash_ref
53              
54             #set the user
55             $session->user($user);
56             #retrieve the user
57             my $user = $session->user();
58              
59             #returns undef if it doesn't exist or it's expired
60             my $session = WWW::Session->find($sid);
61            
62             #returns the existing session if it exists, creates a new session if it doesn't
63             my $session = WWW::Session->find_or_create($sid);
64              
65             Using the session :
66              
67             =over 4
68              
69             =item * Settings values
70              
71             There are two ways you can save a value on the session :
72              
73             $session->set('user',$user);
74            
75             or
76            
77             $session->user($user);
78            
79             If the requested field ("user" in the example above) already exists it will be
80             assigned the new value, if it doesn't it will be added.
81              
82             When you set a value for a field it will be validated first (see setup_field() ).
83             If the value doesn't pass validation the field will keep it's old value and the
84             set method will return 0. If everything goes well the set method will return 1.
85            
86             =item * Retrieving values
87              
88             my $user = $session->get('user');
89            
90             or
91            
92             my $user = $session->user();
93            
94             If the requested field ("user" in the example above) already exists it will return
95             it's value, otherwise will return C
96              
97             =back
98              
99             We can automaticaly deflate/inflate certain informations when we store / retrieve
100             from storage the session data (see setup_field() for more details):
101              
102             WWW::Session->setup_field( 'user',
103             inflate => sub { return Some::Package->new( $_[0] ) },
104             deflate => sub { $_[0]->id() }
105             );
106              
107             We can automaticaly validate certain informations when we store / retrieve
108             from storage the session data (see setup_field() for more details):
109              
110             WWW::Session->setup_field( 'age',
111             filter => sub { $_[0] >= 18 }
112             );
113              
114              
115             Another way to initialize the module :
116              
117             use WWW::Session storage => [ 'File' => { path => '/tmp/sessions'},
118             'Memcached' => { servers => ['127.0.0.1'] }
119             ],
120             serialization => 'JSON',
121             expires => 3600,
122             fields => {
123             user => {
124             inflate => sub { return Some::Package->new( $_[0] ) },
125             deflate => sub { $_[0]->id() },
126             }
127             };
128            
129             =cut
130              
131             #Internal variables
132             my @storage_engines = ();
133             my $serializer = undef;
134             my $default_expiration = -1;
135             my $fields_modifiers = {};
136             my $autosave = 1;
137              
138             #Set up the default serializer
139             __PACKAGE__->serialization_engine('JSON');
140              
141             =head1 SESSION & OBJECTS
142              
143             The default serialization engine is JSON, but JSON can't serialize objects by default,
144             you will have to write more code to accomplish that. If your session data data contains
145             objects you can take one of the following approaches :
146              
147             =over 4
148              
149             =item * Use inflate/deflate (recommended)
150              
151             # if we have a user object (eg MyApp::User) we can deflate it like this
152            
153             WWW::Session->setup_field('user', deflate => sub { return $_[0]->id() } );
154            
155             #and inflate it back like this
156            
157             WWW::Session->setup_field('user',inflate => sub { return Some::Package->new( $_[0] ) } );
158            
159             This method even thow it's slower, it reduces the size of the session object when stored, and
160             it ensures that if the object data changed since we saved it, this changes will be reflected in the
161             object when we retrieve restore it (usefull for database result objects)
162              
163             =item * Change the serialization module to 'Storable'
164              
165             The 'Storable' serialization engine can handle object without any additional changes
166              
167             WWW::Session->serialization_engine('Storable');
168              
169             Note : The perl Storable module is not very compatible between different version, so sharing data
170             between multiple machines could cause problems. We recommad using the 'JSON' engine with
171             inflate/defate (described above);
172              
173             =back
174              
175             =head1 STORAGE BACKENDS
176              
177             You can use one or more of the fallowing backends (the list might not be complete, more backends might be available on CPAN):
178              
179             =head2 File storage
180              
181             Here is how you can set up the File storage backend :
182              
183             use WWW::Session;
184              
185             WWW::Session->add_storage('File', {path => '.'} );
186              
187             See WWW::Session::Storage::File for more details
188              
189             =head2 Database storage
190              
191             If you want to store your session is MySQL do this :
192              
193             use WWW::Session;
194              
195             WWW::Session->add_storage( 'MySQL', {
196             dbh => $dbh,
197             table => 'sessions',
198             fields => {
199             sid => 'session_id',
200             expires => 'expires',
201             data => 'data'
202             },
203             }
204             );
205              
206             The "fields" hasref contains the mapping of session internal data to the column names from MySQL.
207             The keys are the session fields ("sid","expires" and "data") and must all be present.
208              
209             The MySQL types of the columns should be :
210              
211             =over 4
212              
213             =item * sid => varchar(32)
214              
215             =item * expires => DATETIME or TIMESTAMP
216              
217             =item * data => text
218              
219             =back
220              
221             See WWW::Session::Storage::MySQL for more details
222              
223             =head2 Memcached storage
224              
225             To use memcached as a storage backend do this :
226              
227             use WWW::Session;
228              
229             WWW::Session->add_storage('Memcached', {servers => ['127.0.0.1:11211']} );
230              
231              
232             See WWW::Session::Storage::Memcached for more details
233              
234              
235             =head1 SUBROUTINES/METHODS
236              
237             =head2 new
238              
239             Creates a new session object with the unique identifier and the given data.
240             If a session with the same identifier previously existed it will be overwritten
241              
242             Parameters
243              
244             =over 4
245              
246             =item * sid = unique id for this session
247              
248             =item * data = hash reference containing the data that we want to store in the session object
249              
250             =item * exipres = for how many secconds is this session valid (defaults to the default expiration time)
251              
252             =back
253              
254             Retuns a WWW::Session object
255              
256             Usage :
257              
258             my $session = WWW::Session->new('session_id',{ a=> 1, b=> 2});
259              
260             =cut
261              
262             sub new {
263 11     11 1 1967 my ($class,$sid,$data,$expires) = @_;
264            
265 11   50     60 $expires ||= -1;
266 11   50     23 $data ||= {};
267            
268 11 50       29 die "You cannot use a undefined string as a session id!" unless $sid;
269            
270 11         53 my $self = {
271             data => {},
272             expires => $expires,
273             sid => $sid,
274             changed => {},
275             };
276            
277 11         33 bless $self, $class;
278            
279 11         25 $self->set($_,$data->{$_}) foreach keys %{$data};
  11         69  
280            
281 11         36 return $self;
282             }
283              
284             =head2 find
285              
286             Retieves the session object for the given session id
287              
288             Usage :
289              
290             my $session = WWW::Session->find('session_id');
291              
292             =cut
293             sub find {
294 8     8 1 572 my ($class,$sid) = @_;
295            
296 8 50       26 die "You cannot use a undefined string as a session id!" unless $sid;
297            
298 8         10 my $info;
299            
300 8         18 foreach my $storage (@storage_engines) {
301 8         41 $info = $storage->retrieve($sid);
302 8 100       33 last if defined $info;
303             }
304            
305 8 100       25 if ($info) {
306 5         28 my $session = $class->load($info);
307 5         188 $session->{changed} = {};
308 5         17 return $session;
309             }
310            
311 3         7 return undef;
312             }
313              
314             =head2 find_or_create
315              
316             Retieves the session object for the given session id if it exists, if not it
317             creates a new object with the given session id
318              
319             =over 4
320              
321             =item * sid = unique id for this session
322              
323             =item * data = hash reference containing the data that we want to store in the session object
324              
325             =item * exipres = for how many secconds is this session valid (defaults to the default expiration time),
326              
327             =back
328              
329             Usage:
330              
331             my $session = WWW::Session->find_or_create('session_id',{ c=>2 })
332              
333             =cut
334             sub find_or_create {
335 2     2 1 6 my ($class,$sid,$data,$expires) = @_;
336            
337 2         81 my $self = $class->find($sid);
338            
339 2 100       9 if ($self) {
340 1 50       4 $self->expires($expires) if defined ($expires);
341 1         3 $self->set($_,$data->{$_}) foreach keys %{$data};
  1         8  
342             }
343             else {
344 1         5 $self = $class->new($sid,$data,$expires);
345             }
346            
347 2         9 return $self;
348             }
349              
350              
351             =head2 set
352              
353             Adds/sets a new value for the given field
354              
355             Usage :
356              
357             $session->set('user',$user);
358            
359             The values can also be set by calling the name of the field you want to set
360             as a method :
361              
362             $session->user($user);
363              
364             =cut
365              
366             sub set {
367 28     28 1 2729 my ($self,$field,$value) = @_;
368            
369 28 0 33     79 if (! defined $value && exists $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{default}) {
      33        
370 0         0 $value = $fields_modifiers->{$field}->{default};
371             }
372            
373 28         42 my $validated = 1;
374            
375 28 100 66     114 if ( exists $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{filter} ) {
376            
377 6         8 $validated = 0; #we have a filter, check the value against the filter first
378            
379 6         11 my $filter = $fields_modifiers->{$field}->{filter};
380            
381 6 50       18 die "Filter must be a hash ref or array ref or code ref" unless ref($filter);
382            
383 6 100       30 if (ref($filter) eq "ARRAY") {
    100          
    50          
384 2 100       3 if (grep { $value eq $_ } @{$filter}) {
  6         107  
  2         4  
385 1         3 $validated = 1;
386             }
387             }
388             elsif (ref($filter) eq "CODE") {
389 2         6 $validated = $filter->($value);
390             }
391             elsif (ref($filter) eq "HASH") {
392 2         3 my $h_valid = 1;
393            
394 2 50       8 if ( defined $filter->{isa} ) {
395 2 100       10 $h_valid = 0 unless ref($value) eq $filter->{isa};
396             }
397            
398 2         4 $validated = $h_valid;
399             }
400             }
401            
402 28 100       58 if ($validated) {
403 25         66 $self->{data}->{$field} = $value;
404 25         53 $self->{changed}->{$field} = 1;
405             }
406             else {
407 3         409 warn "Value $value failed validation for key $field";
408             }
409            
410 28         104 return $validated;
411             }
412              
413              
414             =head2 get
415              
416             Retrieves the value of the given key from the session object
417              
418             Usage :
419              
420             my $user = $session->get('user');
421            
422             You can also use the name of the field you want to retrieve as a method.
423             The above call does the same as :
424              
425             my $user = $session->user();
426            
427             =cut
428              
429             sub get {
430 22     22 1 129 my ($self,$field) = @_;
431            
432 22         110 return $self->{data}->{$field};
433             }
434              
435             =head2 delete
436              
437             Removes the given key from the session data
438              
439             Usage :
440              
441             $session->delete('user');
442              
443             =cut
444             sub delete {
445 1     1 1 3 my ($self,$key) = @_;
446            
447 1         3 $self->{changed}->{$key} = 1;
448 1         9 return delete $self->{data}->{$key};
449             }
450              
451             =head2 sid
452              
453             Returns the session id associated with this session
454            
455             =cut
456              
457             sub sid {
458 19     19 1 3980 my ($self) = @_;
459            
460 19         67 return $self->{sid};
461             }
462              
463             =head2 expires
464              
465             Getter/Setter for the expiration time of this session
466            
467             =cut
468              
469             sub expires {
470 0     0 1 0 my ($self,$value) = @_;
471              
472 0 0       0 if (defined $value) {
473 0         0 $self->{expires} = $value;
474             }
475              
476 0         0 return $self->{expires};
477             }
478              
479             =head2 add_storage
480              
481             Adds a new storge engine to the list of Storage engines that will be used to
482             store the session info
483              
484             Usage :
485              
486             WWW::Session->add_storage($storage_engine_name,$storage_engine_options);
487            
488             Parameters :
489              
490             =over 4
491              
492             =item * $storage_engine_name = Name of the class that defines a valid storage engine
493              
494             For WWW::Session::Storage::* modules you can use only the name of the storage,
495             you don't need the full name. eg Memcached and WWW::Session::Storage::Memcached
496             are synonyms
497              
498             =item * $storage_engine_options = hash ref containing the options that will be
499             passed on to the storage engine module when new() is called
500              
501             =back
502              
503             Example :
504              
505             WWW::Session->add_storage( 'File', {path => '/tmp/sessions'} );
506            
507             WWW::Session->add_storage( 'Memcached', {servers => ['127.0.0.1:11211']} );
508              
509             See each storage module for aditional details
510              
511             =cut
512              
513             sub add_storage {
514 2     2 1 1068 my ($class,$name,$options) = @_;
515            
516 2   50     13 $options ||= {};
517            
518 2 50       12 if ($name !~ /::/) {
519 2         8 $name = "WWW::Session::Storage::$name";
520             }
521            
522 2     2   159 eval "use $name";
  2         1933  
  2         5  
  2         38  
523            
524 2 50       9 die "WWW::Session cannot load '$name' storage engine! Error : $@" if ($@);
525            
526 2         12 my $storage = $name->new($options);
527            
528 2 50       15 if ($storage) {
529 2         11 push @storage_engines, $storage;
530             }
531             else {
532 0         0 die "WWW::Session storage engine '$name' failed to initialize with the given arguments!";
533             }
534             }
535              
536             =head2 serialization_engine
537              
538             Configures the serialization engine to be used for serialising sessions.
539              
540             The default serialization engine is JSON
541              
542             Usage :
543              
544             WWW::Session->serialization_engine('JSON');
545            
546             Parameters :
547              
548             =over 4
549              
550             =item * $serialization_engine_name = Name of the class that defines a valid serialization engine
551              
552             For WWW::Session::Serialization::* modules you can use only the short name of the module,
553             you don't need the full name. eg JSON and WWW::Session::Serialization::JSON
554             are synonyms
555              
556             =back
557              
558             =cut
559              
560             sub serialization_engine {
561 5     5 1 657 my ($class,$name) = @_;
562            
563 5 50       26 if ($name !~ /::/) {
564 5         16 $name = "WWW::Session::Serialization::$name";
565             }
566            
567 3     3   2891 eval "use $name";
  3     2   9  
  3         69  
  5         336  
  2         12  
  2         589  
  2         34  
568            
569 5 50       25 die "WWW::Session cannot load '$name' serialization engine! Error : $@" if ($@);
570            
571 5         27 my $serializer_object = $name->new($fields_modifiers);
572            
573 5 50       30 if ($serializer_object) {
574 5         15 $serializer = $serializer_object;
575             }
576             else {
577 0         0 die "WWW::Session serialization engine '$name' failed to initialize!";
578             }
579             }
580              
581             =head2 autosave
582              
583             Turn on/off the autosave feature (on by default)
584              
585             If this feature is on the object will always be saved before beying destroyed
586              
587             Usage :
588              
589             WWW::Session->autosave(1);
590              
591             =cut
592              
593             sub autosave {
594 3     3 1 1354 my ($class,$value) = @_;
595            
596 3 50       12 $autosave = $value if defined $value;
597            
598 3         11 return $autosave;
599             }
600              
601             =head2 default_expiration_time
602              
603             Setter/Getter for the default expiration time
604              
605             Usage :
606              
607             WWW::Session->default_expiration_time(1800);
608            
609             =cut
610              
611             sub default_expiration_time {
612 0     0 1 0 my ($class,$value) = @_;
613            
614 0 0       0 if (defined $value) {
615 0         0 $default_expiration = $value;
616             }
617            
618 0         0 return $default_expiration;
619             }
620              
621             =head2 destroy
622              
623             Completely removes all the data related to the current session
624              
625             NOTE: After calling destroy the session object will no longer be usable
626              
627             Usage :
628              
629             $session->destroy();
630            
631             =cut
632              
633             sub destroy {
634            
635             #save the session id fiers and undef the object before we delete it from
636             #storage to avoid autosave kikking in after we remove it from storage
637            
638 13     13 1 631 my $sid = $_[0]->sid();
639            
640 13         19 $_[0] = undef;
641            
642 13         43 foreach my $storage (@storage_engines) {
643 13         54 $storage->delete($sid);
644             }
645             }
646              
647              
648             =head2 setup_field
649              
650             Sets up the filters, inflators and deflators for the given field
651              
652             =head3 deflators
653              
654             Deflators are passed as code refs. The only argument the deflator
655             method receives is the value of the filed that it must be deflated and
656             it must return a single value (scalar, object or reference) that will be
657             asigned to the key.
658              
659             Example :
660              
661             # if we have a user object (eg MyApp::User) we can deflate it like this
662            
663             WWW::Session->setup_field('user', deflate => sub { return $_[0]->id() } );
664              
665             =head3 inflators
666              
667             Inflators are passed as code refs. The only argument the inflator
668             method receives is the value of the filed that it must inflate and
669             it must return a single value (scalar, object or reference) that will be
670             asigned to the key.
671              
672             Example :
673              
674             # if we have a user object (eg MyApp::User) we can inflate it like this
675              
676             WWW::Session->setup_field('user',inflate => sub { return Some::Package->new( $_[0] ) } );
677              
678             =head3 filters
679              
680             Filters can be used to ensure that the values from the session have the required values
681              
682             Filters can be :
683              
684             =over 4
685              
686             =item * array ref
687              
688             In this case when we call C<$session->set($field,$value)> the values will have to be one of the
689             values from the array ref , or the operation will fail
690              
691             Example :
692              
693             #Check that the age is between 18 and 99
694             WWW::Session->setup_field('age',filter => [18..99] );
695              
696             =item * code ref
697              
698             In this case the field value will be passed to the code ref as the only parameter. The code ref
699             must return a true or false value. If it returns a false value the set() operation will fail
700              
701             Example :
702              
703             #Check that the age is > 18
704             WWW::Session->setup_field('age',filter => sub { $_[0] > 18 } );
705              
706             =item * hash ref
707              
708             In this case the only key from the hash that is recognised is "isa" will will chek that the
709             given value has the types specified as the value for "isa"
710              
711             Example :
712              
713             #Check that the 'rights' field is an array
714             WWW::Session->setup_field('age',filter => { isa => "ARRAY" } );
715            
716             #Check that the 'user' field is an MyApp::User object
717             WWW::Session->setup_field('user',filter => { isa => "MyApp::User" } );
718              
719             =back
720              
721             Note: The entire setup for a field must be done in a single call to setup_field() or the previous
722             settings will be overwritten!
723              
724             Example :
725              
726             WWW::Session->setup_field(
727             'user',
728             filter => { isa => "MyApp::User" },
729             deflate => sub { $_[0]->id() },
730             inflate => sub { return MyApp::User->find($_[0]) }
731             );
732              
733             =cut
734              
735             sub setup_field {
736 3     3 1 807 my ($self,$field,%settings) = @_;
737            
738 3         14 $fields_modifiers->{$field} = \%settings;
739             }
740              
741             =head2 save
742              
743             Serializes a WWW::Session object sends it to all storage engines for saving
744              
745             =cut
746              
747             sub save {
748 15     15 1 561 my ($self) = @_;
749            
750 15         66 my $data = {
751             sid => $self->{sid},
752             expires => $self->{expires},
753             };
754            
755 15         26 foreach my $field ( keys %{$self->{data}} ) {
  15         49  
756 28 100 100     180 if (defined $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{deflate}) {
757 2         9 $data->{data}->{$field} = $fields_modifiers->{$field}->{deflate}->($self->{data}->{$field});
758             }
759             else {
760 26         88 $data->{data}->{$field} = $self->{data}->{$field}
761             }
762             }
763            
764 15         192 my $string = $serializer->serialize($data);
765            
766 15         411 foreach my $storage (@storage_engines) {
767 15         236 $storage->save($self->{sid},$self->{expires},$string);
768             }
769             }
770              
771             =head1 ACCESSING SESSION DATA
772              
773             Allows us to get/set session data directly by calling the field name as a method
774              
775             Example:
776              
777             my $user = $session->user(); #same as $user = $session->get('user');
778            
779             #or
780            
781             $session->age(21); #same as $session->set('age',21);
782              
783             =cut
784              
785             our $AUTOLOAD;
786             sub AUTOLOAD {
787 4     4   17 my $self = shift;
788 4         6 my $value = shift;
789              
790 4         8 my $field = $AUTOLOAD;
791              
792 4         18 $field =~ s/.*:://;
793              
794 4 100       13 if (defined $value) {
795 2         9 $self->set($field,$value);
796             }
797            
798 4         12 return $self->get($field);
799             }
800              
801              
802             =head1 AUTOSAVE FEATURE
803              
804             If you set autosave to 1 the session will be saved before the object is
805             destroyed if any data has changed
806              
807             BE CAREFULL : If you store complex structures only the changes made to direct
808             session keys will be detected.
809              
810             Example :
811              
812             #this change will be detected because it affects a direct session attribute
813             $session->age(21);
814              
815             #this changes won't be detected :
816             my $user = $session->user();
817             $user->{age} = 21;
818            
819             You have two choices :
820              
821             =over 4
822              
823             =item 1 Make a change that can be detected
824              
825             $session->some_random_field( time() );
826            
827             =item 2 Save the session manually
828              
829             $session->save();
830            
831             =back
832            
833             =cut
834              
835             sub DESTROY {
836 16     16   2891 my $self = shift;
837            
838 16 100 100     179 if ($autosave && scalar(keys %{$self->{changed}})) {
  14         66  
839 10         25 $self->save();
840             }
841             }
842              
843              
844             =head1 PRIVATE METHODS
845              
846             =head2 load
847              
848             Deserializes a WWW::Session object from the given string and deflates all the fields that
849             were inflated when the session was serialized
850              
851             =cut
852              
853             sub load {
854 5     5 1 11 my ($class,$string) = @_;
855            
856 5         22 my $self = $serializer->expand($string);
857            
858 5         223 foreach my $field ( keys %{$self->{data}} ) {
  5         22  
859 9 100 66     41 if (defined $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{inflate}) {
860 1         7 $self->{data}->{$field} = $fields_modifiers->{$field}->{inflate}->($self->{data}->{$field});
861             }
862             }
863            
864 5         31 bless $self,$class;
865            
866 5         13 return $self;
867             }
868              
869             =head2 import
870              
871             Allows us to configure all the module options in one line
872              
873             Example :
874              
875             use WWW::Session storage => [
876             'File' => { path => '/tmp/sessions'},
877             'Memcached' => { servers => ['127.0.0.1'] }
878             ],
879             serialization => 'Storable',
880             expires => 3600,
881             fields => {
882             user => {
883             inflate => sub { return Some::Package->new( $_[0]->id() ) },
884             deflate => sub { $_[0]->id() },
885             },
886             age => {
887             filter => [21..99],
888             }
889             },
890             autosave => 1;
891              
892             =cut
893              
894             sub import {
895 3     3   37 my ($class, %params) = @_;
896            
897 3 100       22 if (defined $params{storage}) {
898 1         2 while ( scalar(@{$params{storage}}) ) {
  2         10  
899 1         2 my $engine = shift @{$params{storage}};
  1         3  
900 1         2 my $options = shift @{$params{storage}};
  1         2  
901 1         6 $class->add_storage($engine,$options);
902             }
903             }
904 3 100       13 if (defined $params{serialization}) {
905 1         6 $class->serialization_engine($params{serialization});
906             }
907 3 50       20 if (defined $params{expires}) {
908 0         0 $class->default_expiration_time($params{expires});
909             }
910 3 100       27 if (defined $params{autosave}) {
911 1         5 $class->autosave($params{autosave});
912             }
913 3 50       2610 if (defined $params{fields}) {
914 0         0 foreach my $field (keys %{$params{fields}}) {
  0         0  
915 0         0 $class->setup_field($field,%{ $params{fields}->{$field} });
  0         0  
916             }
917             }
918             }
919              
920              
921             =head1 TIE INTERFACE
922              
923             The WWW::Session objects can be tied to hashes to make them easier to use
924              
925             Example :
926              
927             my %session;
928            
929             tie %session, WWW::Session, 'session_id', {user => $user, authenticated => 1};
930            
931             ...
932             my $user = $session{user};
933              
934             ...
935             $session{authenticated} = 0;
936             delete $session{user};
937              
938             =cut
939              
940             sub TIEHASH {
941 1     1   16 my ($class,@params) = @_;
942            
943 1         6 return $class->find_or_create(@params);
944             }
945              
946             sub STORE {
947 1     1   75 my ($self,$key,$value) = @_;
948            
949 1         6 $self->set($key,$value);
950             }
951              
952             sub FETCH {
953 3     3   460 my ($self,$key) = @_;
954            
955 3         8 return $self->get($key);
956             }
957              
958             sub DELETE {
959 1     1   5 my ($self,$key) = @_;
960            
961 1         6 $self->delete($key);
962             }
963              
964             sub CLEAR {
965 0     0   0 my ($self) = @_;
966            
967 0         0 $self->{data} = {};
968             }
969              
970             sub EXISTS {
971 0     0   0 my ($self,$key) = @_;
972            
973 0         0 return exists $self->{data}->{$key};
974             }
975              
976             sub FIRSTKEY {
977 3     3   811 my ($self) = @_;
978            
979 3         5 my $a = keys %{ $self->{data} };
  3         8  
980            
981 3         4 each %{ $self->{data} };
  3         16  
982             }
983              
984             sub NEXTKEY {
985 5     5   9 my ($self) = @_;
986            
987 5         4 return each %{ $self->{data} };
  5         30  
988             }
989              
990             sub SCALAR {
991 0     0   0 my ($self) = @_;
992            
993 0         0 return scalar %{ $self->{data} };
  0         0  
994             }
995              
996             =head1 AUTHOR
997              
998             Gligan Calin Horea, C<< >>
999              
1000             =head1 BUGS
1001              
1002             Please report any bugs or feature requests to C, or through
1003             the web interface at L. I will be notified, and then you'll
1004             automatically be notified of progress on your bug as I make changes.
1005              
1006              
1007             =head1 SUPPORT
1008              
1009             You can find documentation for this module with the perldoc command.
1010              
1011             perldoc WWW::Session
1012              
1013              
1014             You can also look for information at:
1015              
1016             =over 4
1017              
1018             =item * RT: CPAN's request tracker (report bugs here)
1019              
1020             L
1021              
1022             =item * AnnoCPAN: Annotated CPAN documentation
1023              
1024             L
1025              
1026             =item * CPAN Ratings
1027              
1028             L
1029              
1030             =item * Search CPAN
1031              
1032             L
1033              
1034             =back
1035              
1036              
1037             =head1 ACKNOWLEDGEMENTS
1038              
1039              
1040             =head1 LICENSE AND COPYRIGHT
1041              
1042             Copyright 2012 Gligan Calin Horea.
1043              
1044             This program is free software; you can redistribute it and/or modify it
1045             under the terms of either: the GNU General Public License as published
1046             by the Free Software Foundation; or the Artistic License.
1047              
1048             See http://dev.perl.org/licenses/ for more information.
1049              
1050              
1051             =cut
1052              
1053             1; # End of WWW::Session