Fixed some (Linux kernel?) compatibility problem of flock(1) of a directory.
[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.pm calls instead of: pass(...)
33
34
35 # My::Hash::KIND->new($parent,@optional_KIND_arguments);
36 # My::Hash->new($parent,"My::Hash::KIND_OUTER",["My::Hash::KIND_INNER",@optional_KIND_INNER_arguments]);
37 sub new($$;@)
38 {
39 my($class,$parent,@classes)=@_;
40
41         @classes=[$class,@classes] if $class ne __PACKAGE__ || !@classes;
42         for my $item (reverse @classes) {
43                 my $pkg=(!ref($item) ? $item : $item->[0]);
44                 my @args=@{$item}[1..$#$item] if ref $item;
45                 local $_=$pkg.".pm";
46                 s{::}{/}g;
47                 require $_;
48                 my %hash;
49                 tie %hash,$pkg,$parent,@args;
50                 $parent=\%hash;
51                 }
52         return $parent;
53 }
54
55 sub TIEHASH($$)
56 {
57 my($class,$parent)=@_;
58
59         my $self=bless {},$class;
60         if (!ref(tied(%$parent))) {
61                 my %parent;
62                 tie %parent,"Tie::StdHash";
63                 %parent=%$parent;
64                 $parent=\%parent;
65                 }
66         $self->{"parent"}=$parent;
67         return $self;
68 }
69
70 sub pass($$@)
71 {
72 my($self,$funcname,@funcargs)=@_;
73
74         local $@=undef();
75         my $parent_tied=tied(%{$self->{"parent"}});
76         my $destroy=1 if $funcname eq "DESTROY" || $funcname eq "UNTIE";
77         return if !defined($parent_tied) && $destroy;
78         my $r=eval {
79                 $parent_tied->$funcname(@funcargs);
80                 };
81         return $r if !$@ || ($destroy && $@=~/^Can't locate object method "$funcname" via package ".*" at /);
82         confess $@;
83 }
84
85 our $AUTOLOAD;
86 sub AUTOLOAD
87 {
88 my($self,@rest)=@_;
89
90         my $ref_self=ref($self);
91         cluck "Invalid AUTOLOAD prefix (ref_self=\"$ref_self\"): $AUTOLOAD"
92                         if $AUTOLOAD!~s{^\Q$ref_self\E::}{};
93         return $self->pass($AUTOLOAD,@rest);
94 }
95
96 # Skip over existing &Tie::Hash::EXISTS and &Tie::Hash::CLEAR to: &AUTOLOAD
97 sub EXISTS { return pass $_[0],"EXISTS",@_[1..$#_]; }
98 sub CLEAR  { return pass $_[0],"CLEAR" ,@_[1..$#_]; }
99
100 1;