blib/lib/Object/Pad/MOP/Class.pm

filter: Statementstmt 78.8% 41 / 52 Branchbran 75.0% 6 / 8 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 75.0% 12 / 16 Podpod 90.9% 10 / 11 totaltotal 74.7% 71 / 95 CC 7 SCAR 8.3
CC
7
Cov
75%
try_for_class
5.1
for_class
2.0
import_into
2.0
CRAP 2.3
« blib/lib/Object/Pad.pm ↑ Summary blib/lib/Object/Pad/MetaFunctions.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2020-2023 -- leonerd@leonerd.org.uk
5  
6 package Object::Pad::MOP::Class 0.825;
7  
8 58 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 58 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # This is an XS-implemented object type provided by Object::Pad itself
13 58 require Object::Pad;
14  
15 =head1 NAME
16  
17 C<Object::Pad::MOP::Class> - meta-object representation of a C<Object::Pad> class
18  
19 =head1 DESCRIPTION
20  
21 =for highlighter language=perl
22  
23 Instances of this class represent a class or role implemented by
24 L<Object::Pad>. Accessors provide information about the class or role, and
25 methods that can alter the class, typically by adding new elements to it,
26 allow a program to extend existing classes.
27  
28 Where possible, this API is designed to be compatible with L<MOP::Class>.
29  
30 This API should be considered B<experimental>, and will emit warnings to that
31 effect. They can be silenced with
32  
33 use Object::Pad qw( :experimental(mop) );
34  
35 or
36  
37 use Object::Pad::MOP::Class qw( :experimental(mop) );
38  
39 =cut
40  
41 sub import
42 1 ▶{
Subroutine import: called CC=1 SCAR=0.0
43 1 my $class = shift;
44 1 my $caller = caller;
45  
46 1 $class->import_into( $caller, @_ );
47 }
48  
49 sub import_into
50 1 ▶{
Subroutine import_into: called CC=2 SCAR=7.0
Pod undocumented
51 1 my $class = shift;
52 1 my $caller = shift;
53  
54 1 Object::Pad->_import_experimental( \@_, qw( mop ) );
55  
56 1 ▶ croak "Unrecognised import symbols @_" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
57 }
58  
59 =head1 CONSTRUCTOR
60  
61 =head2 for_class
62  
63 $metaclass = Object::Pad::MOP::Class->for_class( $class );
64  
65 I<Since version 0.38.>
66  
67 Returns the metaclass instance associated with the given class name. Throws an
68 exception if the requested class is not using C<Object::Pad>.
69  
70 =head2 try_for_class
71  
72 $metaclass = Object::Pad::MOP::Class->try_for_class( $class );
73  
74 I<Since version 0.808.>
75  
76 If the given class name is built using C<Object::Pad> then returns the
77 metaclass instance for it. If not, returns C<undef>.
78  
79 =cut
80  
81 sub try_for_class
82 27 ▶{
Subroutine try_for_class: called CC=5 SCAR=16.3
Pod documented
83 27 shift;
84 27 my ( $targetclass ) = @_;
85  
86 27 my $level = 0;
87 27 ▶ $level++ while (caller $level)[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
(caller $level)[0] eq __PACKAGE__ and do { ++$level } 27 27 54
88  
89 27 my $callerhints = (caller $level)[10];
90 27 ▶ if( !$callerhints or !$callerhints->{"Object::Pad/experimental(mop)"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callerhints and $callerhints->{"Object::Pad/experimental(mop)"}) 0 27 27
Condition33%1/3
$callerhints and $callerhints->{"Object::Pad/experimental(mop)"}
!ll&&!rl&&r
0027
MC/DC0%0/2
$callerhints and $callerhints->{"Object::Pad/experimental(mop)"}
$callerhints $callerhints->{"Object::Pad/experimental(mop)"}
Truth table33%1/3
A and B
A= $callerhintsB= $callerhints->{"Object::Pad/experimental(mop)"}
ABresult
0X0
100
111
91 0 warnings::warnif experimental =>
92 "Object::Pad::MOP is experimental and may be changed or removed without notice";
93 }
94  
95 27 my $code = do {
96 27 my $fqname = "${targetclass}::META";
97 58 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 27 ▶ defined &$fqname or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined &$fqname or return undef 25 2 27
99 25 \&{"${targetclass}::META"};
100 };
101  
102 25 return $code->( $targetclass );
103 }
104  
105 sub for_class
106 25 ▶{
Subroutine for_class: called CC=2 SCAR=7.1
Pod documented
107 25 my $self = shift;
108 25 my ( $targetclass ) = @_;
109  
110 25 ▶ return $self->try_for_class( $targetclass ) //
Condition50%1/2
$self->try_for_class($targetclass) // croak("Cannot obtain Object::Pad::MOP::Class for '${targetclass}' as it does not appear to be based on Object::Pad")
l!l
250
MC/DC0%0/1
$self->try_for_class($targetclass) // croak("Cannot obtain Object::Pad::MOP::Class for '${targetclass}' as it does not appear to be based on Object::Pad")
$self->try_for_class($targetclass)
Truth table50%1/2
A
A= $self->try_for_class($targetclass)
Aresult
11
00
111 croak "Cannot obtain Object::Pad::MOP::Class for '$targetclass' as it does not appear to be based on Object::Pad";
112 }
113  
114 =head2 for_caller
115  
116 $metaclass = Object::Pad::MOP::Class->for_caller;
117  
118 I<Since version 0.38.>
119  
120 A convenient shortcut for obtaining the metaclass instance of the calling
121 package scope. Often handy during C<BEGIN> blocks of the class itself to
122 perform adjustments or additions.
123  
124 class Some::Class::Here 1.234 {
125 BEGIN {
126 my $meta = Object::Pad::MOP::Class->for_caller;
127 ...
128 }
129 }
130  
131 =cut
132  
133 sub for_caller
134 2 ▶{
Subroutine for_caller: called CC=1 SCAR=0.0
Pod documented
135 2 return shift->for_class( caller );
136 }
137  
138 =head2 create_class
139  
140 my $metaclass = Object::Pad::MOP::Class->create_class( $name, %args );
141  
142 I<Since version 0.61.>
143  
144 Creates a new class of the given name and yields the metaclass for it.
145  
146 Takes the following additional named arguments:
147  
148 =over 4
149  
150 =item extends => STRING
151  
152 =item isa => STRING
153  
154 An optional name of a superclass that this class will extend. These options
155 are synonyms; new code should use C<isa>, as C<extends> will eventually be
156 removed.
157  
158 =item abstract => BOOL
159  
160 Optionally; if given a true value the newly-created class will be declared as
161 abstract, as if the L<C<:abstract>|Object::Pad/:abstract> attribute had been
162 applied.
163  
164 =back
165  
166 Once created, this metaclass must be sealed using the L</seal> method before
167 it can be used to actually construct object instances.
168  
169 =head2 create_role
170  
171 my $metaclass = Object::Pad::MOP::Class->create_role( $name, %args );
172  
173 I<Since version 0.61.>
174  
175 As L</create_class> but creates a role instead of a class.
176  
177 =cut
178  
179 2 ▶sub create_class { shift->_create_class( shift, @_ ); }
Subroutine create_class: called CC=1 SCAR=0.0
Pod documented
180 0 ▶sub create_role { shift->_create_role ( shift, @_ ); }
Subroutine create_role: not called CC=1 SCAR=6.9
Pod documented
181  
182 =head2 begin_class
183  
184 BEGIN {
185 my $metaclass = Object::Pad::MOP::Class->begin_class( $name, %args );
186 ...
187 }
188  
189 I<Since version 0.46.>
190  
191 A variant of L</create_class> which sets the newly-created class as the
192 current complication scope of the surrounding code, allowing it to accept
193 C<Object::Pad> syntax forms such as C<has> and C<method>.
194  
195 This must be done during C<BEGIN> time because of this compiletime effect.
196 It additionally creates a deferred code block at C<UNITCHECK> time of its
197 surrounding scope, which is used to finalise the constructed class. In this
198 case you do not need to remember to call L</seal> on it; this happens
199 automatically.
200  
201 =head2 begin_role
202  
203 I<Since version 0.46.>
204  
205 As L</begin_class> but creates a role instead of a class.
206  
207 =cut
208  
209 3 ▶sub begin_class { shift->_create_class( shift, _set_compclassmeta => 1, @_ ); }
Subroutine begin_class: called CC=1 SCAR=0.0
Pod documented
210 1 ▶sub begin_role { shift->_create_role ( shift, _set_compclassmeta => 1, @_ ); }
Subroutine begin_role: called CC=1 SCAR=0.0
Pod documented
211  
212 =head1 METHODS
213  
214 =head2 is_class
215  
216 =head2 is_role
217  
218 $bool = $metaclass->is_class;
219 $bool = $metaclass->is_role;
220  
221 Exactly one of these methods will return true, depending on whether this
222 metaclass instance represents a true C<class>, or a C<role>.
223  
224 =head2 is_abstract
225  
226 $bool = $metaclass->is_abstract;
227  
228 True on a C<role>, or a C<class> that was declared with the C<:abstract>
229 attribute.
230  
231 =head2 name
232  
233 $name = $metaclass->name;
234  
235 Returns the name of the class, as a plain string.
236  
237 =head2 superclasses
238  
239 @classes = $metaclass->superclasses;
240  
241 Returns a list of superclasses, as L<Object::Pad::MOP::Class> instances.
242  
243 Because C<Object::Pad> does not support multiple superclasses, this list will
244 contain at most one item.
245  
246 =head2 direct_roles
247  
248 @roles = $metaclass->direct_roles;
249  
250 Returns a list of the roles introduced by this class (i.e. added by `does`
251 declarations but not inherited from the superclass), as
252 L<Object::Pad::MOP::Class> instances.
253  
254 This method is also aliased as C<roles>.
255  
256 =head2 all_roles
257  
258 @roles = $metaclass->all_roles;
259  
260 I<Since version 0.56.>
261  
262 Returns a list of all the roles implemented by this class (i.e. including
263 those inherited from the superclass), as L<Object::Pad::MOP::Class> instances.
264  
265 =head2 add_role
266  
267 $metaclass->add_role( $rolename );
268 $metaclass->add_role( $rolemeta );
269  
270 I<Since version 0.56.>
271  
272 Adds a new role to the list of those implemented by the class.
273  
274 The new role can be specified either as a plain string giving its name, or as
275 an C<Object::Pad::MOP::Class> meta instance directly.
276  
277 Before version 0.56 this was called C<compose_role>.
278  
279 =head2 add_BUILD
280  
281 $metaclass->add_BUILD( $code );
282  
283 Adds a new C<BUILD> block to the class, as a CODE reference.
284  
285 =head2 add_method
286  
287 $metamethod = $metaclass->add_method( $name, %args, $code );
288  
289 Adds a new named method to the class under the given name, as CODE reference.
290  
291 Returns an instance of L<Object::Pad::MOP::Method> to represent it.
292  
293 Recognises the following additional named arguments:
294  
295 =over 4
296  
297 =item common => BOOL
298  
299 I<Since version 0.62.>
300  
301 If true, the method is a class-common method.
302  
303 =back
304  
305 =head2 get_direct_method
306  
307 $metamethod = $metaclass->get_direct_method( $name );
308  
309 Returns an instance of L<Object::Pad::MOP::Method> to represent the method of
310 the given name, if one exists. If not an exception is thrown.
311  
312 This can only see directly-applied methods; that is, methods created by the
313 C<method> keyword on the class itself, or added via L</add_method>. This will
314 not see other names in the package stash, even if they contain a C<CODE> slot,
315 nor will it see methods inherited from a superclass.
316  
317 This is also aliased as C<get_own_method> for compatibility with the
318 L<MOP::Class> interface.
319  
320 =head2 get_method
321  
322 $metamethod = $metaclass->get_method( $name );
323  
324 I<Since version 0.57.>
325  
326 Returns an instance of L<Object::Pad::MOP::Method> to represent the method of
327 the given name, if one exists. If not an exception is thrown.
328  
329 This will additionally search superclasses, and may return a method belonging
330 to a parent class.
331  
332 =head2 direct_methods
333  
334 @metamethods = $metaclass->direct_methods;
335  
336 I<Since version 0.57.>
337  
338 Returns a list of L<Object::Pad::MOP::Method> instances to represent all the
339 direct methods of the class. This list may be empty.
340  
341 =head2 all_methods
342  
343 @metamethods = $metaclass->all_methods;
344  
345 I<Since version 0.57.>
346  
347 Returns a list of L<Object::Pad::MOP::Method> instances to represent all the
348 methods of the class, including those inherited from superclasses. This list
349 may be empty.
350  
351 =head2 add_field
352  
353 $metafield = $metaclass->add_field( $name, %args );
354  
355 I<since version 0.60.>
356  
357 Adds a new field to the class, using the given name (which must begin with the
358 sigil character C<$>, C<@> or C<%>).
359  
360 Recognises the following additional named arguments:
361  
362 =over 4
363  
364 =item default => SCALAR
365  
366 I<Since version 0.43.>
367  
368 Provides a default value for the field; similar to using the syntax
369  
370 has $field = SCALAR;
371  
372 This value may be C<undef>, to set the value as being optional if it
373 additionally has a parameter name.
374  
375 =item param => STRING
376  
377 I<Since version 0.43.>
378  
379 Provides a parameter name for the field; similar to setting it using the
380 C<:param> attribute. This parameter will be required unless a default value is
381 set (such value may still be C<undef>).
382  
383 =item reader => STRING
384  
385 =item writer => STRING
386  
387 =item mutator => STRING
388  
389 I<Since version 0.46.>
390  
391 =item accessor => STRING
392  
393 I<Since version 0.56.>
394  
395 Provides method names for generated reader, writer, lvalue-mutator or
396 reader+writer accessor methods, similar to setting them via the C<:reader>,
397 C<:writer>, C<:mutator> or C<:accessor> attributes.
398  
399 =item weak => BOOL
400  
401 I<Since version 0.46.>
402  
403 If true, reference values assigned into the field by the constructor or
404 accessor methods will be weakened, similar to setting the C<:weak> attribute.
405  
406 =item attributes => ARRAY
407  
408 I<Since version 0.811.>
409  
410 Provides additional attributes to apply to the field, as if declared by
411 attribute syntax. This is largely useful for applying third-party field
412 attributes.
413  
414 The referenced array should contain an even-sized list of pairs. The first of
415 each pair will be the name of an attribute, and the second will be a value to
416 pass (or C<undef> if not applicable). Note that if the third-party attribute
417 provides separate parse and apply phases in its hook functions, the parse part
418 will I<not> be invoked by this parameter. Whatever value is passed must be
419 something accepted by the apply phase alone.
420  
421 =back
422  
423 Returns an instance of L<Object::Pad::MOP::Field> to represent it.
424  
425 =head2 add_slot
426  
427 $metafield = $metaclass->add_slot( $name, %args );
428  
429 I<Now deprecated.>
430  
431 Back-compatibility alias for C<add_field>.
432  
433 =cut
434  
435 sub add_slot
436 0 ▶{
Subroutine add_slot: not called CC=1 SCAR=6.9
Pod documented
437 0 my $self = shift;
438 0 carp "->add_slot is now deprecated; use ->add_field instead";
439 0 return $self->add_field( @_ );
440 }
441  
442 =head2 get_field
443  
444 $metafield = $metaclass->get_field( $name );
445  
446 I<Since version 0.60.>
447  
448 Returns an instance of L<Object::Pad::MOP::Field> to represent the field of
449 the given name, if one exists. If not an exception is thrown.
450  
451 =head2 get_slot
452  
453 $metafield = $metaclass->get_slot( $name );
454  
455 I<Now deprecated.>
456  
457 Back-compatibility alias for C<get_field>.
458  
459 =cut
460  
461 sub get_slot
462 0 ▶{
Subroutine get_slot: not called CC=1 SCAR=6.9
Pod documented
463 0 my $self = shift;
464 0 carp "->get_slot is now deprecated; use ->get_field instead";
465 0 return $self->get_field( @_ );
466 }
467  
468 =head2 fields
469  
470 @metafields = $metaclass->fields;
471  
472 I<Since version 0.60.>
473  
474 Returns a list of L<Object::Pad::MOP::Field> instances to represent all the
475 fields of the class. This list may be empty.
476  
477 =head2 slots
478  
479 @metafields = $metaclass->slots;
480  
481 I<Since version 0.42; now deprecated.>
482  
483 Back-compatibility alias for C<fields>.
484  
485 =cut
486  
487 sub slots
488 0 ▶{
Subroutine slots: not called CC=1 SCAR=6.9
Pod documented
489 0 my $self = shift;
490 0 carp "->slots is now deprecated; use ->fields instead";
491 0 return $self->fields;
492 }
493  
494 58 *roles = \&direct_roles;
495  
496 58 *get_own_method = \&get_direct_method;
497  
498 =head2 add_required_method
499  
500 $metaclass->add_required_method( $name );
501  
502 I<Since version 0.61.>
503  
504 Adds a new required method to the role, whose name is given as a plain string.
505  
506 Currently returns nothing. This should be considered temporary, as eventually
507 a metatype for required methods will be added, at which point this method can
508 return instances of it. It may also take additional parameters to define the
509 required method with. Currently extra parameters are not permitted.
510  
511 =head2 required_method_names
512  
513 @names = $metaclass->required_method_names;
514  
515 I<Since version 0.61.>
516  
517 Returns a list names of required methods for the role, as plain strings.
518  
519 This should be considered a temporary method. Currently there is no metatype
520 for required methods, so they are represented as plain strings. Eventually a
521 type may be defined and a C<required_methods> method will be added.
522  
523 =cut
524  
525 =head2 seal
526  
527 $metaclass->seal;
528  
529 I<Since version 0.61.>
530  
531 If the metaclass was created by L</create_class> or L</create_role>, this
532 method must be called once everything has been added into it, as the class
533 will not yet be ready to construct actual object instances before this is
534 done.
535  
536 =head1 AUTHOR
537  
538 Paul Evans <leonerd@leonerd.org.uk>
539  
540 =cut
541  
542 58 0x55AA;
« blib/lib/Object/Pad.pm ↑ Summary blib/lib/Object/Pad/MetaFunctions.pm »