File Coverage

blib/lib/Object/Pad/SlotAttr/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 -- leonerd@leonerd.org.uk
5              
6             package Object::Pad::SlotAttr::Isa 0.02;
7              
8 3     3   144391 use v5.14;
  3         29  
9 3     3   12 use warnings;
  3         6  
  3         78  
10              
11 3     3   478 use Object::Pad 0.51;
  3         8062  
  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 slots
19              
20             =head1 SYNOPSIS
21              
22             use Object::Pad;
23             use Object::Pad::SlotAttr::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 slot attribute for L-based
41             classes, which declares that values assigned to the slot must conform to a
42             given object type.
43              
44             B The ability for L to take third-party slot 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 SLOT ATTRIBUTES
49              
50             =head2 :Isa
51              
52             has $slot :Isa(CLASSNAME) ...;
53              
54             Declares that any value assigned to the slot must be an object reference, and
55             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 slot value will not be modified.
58              
59             This type constraint is applied whenever the slot 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 slot variable by method
62             code within the class.
63              
64             =cut
65              
66             sub import
67             {
68 3     3   163 $^H{"Object::Pad::SlotAttr::Isa/Isa"}++;
69             }
70              
71             =head1 AUTHOR
72              
73             Paul Evans
74              
75             =cut
76              
77             0x55AA;