File Coverage

blib/lib/Object/Pad/FieldAttr/Isa.pm
Criterion Covered Total %
statement 9 10 90.0
branch n/a
condition n/a
subroutine 4 5 80.0
pod n/a
total 13 15 86.6


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-2023 -- leonerd@leonerd.org.uk
5              
6             package Object::Pad::FieldAttr::Isa 0.05;
7              
8 3     3   502332 use v5.14;
  3         28  
9 3     3   15 use warnings;
  3         6  
  3         90  
10              
11 3     3   675 use Object::Pad 0.802;
  3         11189  
  3         216  
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             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-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             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             {
68 2     2   192 $^H{"Object::Pad::FieldAttr::Isa/Isa"}++;
69             }
70              
71             sub unimport
72             {
73 0     0     delete $^H{"Object::Pad::FieldAttr::Isa/Isa"};
74             }
75              
76             =head1 AUTHOR
77              
78             Paul Evans
79              
80             =cut
81              
82             0x55AA;