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]};
42 # Shut up a possible typo warning.
43 () = \%{$_[0].'::FIELDS'};
45 return \%{$_[0].'::FIELDS'};
51 return SUCCESS unless @_;
53 # List of base classes from which we will inherit %FIELDS.
56 my $inheritor = caller(0);
58 foreach my $base (@_) {
59 next if $inheritor->isa($base);
61 if (has_version($base)) {
62 ${$base.'::VERSION'} = '-1, set by base.pm'
63 unless defined ${$base.'::VERSION'};
66 local $SIG{__DIE__} = 'IGNORE';
68 # Only ignore "Can't locate" errors from our eval require.
69 # Other fatal errors (syntax etc) must be reported.
70 die if $@ && $@ !~ /^Can't locate .*? at \(eval /;
71 unless (%{"$base\::"}) {
74 Base class package "$base" is empty.
75 (Perhaps you need to 'use' the module which defines that package first.)
79 ${$base.'::VERSION'} = "-1, set by base.pm"
80 unless defined ${$base.'::VERSION'};
82 push @{"$inheritor\::ISA"}, $base;
84 if ( has_fields($base) || has_attr($base) ) {
85 # No multiple fields inheritence *suck*
88 Carp::croak("Can't multiply inherit %FIELDS");
95 if( defined $fields_base ) {
96 inherit_fields($inheritor, $fields_base);
102 my($derived, $base) = @_;
104 return SUCCESS unless $base;
106 my $battr = get_attr($base);
107 my $dattr = get_attr($derived);
108 my $dfields = get_fields($derived);
109 my $bfields = get_fields($base);
111 $dattr->[0] = @$battr;
113 if( keys %$dfields ) {
114 warn "$derived is inheriting from $base but already has its own ".
116 "This will cause problems.\n".
117 "Be sure you use base BEFORE declaring fields\n";
120 # Iterate through the base's fields adding all the non-private
121 # ones to the derived class. Hang on to the original attribute
122 # (Public, Private, etc...) and add Inherited.
123 # This is all too complicated to do efficiently with add_fields().
124 while (my($k,$v) = each %$bfields) {
126 if ($fno = $dfields->{$k} and $fno != $v) {
128 Carp::croak ("Inherited %FIELDS can't override existing %FIELDS");
131 if( $battr->[$v] & PRIVATE ) {
132 $dattr->[$v] = PRIVATE | INHERITED;
135 $dattr->[$v] = INHERITED | $battr->[$v];
140 unless( keys %$bfields ) {
141 foreach my $idx (1..$#{$battr}) {
142 $dattr->[$idx] = $battr->[$idx] & INHERITED;
154 base - Establish IS-A relationship with base classes at compile time
159 use base qw(Foo Bar);
163 Allows you to both load one or more modules, while setting up inheritance from
164 those modules at the same time. Roughly similar in effect to
170 push @ISA, qw(Foo Bar);
173 If any of the listed modules are not loaded yet, I<base> silently attempts to
174 C<require> them (and silently continues if the C<require> failed). Whether to
175 C<require> a base class module is determined by the absence of a global variable
176 $VERSION in the base package. If $VERSION is not detected even after loading
177 it, <base> will define $VERSION in the base package, setting it to the string
178 C<-1, set by base.pm>.
180 Will also initialize the fields if one of the base classes has it.
181 Multiple inheritence of fields is B<NOT> supported, if two or more
182 base classes each have inheritable fields the 'base' pragma will
183 croak. See L<fields>, L<public> and L<protected> for a description of
188 This module was introduced with Perl 5.004_04.
193 Due to the limitations of the implementation, you must use
194 base I<before> you declare any of your own fields.