package Zen::Config::Hash; use strict; sub create { my $class = shift; my $hash = {}; tie %$hash, $class; $hash; } sub createAndLoad { my $class = shift; my $hash = $class->create; %$hash = @_; $hash; } sub TIEHASH { my $class = shift; bless { data => {}, keys => [], }, $class; } sub STORE { my $this = shift; my ($k, $v) = @_; if (exists $this->{data}{$k}) { if (ref($v) eq 'HASH' && ref($this->{data}{$k}) eq 'HASH') { $this->{data}{$k} = Zen::Config::Hash->createAndLoad( %{$this->{data}{$k}}, %$v ); } else { $this->{data}{$k} = $v; } } else { push @{ $this->{keys} }, $k; $this->{data}{$k} = $v; } } sub FETCH { $_[0]->{data}{$_[1]}; } sub FIRSTKEY { my $this = shift; return unless $this->{keys} && scalar(@{ $this->{keys} }); $this->{i} = 0; $this->{keys}[0]; } sub NEXTKEY { my $this = shift; return unless $this->{i} >= 0; $this->{i}++; if ($this->{i} == scalar(@{ $this->{keys} })) { $this->{i} = -1; return; } else { return $this->{keys}[$this->{i}]; } } sub EXISTS { exists $_[0]->{data}{$_[1]}; } sub DELETE { my $this = shift; my $k = shift; @{ $this->{keys} } = grep { $_ ne $k } @{ $this->{keys} }; delete $this->{data}{$k}; } sub CLEAR { @{$_[0]{keys}} = (); %{$_[0]{data}} = (); } 1; 