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;


In reply to Is this proof of concept too evil? by exodist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.