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;