File Coverage

blib/lib/Object/Pad/ClassAttr/Struct.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::ClassAttr::Struct 0.03;
7              
8 2     2   65850 use v5.14;
  2         13  
9 2     2   10 use warnings;
  2         3  
  2         50  
10              
11 2     2   518 use Object::Pad 0.56;
  2         8782  
  2         8  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - declare an C class to be struct-like
19              
20             =head1 SYNOPSIS
21              
22             use Object::Pad;
23             use Object::Pad::ClassAttr::Struct;
24              
25             class Colour :Struct {
26             # These get :param :mutator automatically
27             has $red = 0;
28             has $green = 0;
29             has $blue = 0;
30              
31             # Additional methods are still permitted
32             method lightness {
33             return ($red + $green + $blue) / 3;
34             }
35             }
36              
37             my $cyan = Colour->new( green => 1, blue => 1 );
38              
39             =head1 DESCRIPTION
40              
41             This module provides a third-party class attribute for L-based
42             classes, which applies some attributes automatically to every field added to
43             the class, as a convenient shortcut for making structure-like classes.
44              
45             =head1 CLASS ATTRIBUTES
46              
47             =head2 :Struct
48              
49             class Name :Struct ... { ... }
50              
51             Automatically applies the C<:param> and C<:mutator> attributes to every field
52             defined on the class, meaning the constructor will accept parameters for each
53             field to initialise the value, and each field will have an lvalue mutator
54             method.
55              
56             In addition, the class itself gains the C<:strict(params)> attribute, meaning
57             the constructor will check parameter names and throw an exception for
58             unrecognised names.
59              
60             =cut
61              
62             sub import
63             {
64 2     2   87 $^H{"Object::Pad::ClassAttr::Struct/Struct"}++;
65             }
66              
67             =head1 AUTHOR
68              
69             Paul Evans
70              
71             =cut
72              
73             0x55AA;