From f1192ceea6b2a126a4ff3254f91c2bc47c361c71 Mon Sep 17 00:00:00 2001 From: Gisle Aas Date: Mon, 29 Jun 1998 14:36:09 +0200 Subject: Re: [PATCH] Simplified magic_setisa() and improved fields.pm Message-Id: p4raw-id: //depot/perl@1266 --- lib/base.pm | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'lib/base.pm') diff --git a/lib/base.pm b/lib/base.pm index 4c4fb8b86b..3500cbfb89 100644 --- a/lib/base.pm +++ b/lib/base.pm @@ -5,7 +5,6 @@ base - Establish IS-A relationship with base class at compile time =head1 SYNOPSIS package Baz; - use base qw(Foo Bar); =head1 DESCRIPTION @@ -18,11 +17,19 @@ Roughly similar in effect to push @ISA, qw(Foo Bar); } +Will also initialize the %FIELDS hash if one of the base classes has +it. Multiple inheritance of %FIELDS is not supported. The 'base' +pragma will croak if multiple base classes has a %FIELDS hash. See +L for a description of this feature. + +When strict 'vars' is in scope I also let you assign to @ISA +without having to declare @ISA with the 'vars' pragma first. + This module was introduced with Perl 5.004_04. -=head1 BUGS +=head1 SEE ALSO -Needs proper documentation! +L =cut @@ -30,6 +37,7 @@ package base; sub import { my $class = shift; + my $fields_base; foreach my $base (@_) { unless (defined %{"$base\::"}) { @@ -44,9 +52,26 @@ sub import { "which defines that package first.)"); } } + + # A simple test like (defined %{"$base\::FIELDS"}) will + # sometimes produce typo warnings because it would create + # the hash if it was not present before. + my $fglob; + if ($fglob = ${"$base\::"}{"FIELDS"} and *$fglob{HASH}) { + if ($fields_base) { + require Carp; + Carp::croak("Can't multiply inherit %FIELDS"); + } else { + $fields_base = $base; + } + } + } + my $pkg = caller(0); + push @{"$pkg\::ISA"}, @_; + if ($fields_base) { + require fields; + fields::inherit($pkg, $fields_base); } - - push @{caller(0) . '::ISA'}, @_; } 1; -- cgit v1.2.1