| 1 | # Copyright 2010 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Tie::GetSetKey; | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | 1; | ||
| 22 | __END__ | ||
| 23 | |||
| 24 | sub new_ref { | ||
| 25 | my $class = shift; | ||
| 26 | tie (my $tie, @_); | ||
| 27 | return \$tie; | ||
| 28 | } | ||
| 29 | sub TIESCALAR { | ||
| 30 | my ($class, $obj, $key, $newval) = @_; | ||
| 31 | my $oldval = $obj->get($key); | ||
| 32 | my $self = bless [$obj, $key, $oldval], $class; | ||
| 33 | Scalar::Util::weaken ($self->[1]); | ||
| 34 | if (@_ >= 4) { | ||
| 35 | $obj->set($key, $value); | ||
| 36 | } | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | sub FETCH { | ||
| 40 | my ($self) = @_; | ||
| 41 | my $obj = $self->[0] || return; | ||
| 42 | return $obj->get($self->[1]); | ||
| 43 | } | ||
| 44 | sub STORE { | ||
| 45 | my ($self, $value) = @_; | ||
| 46 | my $obj = $self->[0] || return; | ||
| 47 | return $obj->set($self->[1], $value); | ||
| 48 | } | ||
| 49 | sub UNTIE { | ||
| 50 | my ($self) = @_; | ||
| 51 | my $obj = $self->[0] || return; | ||
| 52 | return $obj->set($self->[1], $self->[2]); | ||
| 53 | } | ||
| 54 | |||
| 55 | 1; | ||
| 56 | __END__ |