2 # Hash tied to optionally dynamically generate its values
3 # Copyright (C) 2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; exactly version 2 of June 1991 is required
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package My::Hash::Push;
20 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
24 use My::Web; # for &Wrequire
26 our @ISA=qw(My::Hash);
27 use Carp qw(cluck confess);
28 require Data::Compare;
31 # You are recommended to STORE all the keys, not to parent-inherit them.
38 $val=[] if !defined $val;
39 $val=[$val] if "ARRAY" ne ref($val);
45 my($self,$key,$state)=@_;
47 my $ref=\$self->{"ours"}{$key};
48 warn "State change for key \"$key\"; was=".(0+$$ref).",now=".(0+$state)
49 if defined($state) && defined($$ref) && $state!=$$ref;
50 $$ref=$state if defined $state;
56 my($this,$key,$value)=@_;
58 if ($key=~s/_push$//) {
60 return $this->pass("STORE",$key,[@{$this->_normalize($this->pass("FETCH",$key))},$value]);
62 if ($key=~s/_pop$//) {
64 my $val_orig=$this->FETCH($key);
65 for my $vali (reverse 0..$#$val_orig) {
66 next if Data::Compare::Compare($val_orig->[$vali],$value);
67 splice @$val_orig,$vali,1;
68 return $this->pass("STORE",$key,$val_orig);
70 cluck "value[1] not found in the array[2] of key[0]:\n".Dumper($key,$value,$val_orig);
73 if ($key=~s/_set$//) {
75 return $this->pass("STORE",$key,$this->_normalize($value));
78 return $this->pass("STORE",$key,$value);
83 my($this,$key_orig)=@_;
85 my $force=((my $key=$key_orig)=~s/_push$//);
86 $this->_ours($key,1) if $force;
87 my $val=$this->pass("FETCH",$key);
88 return $this->_normalize($val) if $force || $this->_ours($key);