File Coverage

blib/lib/Enum/Declare.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
1             package Enum::Declare;
2              
3 7     7   636912 use 5.014;
  7         19  
4 7     7   29 use strict;
  7         31  
  7         163  
5 7     7   20 use warnings;
  7         7  
  7         487  
6              
7             our $VERSION = '0.03';
8              
9 7     7   2745 use Devel::CallParser;
  7         16337  
  7         400  
10 7     7   3165 use Object::Proto;
  7         4103  
  7         490  
11              
12             require XSLoader;
13             XSLoader::load('Enum::Declare', $VERSION);
14              
15 7     7   2578 use Enum::Declare::Meta;
  7         16  
  7         429  
16              
17             our %_registry;
18              
19             =head1 NAME
20              
21             Enum::Declare - Declarative enums with compile-time constants
22              
23             =head1 SYNOPSIS
24              
25             use Enum::Declare;
26              
27             # Basic integer enum (auto-increments from 0)
28             enum Colour {
29             Red,
30             Green,
31             Blue,
32             }
33              
34             say Red; # 0
35             say Green; # 1
36             say Blue; # 2
37              
38             # Explicit values
39             enum HttpStatus {
40             OK = 200,
41             NotFound = 404,
42             ServerError = 500,
43             }
44              
45             # String enum
46             enum LogLevel :Str {
47             Debug,
48             Info,
49             Warn = "warning",
50             }
51              
52             say Debug; # "debug"
53             say Warn; # "warning"
54              
55             # Bitflag enum (powers of 2)
56             enum Perms :Flags {
57             Read,
58             Write,
59             Execute,
60             }
61              
62             say Read; # 1
63             say Write; # 2
64             say Read | Write; # 3
65              
66             # Auto-exported constants
67             enum Colour :Export {
68             Red,
69             Green,
70             Blue,
71             }
72              
73             # Meta introspection
74             my $meta = Colour();
75             say $meta->count; # 3
76             say $meta->name(0); # "Red"
77             say $meta->value('Green'); # 1
78             say $meta->valid(2); # 1
79             my @pairs = $meta->pairs; # (Red => 0, Green => 1, Blue => 2)
80             my $names = $meta->names; # ['Red', 'Green', 'Blue']
81             my $values = $meta->values; # [0, 1, 2]
82              
83             =head1 DESCRIPTION
84              
85             Enum::Declare provides a declarative C keyword for defining enumerated
86             types in Perl. Constants are installed as true constant subs at compile time
87             and a metadata object is accessible via the enum name.
88              
89             =head2 Attributes
90              
91             Attributes are specified after the enum name with a colon prefix. Multiple
92             attributes may be combined.
93              
94             =over 4
95              
96             =item C<:Str>
97              
98             Values are strings. Variants without an explicit value default to their
99             lowercased name.
100              
101             =item C<:Flags>
102              
103             Values are assigned as ascending powers of 2 (1, 2, 4, 8, ...), suitable for
104             use as bitflags.
105              
106             =item C<:Export>
107              
108             Populates C<@EXPORT> and C<@EXPORT_OK> in the declaring package and adds
109             L to C<@ISA>, allowing consumers to import the constants via
110             C.
111              
112             =back
113              
114             =head2 Explicit Values
115              
116             Any variant may be given an explicit value with C<= VALUE>. For integer enums
117             subsequent variants auto-increment from the last explicit value. For string
118             enums the value must be a quoted string.
119              
120             enum Example {
121             A = 10,
122             B, # 11
123             C = 20,
124             D, # 21
125             }
126              
127             =head2 Meta Object
128              
129             Calling the enum name as a function returns an L object:
130              
131             my $meta = EnumName();
132              
133             Methods on the meta object:
134              
135             =over 4
136              
137             =item C
138              
139             Returns an arrayref of variant names.
140              
141             =item C
142              
143             Returns an arrayref of variant values.
144              
145             =item C
146              
147             Returns the variant name for the given value.
148              
149             =item C
150              
151             Returns the value for the given variant name.
152              
153             =item C
154              
155             Returns true if the value belongs to the enum.
156              
157             =item C
158              
159             Returns a flat list of name/value pairs.
160              
161             =item C
162              
163             Returns the number of variants.
164              
165             =item C
166              
167             Exhaustive pattern match. Every variant must have a corresponding handler in
168             the hash, or a C<_> wildcard default must be present. Dies with a
169             C error if any variants are unhandled.
170              
171             my $hex = Color()->match($val, {
172             Red => sub { '#ff0000' },
173             Green => sub { '#00ff00' },
174             Blue => sub { '#0000ff' },
175             });
176              
177             A wildcard C<_> handler catches any unmatched or unknown value:
178              
179             my $label = Color()->match($val, {
180             Red => sub { 'stop' },
181             _ => sub { 'go' },
182             });
183              
184             Each handler receives the matched value as its argument.
185              
186             =back
187              
188             =head1 AUTHOR
189              
190             LNATION C<< >>
191              
192             =head1 BUGS
193              
194             Please report any bugs or feature requests to C, or through
195             the web interface at L. I will be notified, and then you'll
196             automatically be notified of progress on your bug as I make changes.
197              
198             =head1 SUPPORT
199              
200             You can find documentation for this module with the perldoc command.
201              
202             perldoc Enum::Declare
203              
204              
205             You can also look for information at:
206              
207             =over 4
208              
209             =item * RT: CPAN's request tracker (report bugs here)
210              
211             L
212              
213             =item * Search CPAN
214              
215             L
216              
217             =back
218              
219             =head1 ACKNOWLEDGEMENTS
220              
221             =head1 LICENSE AND COPYRIGHT
222              
223             This software is Copyright (c) 2026 by LNATION .
224              
225             This is free software, licensed under:
226              
227             The Artistic License 2.0 (GPL Compatible)
228              
229              
230             =cut
231              
232             1; # End of Enum::Declare