5 fields - compile-time class fields
11 use fields qw(foo bar _private);
14 my Foo $var = new Foo;
17 # This will generate a compile-time error.
23 use fields 'bar'; # hides Foo->{bar}
24 use fields qw(baz _private); # not shared with Foo
29 The C<fields> pragma enables compile-time verified class fields. It
30 does so by updating the %FIELDS hash in the calling package.
32 If a typed lexical variable holding a reference is used to access a
33 hash element and the %FIELDS hash of the given type exists, then the
34 operation is turned into an array access at compile time. The %FIELDS
35 hash maps from hash element names to the array indices. If the hash
36 element is not present in the %FIELDS hash, then a compile-time error
39 Since the %FIELDS hash is used at compile-time, it must be set up at
40 compile-time too. This is made easier with the help of the 'fields'
41 and the 'base' pragma modules. The 'base' pragma will copy fields
42 from base classes and the 'fields' pragma adds new fields. Field
43 names that start with an underscore character are made private to a
44 class and are not visible to subclasses. Inherited fields can be
45 overridden but will generate a warning if used together with the C<-w>
48 The effect of all this is that you can have objects with named fields
49 which are as compact and as fast arrays to access. This only works
50 as long as the objects are accessed through properly typed variables.
51 For untyped access to work you have to make sure that a reference to
52 the proper %FIELDS hash is assigned to the 0'th element of the array
53 object (so that the objects can be treated like an pseudo-hash). A
54 constructor like this does the job:
60 my $self = bless [\%{"$class\::FIELDS"}], $class;
68 L<perlref/Pseudo-hashes: Using an array as a hash>
83 # The %attr hash holds the attributes of the currently assigned fields
84 # per class. The hash is indexed by class names and the hash value is
85 # an array reference. The first element in the array is the lowest field
86 # number not belonging to a base class. The remaining elements' indices
87 # are the field numbers. The values are integer bit masks, or undef
88 # in the case of base class private fields (which occupy a slot but are
89 # otherwise irrelevant to the class).
94 my $package = caller(0);
95 my $fields = \%{"$package\::FIELDS"};
96 my $fattr = ($attr{$package} ||= [1]);
99 if ($next > $fattr->[0]
100 and ($fields->{$_[0]} || 0) >= $fattr->[0])
102 # There are already fields not belonging to base classes.
103 # Looks like a possible module reload...
107 my $fno = $fields->{$f};
109 # Allow the module to be reloaded so long as field positions
111 if ($fno and $fno != $next) {
113 if ($fno < $fattr->[0]) {
114 Carp::carp("Hides field '$f' in base class") if $^W;
116 Carp::croak("Field name '$f' already in use");
119 $fields->{$f} = $next;
120 $fattr->[$next] = ($f =~ /^_/) ? _PRIVATE : _PUBLIC;
123 if (@$fattr > $next) {
124 # Well, we gave them the benefit of the doubt by guessing the
125 # module was reloaded, but they appear to be declaring fields
126 # in more than one place. We can't be sure (without some extra
127 # bookkeeping) that the rest of the fields will be declared or
128 # have the same positions, so punt.
130 Carp::croak ("Reloaded module must declare all fields at once");
134 sub inherit # called by base.pm when $base_fields is nonempty
136 my($derived, $base) = @_;
137 my $base_attr = $attr{$base};
138 my $derived_attr = $attr{$derived} ||= [];
139 my $base_fields = \%{"$base\::FIELDS"};
140 my $derived_fields = \%{"$derived\::FIELDS"};
142 $derived_attr->[0] = $base_attr ? scalar(@$base_attr) : 1;
143 while (my($k,$v) = each %$base_fields) {
145 if ($fno = $derived_fields->{$k} and $fno != $v) {
147 Carp::croak ("Inherited %FIELDS can't override existing %FIELDS");
149 if ($base_attr->[$v] & _PRIVATE) {
150 $derived_attr->[$v] = undef;
152 $derived_attr->[$v] = $base_attr->[$v];
153 $derived_fields->{$k} = $v;
158 sub _dump # sometimes useful for debugging
160 for my $pkg (sort keys %attr) {
162 if (@{"$pkg\::ISA"}) {
163 print " (", join(", ", @{"$pkg\::ISA"}), ")";
166 my $fields = \%{"$pkg\::FIELDS"};
167 for my $f (sort {$fields->{$a} <=> $fields->{$b}} keys %$fields) {
168 my $no = $fields->{$f};
170 my $fattr = $attr{$pkg}[$no];
171 if (defined $fattr) {
173 push(@a, "public") if $fattr & _PUBLIC;
174 push(@a, "private") if $fattr & _PRIVATE;
175 push(@a, "inherited") if $no < $attr{$pkg}[0];
176 print "\t(", join(", ", @a), ")";