Debian Codegraph @

README

raw
1 NAME
2
3 Object::Pad::ClassAttr::Struct - declare an Object::Pad class to be
4 struct-like
5
6 SYNOPSIS
7
8 use Object::Pad;
9 use Object::Pad::ClassAttr::Struct;
10
11 class Colour :Struct {
12 # These get :param :mutator automatically
13 field $red = 0;
14 field $green = 0;
15 field $blue = 0;
16
17 # Additional methods are still permitted
18 method lightness {
19 return ($red + $green + $blue) / 3;
20 }
21 }
22
23 my $cyan = Colour->new( green => 1, blue => 1 );
24
25 # A positional constructor is created automatically
26 my $white = Colour->new_values(1, 1, 1);
27
28 DESCRIPTION
29
30 This module provides a third-party class attribute for
31 Object::Pad-based classes, which applies some attributes automatically
32 to every field added to the class, as a convenient shortcut for making
33 structure-like classes.
34
35 CLASS ATTRIBUTES
36
37 :Struct
38
39 class Name :Struct ... { ... }
40
41 Automatically applies the :param and :mutator attributes to every field
42 defined on the class, meaning the constructor will accept parameters
43 for each field to initialise the value, and each field will have an
44 lvalue mutator method.
45
46 In addition, the class itself gains the :strict(params) attribute,
47 meaning the constructor will check parameter names and throw an
48 exception for unrecognised names.
49
50 Since version 0.04 a positional constructor class method called
51 new_values is also provided into the class, which takes a value for
52 every field positionally, in declared order.
53
54 $obj = ClassName->new_values($v1, $v2, $v3, ...);
55
56 This positional constructor must receive as many positional arguments
57 as there are fields in total in the class; even the optional ones. All
58 arguments are required here.
59
60 Since version 0.05 the following options are permitted inside the
61 attribute value parentheses:
62
63 :Struct(readonly)
64
65 Instances of this class do not permit fields to be modified after
66 construction. The accessor is created using the :reader field attribute
67 rather than :mutator.
68
69 AUTHOR
70
71 Paul Evans <[email protected]>
72