blib/lib/Object/Pad/FieldAttr/Isa.pm

filter: Statementstmt 92.3% 12 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 1.8
CC
1
Cov
92%
unimport
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary lib/Object/Pad/FieldAttr/Isa.xs »
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, 2021-2023 -- leonerd@leonerd.org.uk
5  
6 package Object::Pad::FieldAttr::Isa 0.05;
7  
8 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Object::Pad 0.802;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 require XSLoader;
14 3 XSLoader::load( __PACKAGE__, our $VERSION );
15  
16 =head1 NAME
17  
18 C<Object::Pad::FieldAttr::Isa> - apply class type constraints to C<Object::Pad> fields
19  
20 =head1 SYNOPSIS
21  
22 use Object::Pad;
23 use Object::Pad::FieldAttr::Isa;
24  
25 class ListNode {
26 field $next :param :reader :writer :Isa(ListNode) = undef;
27 }
28  
29 my $first = ListNode->new();
30 my $second = ListNode->new(next => $first);
31  
32 # This will fail
33 my $third = ListNode->new(next => "something else");
34  
35 # This will fail
36 $second->set_next("another thing");
37  
38 =head1 DESCRIPTION
39  
40 This module provides a third-party field attribute for L<Object::Pad>-based
41 classes, which declares that values assigned to the field must conform to a
42 given object type.
43  
44 B<WARNING> The ability for L<Object::Pad> to take third-party field attributes
45 is still new and highly experimental, and subject to much API change in
46 future. As a result, this module should be considered equally experimental.
47  
48 =head1 FIELD ATTRIBUTES
49  
50 =head2 :Isa
51  
52 field $name :Isa(CLASSNAME) ...;
53  
54 Declares that any value assigned to the field must be an object reference,
55 and must be derived from the named class. Attempts to assign a non-conforming
56 value, such as a non-reference, or reference to a class not derived from that
57 named, will throw an exception, and the field value will not be modified.
58  
59 This type constraint is applied whenever the field itself is assigned to,
60 whether that is from C<:param> initialisation, invoking a C<:writer> or
61 C<:mutator> accessor, or direct assignment into the field variable by method
62 code within the class.
63  
64 =cut
65  
66 sub import
67 2 ▶{
Subroutine import: called CC=1 SCAR=0.0
68 2 $^H{"Object::Pad::FieldAttr::Isa/Isa"}++;
69 }
70  
71 sub unimport
72 0 ▶{
Subroutine unimport: not called CC=1 SCAR=6.9
73 0 delete $^H{"Object::Pad::FieldAttr::Isa/Isa"};
74 }
75  
76 =head1 AUTHOR
77  
78 Paul Evans <leonerd@leonerd.org.uk>
79  
80 =cut
81  
82 3 0x55AA;
↑ Summary lib/Object/Pad/FieldAttr/Isa.xs »