10 sub PUBLIC () { 2**0 }
11 sub PRIVATE () { 2**1 }
12 sub INHERITED () { 2**2 }
13 sub PROTECTED () { 2**3 }
16 my $Fattr = \%fields::attr;
20 my $fglob = ${"$base\::"}{FIELDS};
21 return( ($fglob && *$fglob{HASH}) ? 1 : 0 );
26 my $vglob = ${$base.'::'}{VERSION};
27 return( ($vglob && *$vglob{SCALAR}) ? 1 : 0 );
32 my($class) = ref $proto || $proto;
33 return exists $Fattr->{$class};
37 $Fattr->{$_[0]} = [1] unless $Fattr->{$_[0]};
38 return $Fattr->{$_[0]};
43 # Shut up a possible typo warning.
44 () = \%{$_[0].'::FIELDS'};
45 my $f = \%{$_[0].'::FIELDS'};
47 # should be centralized in fields? perhaps
48 # fields::mk_FIELDS_be_OK. Peh. As long as %{ $package . '::FIELDS' }
49 # is used here anyway, it doesn't matter.
50 bless $f, 'pseudohash' if (ref($f) ne 'pseudohash');
57 # Shut up a possible typo warning.
58 () = \%{$_[0].'::FIELDS'};
59 return \%{$_[0].'::FIELDS'};
66 return SUCCESS unless @_;
68 # List of base classes from which we will inherit %FIELDS.
71 my $inheritor = caller(0);
73 foreach my $base (@_) {
74 if ( $inheritor eq $base ) {
75 warn "Class '$inheritor' tried to inherit from itself\n";
78 next if $inheritor->isa($base);
80 if (has_version($base)) {
81 ${$base.'::VERSION'} = '-1, set by base.pm'
82 unless defined ${$base.'::VERSION'};
89 # Only ignore "Can't locate" errors from our eval require.
90 # Other fatal errors (syntax etc) must be reported.
91 die if $@ && $@ !~ /^Can't locate .*? at \(eval /;
92 unless (%{"$base\::"}) {
95 Base class package "$base" is empty.
96 (Perhaps you need to 'use' the module which defines that package first.)
99 $sigdie = $SIG{__DIE__};
101 # Make sure a global $SIG{__DIE__} makes it out of the localization.
102 $SIG{__DIE__} = $sigdie if defined $sigdie;
103 ${$base.'::VERSION'} = "-1, set by base.pm"
104 unless defined ${$base.'::VERSION'};
106 push @{"$inheritor\::ISA"}, $base;
108 if ( has_fields($base) || has_attr($base) ) {
109 # No multiple fields inheritance *suck*
112 Carp::croak("Can't multiply inherit %FIELDS");
114 $fields_base = $base;
119 if( defined $fields_base ) {
120 inherit_fields($inheritor, $fields_base);
126 my($derived, $base) = @_;
128 return SUCCESS unless $base;
130 my $battr = get_attr($base);
131 my $dattr = get_attr($derived);
132 my $dfields = get_fields($derived);
133 my $bfields = get_fields($base);
135 $dattr->[0] = @$battr;
137 if( keys %$dfields ) {
138 warn "$derived is inheriting from $base but already has its own ".
140 "This will cause problems.\n".
141 "Be sure you use base BEFORE declaring fields\n";
144 # Iterate through the base's fields adding all the non-private
145 # ones to the derived class. Hang on to the original attribute
146 # (Public, Private, etc...) and add Inherited.
147 # This is all too complicated to do efficiently with add_fields().
148 while (my($k,$v) = each %$bfields) {
150 if ($fno = $dfields->{$k} and $fno != $v) {
152 Carp::croak ("Inherited %FIELDS can't override existing %FIELDS");
155 if( $battr->[$v] & PRIVATE ) {
156 $dattr->[$v] = PRIVATE | INHERITED;
159 $dattr->[$v] = INHERITED | $battr->[$v];
164 foreach my $idx (1..$#{$battr}) {
165 next if defined $dattr->[$idx];
166 $dattr->[$idx] = $battr->[$idx] & INHERITED;
177 base - Establish IS-A relationship with base classes at compile time
182 use base qw(Foo Bar);
186 Allows you to both load one or more modules, while setting up inheritance from
187 those modules at the same time. Roughly similar in effect to
193 push @ISA, qw(Foo Bar);
196 If any of the listed modules are not loaded yet, I<base> silently attempts to
197 C<require> them (and silently continues if the C<require> failed). Whether to
198 C<require> a base class module is determined by the absence of a global variable
199 $VERSION in the base package. If $VERSION is not detected even after loading
200 it, <base> will define $VERSION in the base package, setting it to the string
201 C<-1, set by base.pm>.
203 Will also initialize the fields if one of the base classes has it.
204 Multiple inheritance of fields is B<NOT> supported, if two or more
205 base classes each have inheritable fields the 'base' pragma will
206 croak. See L<fields>, L<public> and L<protected> for a description of
213 =item Base class package "%s" is empty.
215 base.pm was unable to require the base package, because it was not
222 This module was introduced with Perl 5.004_04.
224 Attempting to inherit from yourself generates a warning:
229 # Class 'Foo' tried to inherit from itself
233 Due to the limitations of the implementation, you must use
234 base I<before> you declare any of your own fields.