1fields(3pm) Perl Programmers Reference Guide fields(3pm)
2
3
4
6 fields - compile-time class fields
7
9 {
10 package Foo;
11 use fields qw(foo bar _Foo_private);
12 sub new {
13 my Foo $self = shift;
14 unless (ref $self) {
15 $self = fields::new($self);
16 $self->{_Foo_private} = "this is Foo's secret";
17 }
18 $self->{foo} = 10;
19 $self->{bar} = 20;
20 return $self;
21 }
22 }
23
24 my $var = Foo->new;
25 $var->{foo} = 42;
26
27 # this will generate a run-time error
28 $var->{zap} = 42;
29
30 # this will generate a compile-time error
31 my Foo $foo = Foo->new;
32 $foo->{zap} = 24;
33
34 # subclassing
35 {
36 package Bar;
37 use base 'Foo';
38 use fields qw(baz _Bar_private); # not shared with Foo
39 sub new {
40 my $class = shift;
41 my $self = fields::new($class);
42 $self->SUPER::new(); # init base fields
43 $self->{baz} = 10; # init own fields
44 $self->{_Bar_private} = "this is Bar's secret";
45 return $self;
46 }
47 }
48
50 The "fields" pragma enables compile-time and run-time verified class
51 fields.
52
53 NOTE: The current implementation keeps the declared fields in the
54 %FIELDS hash of the calling package, but this may change in future
55 versions. Do not update the %FIELDS hash directly, because it must be
56 created at compile-time for it to be fully useful, as is done by this
57 pragma.
58
59 If a typed lexical variable ("my Class $var") holding a reference is
60 used to access a hash element and a package with the same name as the
61 type has declared class fields using this pragma, then the hash key is
62 verified at compile time. If the variables are not typed, access is
63 only checked at run time.
64
65 The related "base" pragma will combine fields from base classes and any
66 fields declared using the "fields" pragma. This enables field
67 inheritance to work properly. Inherited fields can be overridden but
68 will generate a warning if warnings are enabled.
69
70 Only valid for Perl 5.8.x and earlier: Field names that start with an
71 underscore character are made private to the class and are not visible
72 to subclasses.
73
74 Also, in Perl 5.8.x and earlier, this pragma uses pseudo-hashes, the
75 effect being that you can have objects with named fields which are as
76 compact and as fast arrays to access, as long as the objects are
77 accessed through properly typed variables.
78
79 The following functions are supported:
80
81 new fields::new() creates and blesses a hash comprised of the fields
82 declared using the "fields" pragma into the specified class. It is
83 the recommended way to construct a fields-based object.
84
85 This makes it possible to write a constructor like this:
86
87 package Critter::Sounds;
88 use fields qw(cat dog bird);
89
90 sub new {
91 my $self = shift;
92 $self = fields::new($self) unless ref $self;
93 $self->{cat} = 'meow'; # scalar element
94 @$self{'dog','bird'} = ('bark','tweet'); # slice
95 return $self;
96 }
97
98 phash
99 This function only works in Perl 5.8.x and earlier. Pseudo-hashes
100 were removed from Perl as of 5.10. Consider using restricted
101 hashes or fields::new() instead (which itself uses restricted
102 hashes under 5.10+). See Hash::Util. Using fields::phash() under
103 5.10 or higher will cause an error.
104
105 fields::phash() can be used to create and initialize a plain
106 (unblessed) pseudo-hash. This function should always be used
107 instead of creating pseudo-hashes directly.
108
109 If the first argument is a reference to an array, the pseudo-hash
110 will be created with keys from that array. If a second argument is
111 supplied, it must also be a reference to an array whose elements
112 will be used as the values. If the second array contains less
113 elements than the first, the trailing elements of the pseudo-hash
114 will not be initialized. This makes it particularly useful for
115 creating a pseudo-hash from subroutine arguments:
116
117 sub dogtag {
118 my $tag = fields::phash([qw(name rank ser_num)], [@_]);
119 }
120
121 fields::phash() also accepts a list of key-value pairs that will be
122 used to construct the pseudo hash. Examples:
123
124 my $tag = fields::phash(name => "Joe",
125 rank => "captain",
126 ser_num => 42);
127
128 my $pseudohash = fields::phash(%args);
129
131 base, Hash::Util
132
133
134
135perl v5.34.1 2022-03-15 fields(3pm)