#!perl -w use strict; # Creating a class called ValueRing package ValueRing; # Class method to construct a ValueRing object, a blessed ARRAY reference # this is the constructor for scalar ties # my $ring = tie $color, "ValueRing", qw(red blue); # foreach my $value (@$ring) { print "$value "; } sub TIESCALAR { my ($class, @values) = @_; bless \@values, $class; return \@values; } # Object method to read the ValueRing # this intercepts read accesses # my $ring = tie $color, "ValueRing", qw(red blue); # prints "red blue red" # print "$color $color $color\n"; sub FETCH { my $self = shift; push(@$self, shift(@$self)); return $self->[-1]; } # Object method to add to the ValueRing # this intercepts write accesses # my $ring = tie $color, "ValueRing", qw(red blue); # $color = "green" # Now the ring is ("green", "red", "blue") sub STORE { my ($self, $value) = @_; unshift @$self, $value; return $value; } 1;