File Coverage

blib/lib/base.pm
Criterion Covered Total %
statement 0 73 0.0
branch 0 32 0.0
condition 0 19 0.0
subroutine 0 7 0.0
pod 0 4 0.0
total 0 135 0.0


line stmt bran cond sub pod time code
1             use 5.008;
2             package base;
3              
4             use strict 'vars';
5             use vars qw($VERSION);
6             $VERSION = '2.23';
7             $VERSION =~ tr/_//d;
8              
9             # constant.pm is slow
10             sub SUCCESS () { 1 }
11              
12             sub PUBLIC () { 2**0 }
13             sub PRIVATE () { 2**1 }
14             sub INHERITED () { 2**2 }
15             sub PROTECTED () { 2**3 }
16              
17              
18             my $Fattr = \%fields::attr;
19              
20             sub has_fields {
21 0     0 0   my($base) = shift;
22 0           my $fglob = ${"$base\::"}{FIELDS};
  0            
23 0 0 0       return( ($fglob && 'GLOB' eq ref($fglob) && *$fglob{HASH}) ? 1 : 0 );
24             }
25              
26             sub has_attr {
27 0     0 0   my($proto) = shift;
28 0   0       my($class) = ref $proto || $proto;
29 0           return exists $Fattr->{$class};
30             }
31              
32             sub get_attr {
33 0 0   0 0   $Fattr->{$_[0]} = [1] unless $Fattr->{$_[0]};
34 0           return $Fattr->{$_[0]};
35             }
36              
37             if ($] < 5.009) {
38             *get_fields = sub {
39             # Shut up a possible typo warning.
40             () = \%{$_[0].'::FIELDS'};
41             my $f = \%{$_[0].'::FIELDS'};
42              
43             # should be centralized in fields? perhaps
44             # fields::mk_FIELDS_be_OK. Peh. As long as %{ $package . '::FIELDS' }
45             # is used here anyway, it doesn't matter.
46             bless $f, 'pseudohash' if (ref($f) ne 'pseudohash');
47              
48             return $f;
49             }
50             }
51             else {
52             *get_fields = sub {
53             # Shut up a possible typo warning.
54 0     0     () = \%{$_[0].'::FIELDS'};
  0            
55 0           return \%{$_[0].'::FIELDS'};
  0            
56             }
57             }
58              
59             if ($] < 5.008) {
60             *_module_to_filename = sub {
61             (my $fn = $_[0]) =~ s!::!/!g;
62             $fn .= '.pm';
63             return $fn;
64             }
65             }
66             else {
67             *_module_to_filename = sub {
68 0     0     (my $fn = $_[0]) =~ s!::!/!g;
69 0           $fn .= '.pm';
70 0           utf8::encode($fn);
71 0           return $fn;
72             }
73             }
74              
75              
76             sub import {
77 0     0     my $class = shift;
78              
79 0 0         return SUCCESS unless @_;
80              
81             # List of base classes from which we will inherit %FIELDS.
82 0           my $fields_base;
83              
84 0           my $inheritor = caller(0);
85              
86 0           my @bases;
87 0           foreach my $base (@_) {
88 0 0         if ( $inheritor eq $base ) {
89 0           warn "Class '$inheritor' tried to inherit from itself\n";
90             }
91              
92 0 0         next if grep $_->isa($base), ($inheritor, @bases);
93              
94             # Following blocks help isolate $SIG{__DIE__} changes
95             {
96 0           my $sigdie;
  0            
97             {
98 0           local $SIG{__DIE__};
  0            
99 0           my $fn = _module_to_filename($base);
100 0           eval { require $fn };
  0            
101             # Only ignore "Can't locate" errors from our eval require.
102             # Other fatal errors (syntax etc) must be reported.
103             #
104             # changing the check here is fragile - if the check
105             # here isn't catching every error you want, you should
106             # probably be using parent.pm, which doesn't try to
107             # guess whether require is needed or failed,
108             # see [perl #118561]
109 0 0 0       die if $@ && $@ !~ /^Can't locate \Q$fn\E .*? at .* line [0-9]+(?:, <[^>]*> (?:line|chunk) [0-9]+)?\.\n\z/s
      0        
110             || $@ =~ /Compilation failed in require at .* line [0-9]+(?:, <[^>]*> (?:line|chunk) [0-9]+)?\.\n\z/;
111 0 0         unless (%{"$base\::"}) {
  0            
112 0           require Carp;
113 0           local $" = " ";
114 0           Carp::croak(<
115             Base class package "$base" is empty.
116             (Perhaps you need to 'use' the module which defines that package first,
117             or make that module available in \@INC (\@INC contains: @INC).
118             ERROR
119             }
120 0   0       $sigdie = $SIG{__DIE__} || undef;
121             }
122             # Make sure a global $SIG{__DIE__} makes it out of the localization.
123 0 0         $SIG{__DIE__} = $sigdie if defined $sigdie;
124             }
125 0           push @bases, $base;
126              
127 0 0 0       if ( has_fields($base) || has_attr($base) ) {
128             # No multiple fields inheritance *suck*
129 0 0         if ($fields_base) {
130 0           require Carp;
131 0           Carp::croak("Can't multiply inherit fields");
132             } else {
133 0           $fields_base = $base;
134             }
135             }
136             }
137             # Save this until the end so it's all or nothing if the above loop croaks.
138 0           push @{"$inheritor\::ISA"}, @bases;
  0            
139              
140 0 0         if( defined $fields_base ) {
141 0           inherit_fields($inheritor, $fields_base);
142             }
143             }
144              
145              
146             sub inherit_fields {
147 0     0 0   my($derived, $base) = @_;
148              
149 0 0         return SUCCESS unless $base;
150              
151 0           my $battr = get_attr($base);
152 0           my $dattr = get_attr($derived);
153 0           my $dfields = get_fields($derived);
154 0           my $bfields = get_fields($base);
155              
156 0           $dattr->[0] = @$battr;
157              
158 0 0         if( keys %$dfields ) {
159 0           warn <<"END";
160             $derived is inheriting from $base but already has its own fields!
161             This will cause problems. Be sure you use base BEFORE declaring fields.
162             END
163              
164             }
165              
166             # Iterate through the base's fields adding all the non-private
167             # ones to the derived class. Hang on to the original attribute
168             # (Public, Private, etc...) and add Inherited.
169             # This is all too complicated to do efficiently with add_fields().
170 0           while (my($k,$v) = each %$bfields) {
171 0           my $fno;
172 0 0 0       if ($fno = $dfields->{$k} and $fno != $v) {
173 0           require Carp;
174 0           Carp::croak ("Inherited fields can't override existing fields");
175             }
176              
177 0 0         if( $battr->[$v] & PRIVATE ) {
178 0           $dattr->[$v] = PRIVATE | INHERITED;
179             }
180             else {
181 0           $dattr->[$v] = INHERITED | $battr->[$v];
182 0           $dfields->{$k} = $v;
183             }
184             }
185              
186 0           foreach my $idx (1..$#{$battr}) {
  0            
187 0 0         next if defined $dattr->[$idx];
188 0           $dattr->[$idx] = $battr->[$idx] & INHERITED;
189             }
190             }
191              
192              
193             1;
194              
195             __END__