fix diagnostics to report "our" vs "my" correctly
[p5sagit/p5-mst-13.2.git] / lib / base.pm
CommitLineData
fb73857a 1=head1 NAME
2
3base - Establish IS-A relationship with base class at compile time
4
5=head1 SYNOPSIS
6
7 package Baz;
fb73857a 8 use base qw(Foo Bar);
9
10=head1 DESCRIPTION
11
12Roughly similar in effect to
13
14 BEGIN {
15 require Foo;
16 require Bar;
17 push @ISA, qw(Foo Bar);
18 }
19
f1192cee 20Will also initialize the %FIELDS hash if one of the base classes has
21it. Multiple inheritance of %FIELDS is not supported. The 'base'
b8bc843f 22pragma will croak if multiple base classes have a %FIELDS hash. See
f1192cee 23L<fields> for a description of this feature.
24
25When strict 'vars' is in scope I<base> also let you assign to @ISA
26without having to declare @ISA with the 'vars' pragma first.
27
b8bc843f 28If any of the base classes are not loaded yet, I<base> silently
29C<require>s them. Whether to C<require> a base class package is
30determined by the absence of a global $VERSION in the base package.
31If $VERSION is not detected even after loading it, <base> will
32define $VERSION in the base package, setting it to the string
33C<-1, defined by base.pm>.
34
35=head1 HISTORY
36
fb73857a 37This module was introduced with Perl 5.004_04.
38
f1192cee 39=head1 SEE ALSO
fb73857a 40
f1192cee 41L<fields>
fb73857a 42
43=cut
44
45package base;
b8bc843f 46use vars qw($VERSION);
f30a1143 47$VERSION = "1.01";
fb73857a 48
49sub import {
50 my $class = shift;
f1192cee 51 my $fields_base;
f30a1143 52 my $pkg = caller(0);
fb73857a 53
54 foreach my $base (@_) {
f30a1143 55 next if $pkg->isa($base);
56 push @{"$pkg\::ISA"}, $base;
b8bc843f 57 unless (exists ${"$base\::"}{VERSION}) {
fb73857a 58 eval "require $base";
9b599b2a 59 # Only ignore "Can't locate" errors from our eval require.
60 # Other fatal errors (syntax etc) must be reported.
61 die if $@ && $@ !~ /^Can't locate .*? at \(eval /;
c5be433b 62 unless (%{"$base\::"}) {
fb73857a 63 require Carp;
64 Carp::croak("Base class package \"$base\" is empty.\n",
65 "\t(Perhaps you need to 'use' the module ",
66 "which defines that package first.)");
67 }
b8bc843f 68 ${"$base\::VERSION"} = "-1, set by base.pm"
69 unless exists ${"$base\::"}{VERSION};
fb73857a 70 }
f1192cee 71
72 # A simple test like (defined %{"$base\::FIELDS"}) will
73 # sometimes produce typo warnings because it would create
74 # the hash if it was not present before.
75 my $fglob;
76 if ($fglob = ${"$base\::"}{"FIELDS"} and *$fglob{HASH}) {
77 if ($fields_base) {
78 require Carp;
79 Carp::croak("Can't multiply inherit %FIELDS");
80 } else {
81 $fields_base = $base;
82 }
83 }
84 }
f1192cee 85 if ($fields_base) {
86 require fields;
87 fields::inherit($pkg, $fields_base);
fb73857a 88 }
fb73857a 89}
90
911;