1MakeMethods::Template::UUsneirveCrosnatlr(i3b)uted PerlMDaokceuMmeetnhtoadtsi:o:nTemplate::Universal(3)
2
3
4
6 Class::MakeMethods::Template::Universal - Meta-methods for any type of
7 object
8
10 package MyObject;
11 use Class::MakeMethods::Template::Universal (
12 'no_op' => [ 'twiddle' ],
13 'croak' => [ 'fail', { croak_msg => 'Curses!' } ]
14 );
15
16 package main;
17
18 MyObject->twiddle; # Does nothing
19 if ( $foiled ) { MyObject->fail() } # Dies with croak_msg
20
23 The following meta-methods and behaviors are applicable across multiple
24 types of classes and objects.
25
26 Universal:generic
27 This is not a directly-invokable method type, but instead provides code
28 expressions for use in other method-generators.
29
30 You can use any of these features in your meta-method interfaces
31 without explicitly importing them.
32
33 Modifiers
34
35 • --private
36
37 Causes the method to croak if it is called from outside of the
38 package which originally declared it.
39
40 Note that this protection can currently be circumvented if your
41 class provides the method_init behavior, or another subroutine that
42 calls methods by name.
43
44 • --protected
45
46 Causes the method to croak if it is called from a package other
47 than the declaring package and its inheritors.
48
49 Note that this protection can currently be circumvented if your
50 class provides the method_init behavior, or another subroutine that
51 calls methods by name.
52
53 • --public
54
55 Cancels any previous -private or -protected declaration.
56
57 • --self_closure
58
59 Causes the method to return a function reference which is bound to
60 the arguments provided when it is first called.
61
62 For examples of usage, see the test scripts in t/*closure.t.
63
64 • --lvalue
65
66 Adds the ":lvalue" attribute to the subroutine declaration.
67
68 For examples of usage, see the test scripts in t/*lvalue.t.
69
70 • --warn_calls
71
72 For diagnostic purposes, call warn with the object reference,
73 method name, and arguments before executing the body of the method.
74
75 Behaviors
76
77 • attributes
78
79 Runtime access to method parameters.
80
81 • no_op -- See below.
82
83 • croak -- See below.
84
85 • method_init -- See below.
86
87 no_op
88 For each meta-method, creates a method with an empty body.
89
90 use Class::MakeMethods::Template::Universal (
91 'no_op' => [ 'foo bar baz' ],
92 );
93
94 You might want to create and use such methods to provide hooks for
95 subclass activity.
96
97 No interfaces or parameters supported.
98
99 croak
100 For each meta-method, creates a method which will croak if called.
101
102 use Class::MakeMethods::Template::Universal (
103 'croak' => [ 'foo bar baz' ],
104 );
105
106 This is intended to support the use of abstract methods, that must be
107 overidden in a useful subclass.
108
109 If each subclass is expected to provide an implementation of a given
110 method, using this abstract method will replace the generic error
111 message below with the clearer, more explicit error message that
112 follows it:
113
114 Can't locate object method "foo" via package "My::Subclass"
115 The "foo" method is abstract and can not be called on My::Subclass
116
117 However, note that the existence of this method will be detected by
118 UNIVERSAL::can(), so it is not suitable for use in optional interfaces,
119 for which you may wish to be able to detect whether the method is
120 supported or not.
121
122 The -unsupported and -prohibited interfaces provide alternate error
123 messages, or a custom error message can be provided using the
124 'croak_msg' parameter.
125
126 method_init
127 Creates a method that accepts a hash of key-value pairs, or a reference
128 to hash of such pairs. For each pair, the key is interpreted as the
129 name of a method to call, and the value is the argument to be passed to
130 that method.
131
132 Sample declaration and usage:
133
134 package MyObject;
135 use Class::MakeMethods::Template::Universal (
136 method_init => 'init',
137 );
138 ...
139
140 my $object = MyObject->new()
141 $object->init( foo => 'Foozle', bar => 'Barbados' );
142
143 # Equivalent to:
144 $object->foo('Foozle');
145 $object->bar('Barbados');
146
147 You might want to create and use such methods to allow easy
148 initialization of multiple object or class parameters in a single call.
149
150 Note: including methods of this type will circumvent the protection of
151 "private" and "protected" methods, because it an outside caller can
152 cause an object to call specific methods on itself, bypassing the
153 privacy protection.
154
155 forward_methods
156 Creates a method which delegates to an object provided by another
157 method.
158
159 Example:
160
161 use Class::MakeMethods::Template::Universal
162 forward_methods => [
163 --target=> 'whistle', w,
164 [ 'x', 'y' ], { target=> 'xylophone' },
165 { name=>'z', target=>'zither', target_args=>[123], method_name=>do_zed },
166 ];
167
168 Example: The above defines that method "w" will be handled by the
169 calling "w" on the object returned by "whistle", whilst methods "x" and
170 "y" will be handled by "xylophone", and method "z" will be handled by
171 calling "do_zed" on the object returned by calling "zither(123)".
172
173 Interfaces:
174
175 forward (default)
176 Calls the method on the target object. If the target object is
177 missing, croaks at runtime with a message saying "Can't forward bar
178 because bar is empty."
179
180 delegate
181 Calls the method on the target object, if present. If the target
182 object is missing, returns nothing.
183
184 Parameters: The following additional parameters are supported:
185
186 target
187 Required. The name of the method that will provide the object that
188 will handle the operation.
189
190 target_args
191 Optional ref to an array of arguments to be passed to the target
192 method.
193
194 method_name
195 The name of the method to call on the handling object. Defaults to
196 the name of the meta-method being created.
197
199 See Class::MakeMethods for general information about this distribution.
200
201 See Class::MakeMethods::Template for information about this family of
202 subclasses.
203
204
205
206perl v5.34.0 2022-01-21MakeMethods::Template::Universal(3)