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