This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
remove dead code
[perl5.git] / lib / base.pm
... / ...
CommitLineData
1=head1 NAME
2
3base - Establish IS-A relationship with base class at compile time
4
5=head1 SYNOPSIS
6
7 package Baz;
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
20Will also initialize the %FIELDS hash if one of the base classes has
21it. Multiple inheritance of %FIELDS is not supported. The 'base'
22pragma will croak if multiple base classes have a %FIELDS hash. See
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
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
37This module was introduced with Perl 5.004_04.
38
39=head1 SEE ALSO
40
41L<fields>
42
43=cut
44
45package base;
46use vars qw($VERSION);
47$VERSION = "1.00";
48
49sub import {
50 my $class = shift;
51 my $fields_base;
52
53 foreach my $base (@_) {
54 unless (exists ${"$base\::"}{VERSION}) {
55 eval "require $base";
56 # Only ignore "Can't locate" errors from our eval require.
57 # Other fatal errors (syntax etc) must be reported.
58 die if $@ && $@ !~ /^Can't locate .*? at \(eval /;
59 unless (defined %{"$base\::"}) {
60 require Carp;
61 Carp::croak("Base class package \"$base\" is empty.\n",
62 "\t(Perhaps you need to 'use' the module ",
63 "which defines that package first.)");
64 }
65 ${"$base\::VERSION"} = "-1, set by base.pm"
66 unless exists ${"$base\::"}{VERSION};
67 }
68
69 # A simple test like (defined %{"$base\::FIELDS"}) will
70 # sometimes produce typo warnings because it would create
71 # the hash if it was not present before.
72 my $fglob;
73 if ($fglob = ${"$base\::"}{"FIELDS"} and *$fglob{HASH}) {
74 if ($fields_base) {
75 require Carp;
76 Carp::croak("Can't multiply inherit %FIELDS");
77 } else {
78 $fields_base = $base;
79 }
80 }
81 }
82 my $pkg = caller(0);
83 push @{"$pkg\::ISA"}, @_;
84 if ($fields_base) {
85 require fields;
86 fields::inherit($pkg, $fields_base);
87 }
88}
89
901;