File Coverage

blib/lib/Object/Pad/FieldAttr/Isa.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 13 100.0


line stmt bran cond sub pod time code
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-2022 -- leonerd@leonerd.org.uk
5              
6             package Object::Pad::FieldAttr::Isa 0.03;
7              
8 3     3   147585 use v5.14;
  3         29  
9 3     3   16 use warnings;
  3         3  
  3         76  
10              
11 3     3   526 use Object::Pad 0.51;
  3         8163  
  3         12  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - apply class type constraints to C fields
19              
20             =head1 SYNOPSIS
21              
22             use Object::Pad;
23             use Object::Pad::FieldAttr::Isa;
24              
25             class ListNode {
26             has $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-based
41             classes, which declares that values assigned to the field must conform to a
42             given object type.
43              
44             B The ability for L 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             has $field :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             {
68 3     3   174 $^H{"Object::Pad::FieldAttr::Isa/Isa"}++;
69             }
70              
71             =head1 AUTHOR
72              
73             Paul Evans
74              
75             =cut
76              
77             0x55AA;