1Class::MethodMaker::scaUlsaerr(3C)ontributed Perl DocumeCnltaastsi:o:nMethodMaker::scalar(3)
2
3
4
6 Class::Method::scalar - Create methods for handling a scalar value.
7
9 package MyClass;
10 use Class::MethodMaker
11 [ scalar => [qw/ a -static s /]];
12
13 sub new {
14 my $class = shift;
15 bless {}, $class;
16 }
17
18 package main;
19
20 my $m = MyClass->new;
21 my $a, $x;
22
23 $a = $m->a; # *undef*
24 $x = $m->a_isset; # false
25 $a = $m->a(1); # 1
26 $m->a(3);
27 $x = $m->a_isset; # true
28 $a = $m->a; # 3
29 $a = $m->a(5); # 5;
30 $m->a_reset;
31 $x = $m->a_isset; # false
32
34 Creates methods to handle array values in an object. For a component
35 named "x", by default creates methods "x", "x_reset", "x_isset",
36 "x_clear".
37
38 Methods available are:
39
40 "*"
41
42 $m->a(3);
43 $a = $m->a; # 3
44 $a = $m->a(5); # 5;
45
46 Created by default. If an argument is provided, the component is set
47 to that value. The method returns the value of the component (after
48 assignment to a provided value, if appropriate).
49
50 *_reset
51
52 $m->a_reset;
53
54 Created by default. Resets the component back to its default.
55 Normally, this means that *_isset will return false, and "*" will
56 return undef. If "-default" is in effect, then the component will be
57 set to the default value, and *_isset will return true. If
58 "-default_ctor" is in effect, then the default subr will be invoked,
59 and its return value used to set the value of the component, and
60 *_isset will return true.
61
62 Advanced Note: actually, defaults are assigned as needed: typically,
63 the next time a the value of a component is read.
64
65 *_isset
66
67 print $m->a_isset ? "true" : "false";
68
69 Created by default. Whether the component is currently set. This is
70 different from being defined; initially, the component is not set (and
71 if read, will return undef); it can be set to undef (which is a set
72 value, which also returns undef). Having been set, the only way to
73 unset the component is with <*_reset>.
74
75 If a default value is in effect, then <*_isset> will always return
76 true.
77
78 *_clear
79
80 $m->a(5);
81 $a = $m->a; # 5
82 $x = $m->a_isset; # true
83 $m->a_clear;
84 $a = $m->a; # *undef*
85 $x = $m->a_isset; # true
86
87 Created by default. A shorthand for setting to undef. Note that the
88 component will be set to undef, not reset, so *_isset will return true.
89
90 *_get
91
92 package MyClass;
93 use Class::MethodMaker
94 [ scalar => [{'*_get' => '*_get'}, 'a'],
95 new => new, ];
96
97 package main;
98 my $m = MyClass->new;
99 $m->a(3);
100 $a = $m->a_get; # 3
101 $a = $m->a_get(5); # 3; ignores argument
102 $a = $m->a_get(5); # 3; unchanged by previous call
103
104 Created on request. Retrieves the value of the component without
105 setting (ignores any arguments passed).
106
107 *_set
108
109 package MyClass;
110 use Class::MethodMaker
111 [ scalar => [{'*_set' => '*_set'}, 'a'],
112 new => new, ];
113
114 package main;
115 my $m = MyClass->new;
116 $m->a(3);
117 $a = $m->a_set; # *undef*
118 $a = $m->a_set(5); # *undef*; value is set but not returned
119 $a = $m->a; # 5
120
121 Created on request. Sets the component to the first argument (or undef
122 if no argument provided). Returns no value.
123
124
125
126perl v5.34.0 2022-01-21 Class::MethodMaker::scalar(3)