1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Data::Dumper;
5
6 use MyTest;
7
8 # Create some session variables, variables that can be used like any scalar,
9 # but the value will be saved in the session, that way we can save and reload
10 # the session for persistance.
11 tsvars(
12 my $a => 'a' => 'A',
13 my $b => 'b' => 'B',
14 # Lets tie another variable to the same session variable, kinda silly, but
15 # it may be needed to access the session variable in different scopes.
16 my $a2 => 'a' => undef,
17 # Same as above, but give it a new value, should warn us.
18 my $b2 => 'b' => 'B2',
19 );
20
21 print Dumper( $a, $b, $a2 );
22
23 #Try changing a session variable.
24 $a = "bob";
25
26 print Dumper( $a, $b, $a2 );
####
:! ./test.pl
Warning: 'b' session variable set multiple times in one call to tsvars
$VAR1 = 'A';
$VAR2 = 'B2';
$VAR3 = 'A';
$VAR1 = 'bob';
$VAR2 = 'B2';
$VAR3 = 'bob';
####
1 use strict;
2 use warnings;
3
4 package MyTest;
5
6 use base 'Exporter';
7 our @EXPORT = qw/ tsvar tsvars /;
8
9 # This is a proof of concept, in the real session object there is more.
10 my %VALUES;
11
12 # Alias for a single variable
13 sub tsvar {
14 goto &tsvars;
15 }
16
17
18
19 sub tsvars {
20 my %set;
21 while ( @_ ) {
22 my ( $var, $ident, $value ) = @_;
23 die( "Session variables must be scalars\n" ) if ref $var;
24 tie( $_[0], 'MyTest::Scalar', $ident );
25 if ( defined $value ) {
26 warn( "Warning: '$ident' session variable set multiple times in one call to tsvars\n" ) if $set{ $ident };
27 $VALUES{ $ident } = $value;
28 $set{ $ident } = 1;
29 }
30 shift( @_ ) for 0 .. 2;
31 }
32 }
33
34 # Simple Tie::Scalar stuff.
35 # This directly accesses the %VALUES, in the real thing the session objects is
36 # passed in and stored in the scalar object.
37 {
38 package MyTest::Scalar;
39
40 use strict;
41 use warnings;
42
43 use Tie::Scalar;
44
45 our @ISA = qw(Tie::Scalar);
46
47 sub TIESCALAR {
48 my ( $class, $ident ) = @_;
49 $class = ref $class || $class;
50 return bless { ident => $ident }, $class;
51 }
52
53 sub FETCH {
54 my $self = shift;
55 return $VALUES{ $self->ident };
56 }
57
58 sub STORE {
59 my $self = shift;
60 $VALUES{ $self->ident } = shift if @_;
61 }
62
63 sub ident {
64 my $self = shift;
65 return $self->{ ident };
66 }
67 }
68
69 1;