My::Hash::* reimplementation for separate feature add-on packages (cleanup).
[MyWeb.git] / Hash.pm
1 # $Id$
2 # Hash tied to easily provide add-on features
3 # Copyright (C) 2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4
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
8
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.
13
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
17
18
19 package My::Hash;
20 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
21 our $CVS_ID=q$Id$;
22 use strict;
23 use warnings;
24 require Tie::Hash;      # Also for: Tie::StdHash
25 our @ISA=qw(Tie::Hash);
26 use Carp qw(cluck confess);
27
28
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(...)
33
34
35 sub new($$;@)
36 {
37 my($class,$parent,@classes)=@_;
38
39         unshift @classes,$class if $class ne __PACKAGE__;
40         for my $pkg (reverse @classes) {
41                 local $_=$pkg.".pm";
42                 s{::}{/}g;
43                 require $_;
44                 my %hash;
45                 tie %hash,$pkg,$parent;
46                 $parent=\%hash;
47                 }
48         return $parent;
49 }
50
51 sub TIEHASH($$)
52 {
53 my($class,$parent)=@_;
54
55         my $self=bless {},$class;
56         if (!ref(tied(%$parent))) {
57                 my %parent;
58                 tie %parent,"Tie::StdHash";
59                 %parent=%$parent;
60                 $parent=\%parent;
61                 }
62         $self->{"parent"}=$parent;
63         return $self;
64 }
65
66 sub pass($$@)
67 {
68 my($self,$funcname,@funcargs)=@_;
69
70         local $_=ref(tied(%{$self->{"parent"}}))."::".$funcname;
71         return if /^(?:Tie::StdHash)?::(?:DESTROY|UNTIE)$/;
72         tied(%{$self->{"parent"}})->$_(@funcargs);
73 }
74
75 our $AUTOLOAD;
76 sub AUTOLOAD
77 {
78 my($self,@rest)=@_;
79
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);
84 }
85
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..$#_]; }
89
90 1;