2 # Hash tied to easily provide add-on features
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
20 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
24 require Tie::Hash; # Also for: Tie::StdHash
25 our @ISA=qw(Tie::Hash);
26 use Carp qw(cluck confess);
29 # OK, this is just @ISA 'runtime' reimplementation using: ->{"parent"}
30 # Creating classes each time is too complicated in Perl.
31 # Maybe the best would be run-time generation of the classes using: @ISA
32 # This way the feature implementations could use SUPER::... instead of: pass(...)
37 my($class,$parent,@classes)=@_;
39 unshift @classes,$class if $class ne __PACKAGE__;
40 for my $pkg (reverse @classes) {
45 tie %hash,$pkg,$parent;
53 my($class,$parent)=@_;
55 my $self=bless {},$class;
56 if (!ref(tied(%$parent))) {
58 tie %parent,"Tie::StdHash";
62 $self->{"parent"}=$parent;
68 my($self,$funcname,@funcargs)=@_;
70 local $_=ref(tied(%{$self->{"parent"}}))."::".$funcname;
71 return if /^(?:Tie::StdHash)?::(?:DESTROY|UNTIE)$/;
72 tied(%{$self->{"parent"}})->$_(@funcargs);
80 my $ref_self=ref($self);
81 cluck "Invalid AUTOLOAD prefix (ref_self=\"$ref_self\"): $AUTOLOAD"
82 if $AUTOLOAD!~s{^\Q$ref_self\E::}{};
83 return $self->pass($AUTOLOAD,@rest);
86 # Skip over existing &Tie::Hash::EXISTS and &Tie::Hash::CLEAR to: &AUTOLOAD
87 sub EXISTS { return pass $_[0],"EXISTS",@_[1..$#_]; }
88 sub CLEAR { return pass $_[0],"CLEAR" ,@_[1..$#_]; }