1Class::Field(3) User Contributed Perl Documentation Class::Field(3)
2
3
4
6 Class::Field - Class Field Accessor Generator
7
9 This document describes Class::Field version 0.24.
10
12 package Thing;
13 use Class::Field qw'field const';
14
15 field 'this';
16 field 'list' => [];
17 field 'map' => {};
18 field 'that', -init => '$self->setup_that';
19 field 'circular_ref' => -weaken;
20 const 'answer' => 42;
21
23 Class::Field exports two subroutines, "field" and "const". These
24 functions are used to declare fields and constants in your class.
25
26 Class::Field generates custom code for each accessor that is optimized
27 for speed.
28
30 "field"
31 Defines accessor methods for a field of your class:
32
33 package Example;
34 use base 'Parent';
35 use Class::Field qw'field const';
36
37 field 'foo';
38 field bar => [];
39
40 sub lalala {
41 my $self = shift;
42 $self->foo(42);
43 push @{$self->{bar}}, $self->foo;
44 }
45
46 The first parameter passed to "field" is the name of the attribute
47 being defined. Accessors can be given an optional default value.
48 This value will be returned if no value for the field has been set
49 in the object.
50
51 "const"
52 const bar => 42;
53
54 The "const" function is similar to <field> except that it is
55 immutable. It also does not store data in the object. You probably
56 always want to give a "const" a default value, otherwise the
57 generated method will be somewhat useless.
58
60 This code was taken directly out the Spiffy module for those people who
61 just want this functionality without using the rest of Spiffy.
62
64 Ingy döt Net <ingy@cpan.org>
65
67 Copyright 2006-2019. Ingy döt Net.
68
69 This program is free software; you can redistribute it and/or modify it
70 under the same terms as Perl itself.
71
72 See <http://www.perl.com/perl/misc/Artistic.html>
73
74
75
76perl v5.34.0 2021-07-22 Class::Field(3)