summaryrefslogtreecommitdiff
path: root/lib/Class
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Class')
-rw-r--r--lib/Class/Fields.pm33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/Class/Fields.pm b/lib/Class/Fields.pm
new file mode 100644
index 0000000000..4b23e7d731
--- /dev/null
+++ b/lib/Class/Fields.pm
@@ -0,0 +1,33 @@
+package Class::Fields;
+use Carp;
+
+sub import {
+ my $class = shift;
+ my ($package) = caller;
+ my $fields = \%{"$package\::FIELDS"};
+ my $i = $fields->{__MAX__};
+ foreach my $f (@_) {
+ if (defined($fields->{$f})) {
+ croak "Field name $f already used by a base class"
+ }
+ $fields->{$f} = ++$i;
+ }
+ $fields->{__MAX__} = $i;
+ push(@{"$package\::ISA"}, "Class::Fields");
+}
+
+sub new {
+ my $class = shift;
+ bless [\%{"$class\::FIELDS"}, @_], $class;
+}
+
+sub ISA {
+ my ($class, $package) = @_;
+ my $from_fields = \%{"$class\::FIELDS"};
+ my $to_fields = \%{"$package\::FIELDS"};
+ return unless defined %$from_fields;
+ croak "Ambiguous inheritance for %FIELDS" if defined %$to_fields;
+ %$to_fields = %$from_fields;
+}
+
+1;