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 an +y 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 m +ore. 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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is this proof of concept too evil?
by ikegami (Patriarch) on Jan 17, 2009 at 23:16 UTC | |
by exodist (Monk) on Jan 18, 2009 at 01:01 UTC | |
by ikegami (Patriarch) on Jan 18, 2009 at 01:41 UTC | |
by exodist (Monk) on Jan 18, 2009 at 02:11 UTC | |
by ikegami (Patriarch) on Jan 18, 2009 at 02:23 UTC | |
|