I am playing with an api for people to write build systems, I want them to be able to have session variables that can be saved abnd loaded when you save and load a session. In this case specifically I think calling $session->var( 'name' ) would be a huge hassle, and done so much that simplifying it is huge.

UPDATE: no more prototypes, my example implied they were required, but I have since been told otherwise.
So I have played with tie to create this beast. It does as I want, a user can create a scalar, tie it to a session variable, and assign it a value all at once.

So I am looking for any kind of feedback. How evil is it? Is there a way I could simplify or improve this? is it just too horrible for a production module?

test.pl

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 );

The result: (does exactly as I want)

:! ./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';

The MyTest.pm module

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

    First of all, I find tsvars rather clunky. Two args per var would be much better.

    tsvars( a => my $a = 'A', b => my $b = 'B', ); tsvars( a => my $a2, b => my $b2 = 'B2', );
    my ($ident, $value) = @_[0,1]; tie( $_[1], 'MyTest::Scalar', $ident ); $_[1] = $value if defined($value); splice(@_, 0, 2);

    But you're just emulating aliases using tie magic. Magic adds a lot of overhead. You'd probably be better using true aliases. The interface is much more flexible too.

    use Data::Alias qw( alias ); my ($ident, $value) = @_[0,1]; alias $_[1] = $VALUES{ $ident ); $_[1] = $value if defined($value); splice(@_, 0, 2);
      Yay! I learned something new, never used alias before, thanks a bunch!
        Note: Data::Alias doesn't compile on Windows since it accesses private Perl internals (which the Window build process forbids) to make its powerful syntax possible. I just found Lexical::Alias which doesn't have that problem.
        use Lexical::Alias qw( alias ); my ($ident, $value) = @_[0,1]; alias $_[1], $VALUES{ $ident ); $_[1] = $value if defined($value); splice(@_, 0, 2);

        Update: It looks like Lexical::Alias can't alias a scalar to a hash element. It looks like you'll need to use tie as a fallback after all. Here's a slightly simpler version than what you had:

        { package My::Tie::Scalar::Alias; use strict; use warnings; use Tie::Scalar qw( ); our @ISA = 'Tie::Scalar'; sub TIESCALAR { my ($class, $ref) = @_; return bless $ref, $class; } sub FETCH { my $self = shift; return $$self; } sub STORE { my $self = shift; $$self = shift; } } tie $_[1], 'My::Tie::Scalar::Alias', \$VALUES{ $_[0] };