1fields(3pm)            Perl Programmers Reference Guide            fields(3pm)
2
3
4

NAME

6       fields - compile-time class fields
7

SYNOPSIS

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 an error
28           $var->{zap} = 42;
29
30           # subclassing
31           {
32               package Bar;
33               use base 'Foo';
34               use fields qw(baz _Bar_private);        # not shared with Foo
35               sub new {
36                   my $class = shift;
37                   my $self = fields::new($class);
38                   $self->SUPER::new();                # init base fields
39                   $self->{baz} = 10;                  # init own fields
40                   $self->{_Bar_private} = "this is Bar's secret";
41                   return $self;
42               }
43           }
44

DESCRIPTION

46       The "fields" pragma enables compile-time verified class fields.
47
48       NOTE: The current implementation keeps the declared fields in the
49       %FIELDS hash of the calling package, but this may change in future ver‐
50       sions.  Do not update the %FIELDS hash directly, because it must be
51       created at compile-time for it to be fully useful, as is done by this
52       pragma.
53
54       Only valid for perl before 5.9.0:
55
56       If a typed lexical variable holding a reference is used to access a
57       hash element and a package with the same name as the type has declared
58       class fields using this pragma, then the operation is turned into an
59       array access at compile time.
60
61       The related "base" pragma will combine fields from base classes and any
62       fields declared using the "fields" pragma.  This enables field inheri‐
63       tance to work properly.
64
65       Field names that start with an underscore character are made private to
66       the class and are not visible to subclasses.  Inherited fields can be
67       overridden but will generate a warning if used together with the "-w"
68       switch.
69
70       Only valid for perls before 5.9.0:
71
72       The effect of all this is that you can have objects with named fields
73       which are as compact and as fast arrays to access. This only works as
74       long as the objects are accessed through properly typed variables. If
75       the objects are not typed, access is only checked at run time.
76
77       The following functions are supported:
78
79       new  perl before 5.9.0:  fields::new() creates and blesses a pseudo-
80           hash comprised of the fields declared using the "fields" pragma
81           into the specified class.
82
83            perl 5.9.0 and higher:  fields::new() creates and blesses a
84           restricted-hash comprised of the fields declared using the "fields"
85           pragma into the specified class.
86
87           This function is usable with or without pseudo-hashes.  It is the
88           recommended way to construct a fields-based object.
89
90           This makes it possible to write a constructor like this:
91
92               package Critter::Sounds;
93               use fields qw(cat dog bird);
94
95               sub new {
96                   my $self = shift;
97                   $self = fields::new($self) unless ref $self;
98                   $self->{cat} = 'meow';                          # scalar element
99                   @$self{'dog','bird'} = ('bark','tweet');        # slice
100                   return $self;
101               }
102
103       phash
104            before perl 5.9.0:
105
106           fields::phash() can be used to create and initialize a plain
107           (unblessed) pseudo-hash.  This function should always be used
108           instead of creating pseudo-hashes directly.
109
110           If the first argument is a reference to an array, the pseudo-hash
111           will be created with keys from that array.  If a second argument is
112           supplied, it must also be a reference to an array whose elements
113           will be used as the values.  If the second array contains less ele‐
114           ments than the first, the trailing elements of the pseudo-hash will
115           not be initialized.  This makes it particularly useful for creating
116           a pseudo-hash from subroutine arguments:
117
118               sub dogtag {
119                  my $tag = fields::phash([qw(name rank ser_num)], [@_]);
120               }
121
122           fields::phash() also accepts a list of key-value pairs that will be
123           used to construct the pseudo hash.  Examples:
124
125               my $tag = fields::phash(name => "Joe",
126                                       rank => "captain",
127                                       ser_num => 42);
128
129               my $pseudohash = fields::phash(%args);
130
131            perl 5.9.0 and higher:
132
133           Pseudo-hashes have been removed from Perl as of 5.10.  Consider
134           using restricted hashes or fields::new() instead.  Using
135           fields::phash() will cause an error.
136

SEE ALSO

138       base
139
140
141
142perl v5.8.8                       2001-09-21                       fields(3pm)
Impressum