1N/Apackage base;
1N/A
1N/Ause strict 'vars';
1N/Ause vars qw($VERSION);
1N/A$VERSION = '2.05';
1N/A
1N/A# constant.pm is slow
1N/Asub SUCCESS () { 1 }
1N/A
1N/Asub PUBLIC () { 2**0 }
1N/Asub PRIVATE () { 2**1 }
1N/Asub INHERITED () { 2**2 }
1N/Asub PROTECTED () { 2**3 }
1N/A
1N/A
1N/Amy $Fattr = \%fields::attr;
1N/A
1N/Asub has_fields {
1N/A my($base) = shift;
1N/A my $fglob = ${"$base\::"}{FIELDS};
1N/A return( ($fglob && *$fglob{HASH}) ? 1 : 0 );
1N/A}
1N/A
1N/Asub has_version {
1N/A my($base) = shift;
1N/A my $vglob = ${$base.'::'}{VERSION};
1N/A return( ($vglob && *$vglob{SCALAR}) ? 1 : 0 );
1N/A}
1N/A
1N/Asub has_attr {
1N/A my($proto) = shift;
1N/A my($class) = ref $proto || $proto;
1N/A return exists $Fattr->{$class};
1N/A}
1N/A
1N/Asub get_attr {
1N/A $Fattr->{$_[0]} = [1] unless $Fattr->{$_[0]};
1N/A return $Fattr->{$_[0]};
1N/A}
1N/A
1N/Aif ($] < 5.009) {
1N/A *get_fields = sub {
1N/A # Shut up a possible typo warning.
1N/A () = \%{$_[0].'::FIELDS'};
1N/A my $f = \%{$_[0].'::FIELDS'};
1N/A
1N/A # should be centralized in fields? perhaps
1N/A # fields::mk_FIELDS_be_OK. Peh. As long as %{ $package . '::FIELDS' }
1N/A # is used here anyway, it doesn't matter.
1N/A bless $f, 'pseudohash' if (ref($f) ne 'pseudohash');
1N/A
1N/A return $f;
1N/A }
1N/A}
1N/Aelse {
1N/A *get_fields = sub {
1N/A # Shut up a possible typo warning.
1N/A () = \%{$_[0].'::FIELDS'};
1N/A return \%{$_[0].'::FIELDS'};
1N/A }
1N/A}
1N/A
1N/Asub import {
1N/A my $class = shift;
1N/A
1N/A return SUCCESS unless @_;
1N/A
1N/A # List of base classes from which we will inherit %FIELDS.
1N/A my $fields_base;
1N/A
1N/A my $inheritor = caller(0);
1N/A
1N/A foreach my $base (@_) {
1N/A next if $inheritor->isa($base);
1N/A
1N/A if (has_version($base)) {
1N/A ${$base.'::VERSION'} = '-1, set by base.pm'
1N/A unless defined ${$base.'::VERSION'};
1N/A }
1N/A else {
1N/A local $SIG{__DIE__} = 'IGNORE';
1N/A eval "require $base";
1N/A # Only ignore "Can't locate" errors from our eval require.
1N/A # Other fatal errors (syntax etc) must be reported.
1N/A die if $@ && $@ !~ /^Can't locate .*? at \(eval /;
1N/A unless (%{"$base\::"}) {
1N/A require Carp;
1N/A Carp::croak(<<ERROR);
1N/ABase class package "$base" is empty.
1N/A (Perhaps you need to 'use' the module which defines that package first.)
1N/AERROR
1N/A
1N/A }
1N/A ${$base.'::VERSION'} = "-1, set by base.pm"
1N/A unless defined ${$base.'::VERSION'};
1N/A }
1N/A push @{"$inheritor\::ISA"}, $base;
1N/A
1N/A if ( has_fields($base) || has_attr($base) ) {
1N/A # No multiple fields inheritence *suck*
1N/A if ($fields_base) {
1N/A require Carp;
1N/A Carp::croak("Can't multiply inherit %FIELDS");
1N/A } else {
1N/A $fields_base = $base;
1N/A }
1N/A }
1N/A }
1N/A
1N/A if( defined $fields_base ) {
1N/A inherit_fields($inheritor, $fields_base);
1N/A }
1N/A}
1N/A
1N/A
1N/Asub inherit_fields {
1N/A my($derived, $base) = @_;
1N/A
1N/A return SUCCESS unless $base;
1N/A
1N/A my $battr = get_attr($base);
1N/A my $dattr = get_attr($derived);
1N/A my $dfields = get_fields($derived);
1N/A my $bfields = get_fields($base);
1N/A
1N/A $dattr->[0] = @$battr;
1N/A
1N/A if( keys %$dfields ) {
1N/A warn "$derived is inheriting from $base but already has its own ".
1N/A "fields!\n".
1N/A "This will cause problems.\n".
1N/A "Be sure you use base BEFORE declaring fields\n";
1N/A }
1N/A
1N/A # Iterate through the base's fields adding all the non-private
1N/A # ones to the derived class. Hang on to the original attribute
1N/A # (Public, Private, etc...) and add Inherited.
1N/A # This is all too complicated to do efficiently with add_fields().
1N/A while (my($k,$v) = each %$bfields) {
1N/A my $fno;
1N/A if ($fno = $dfields->{$k} and $fno != $v) {
1N/A require Carp;
1N/A Carp::croak ("Inherited %FIELDS can't override existing %FIELDS");
1N/A }
1N/A
1N/A if( $battr->[$v] & PRIVATE ) {
1N/A $dattr->[$v] = PRIVATE | INHERITED;
1N/A }
1N/A else {
1N/A $dattr->[$v] = INHERITED | $battr->[$v];
1N/A $dfields->{$k} = $v;
1N/A }
1N/A }
1N/A
1N/A unless( keys %$bfields ) {
1N/A foreach my $idx (1..$#{$battr}) {
1N/A $dattr->[$idx] = $battr->[$idx] & INHERITED;
1N/A }
1N/A }
1N/A}
1N/A
1N/A
1N/A1;
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/Abase - Establish IS-A relationship with base classes at compile time
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A package Baz;
1N/A use base qw(Foo Bar);
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AAllows you to both load one or more modules, while setting up inheritance from
1N/Athose modules at the same time. Roughly similar in effect to
1N/A
1N/A package Baz;
1N/A BEGIN {
1N/A require Foo;
1N/A require Bar;
1N/A push @ISA, qw(Foo Bar);
1N/A }
1N/A
1N/AIf any of the listed modules are not loaded yet, I<base> silently attempts to
1N/AC<require> them (and silently continues if the C<require> failed). Whether to
1N/AC<require> a base class module is determined by the absence of a global variable
1N/A$VERSION in the base package. If $VERSION is not detected even after loading
1N/Ait, <base> will define $VERSION in the base package, setting it to the string
1N/AC<-1, set by base.pm>.
1N/A
1N/AWill also initialize the fields if one of the base classes has it.
1N/AMultiple inheritence of fields is B<NOT> supported, if two or more
1N/Abase classes each have inheritable fields the 'base' pragma will
1N/Acroak. See L<fields>, L<public> and L<protected> for a description of
1N/Athis feature.
1N/A
1N/A=head1 HISTORY
1N/A
1N/AThis module was introduced with Perl 5.004_04.
1N/A
1N/A
1N/A=head1 CAVEATS
1N/A
1N/ADue to the limitations of the implementation, you must use
1N/Abase I<before> you declare any of your own fields.
1N/A
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<fields>
1N/A
1N/A=cut