blib/lib/Object/PadX/Enum.pm

filter: Statementstmt 98.8% 166 / 168 Branchbran 76.9% 40 / 52 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 23 / 23 Podpod n/a totaltotal 92.1% 236 / 256 CC 37 SCAR 15.5
CC
37
Cov
93%
_finalize_enum
12.0
_begin_enum
8.0
_split_versioned_pkg
4.6
CRAP 4.7
↑ Summary lib/Object/PadX/Enum.xs »
1 package Object::PadX::Enum 0.03;
2  
3 14 ▶use v5.22;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Object::Pad 0.825 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Object::Pad::MOP::Class qw( :experimental(mop) );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Loaded for its XS keyword registrations
11 9 require XSLoader;
12 9 XSLoader::load( __PACKAGE__, our $VERSION );
13  
14 =encoding UTF-8
15  
16 =for highlighter language=perl
17  
18 =head1 NAME
19  
20 C<Object::PadX::Enum> - syntactic sugar for enum-like singleton-bearing C<Object::Pad> classes
21  
22 =head1 SYNOPSIS
23  
24 use Object::PadX::Enum;
25  
26 enum Raptor {
27 item VELOCIRAPTOR ( max_speed_kmh => 60, max_weight_kg => 15, max_height_cm => 50 );
28 item DEINONYCHUS ( max_speed_kmh => 50, max_weight_kg => 80, max_height_cm => 87 );
29 item UTAHRAPTOR ( max_speed_kmh => 35, max_weight_kg => 500, max_height_cm => 150 );
30 item MICRORAPTOR ( max_speed_kmh => 40, max_weight_kg => 1, max_height_cm => 30 );
31 item DROMAEOSAURUS ( max_speed_kmh => 60, max_weight_kg => 15, max_height_cm => 50 );
32  
33 field $max_speed_kmh :param :reader;
34 field $max_weight_kg :param :reader;
35 field $max_height_cm :param :reader;
36  
37 method speed_per_kg { return $max_speed_kmh / $max_weight_kg }
38 method speed_per_cm { return $max_speed_kmh / $max_height_cm }
39  
40 method fastest :common {
41 my ( $top ) = sort { $b->max_speed_kmh <=> $a->max_speed_kmh } $class->values;
42 return $top;
43 }
44 }
45  
46 say Raptor->VELOCIRAPTOR->max_speed_kmh; # 60
47 say Raptor->DEINONYCHUS->speed_per_kg; # 0.625
48 say Raptor->from_ordinal(2)->name; # UTAHRAPTOR
49 say Raptor->from_name("MICRORAPTOR")->speed_per_cm; # 1.33333333333333
50 say 'Fastest in absolute terms: ', Raptor->fastest->name; # VELOCIRAPTOR or DROMAEOSAURUS (tie)
51  
52 =head1 DESCRIPTION
53  
54 C<Object::PadX::Enum> adds two keywords on top of L<Object::Pad>:
55  
56 =over 4
57  
58 =item * C<enum NAME ATTRS? { ... }>
59  
60 Declares a class (using L<Object::Pad>'s C<class> machinery) and auto-injects
61 C<$ordinal :reader> and C<name :reader> fields. The C<name> reader returns the
62 identifier under which the singleton was declared (e.g. C<"RED">). Inside the
63 block, all normal C<Object::Pad> constructs (C<field>, C<method>, C<ADJUST>,
64 ...) are available, plus the C<item> keyword.
65  
66 The entire block body is executed while it is being compiled, BEGIN-block
67 style; see L</COMPILE-TIME SEMANTICS>.
68  
69 The following class-level attributes are accepted:
70  
71 =over 4
72  
73 =item C<:isa(CLASS)>, C<:isa(CLASS VERSION)>
74  
75 =item C<:extends(CLASS)>, C<:extends(CLASS VERSION)>
76  
77 Declares a superclass; equivalent to L<Object::Pad>'s C<:isa>. The package is
78 loaded automatically. If a VERSION is given, C<< CLASS->VERSION(VERSION) >> is
79 called to enforce it.
80  
81 An C<enum> may inherit from another C<enum>. Fields, methods, roles and
82 C<ADJUST> phasers from the parent are inherited normally. The parent's
83 B<items> are I<not> inherited: the child has its own ordinal-zero-based item
84 sequence, and accessing a parent item name on the child raises an error. The
85 child's C<values>, C<from_ordinal> and C<from_name> see only the child's
86 items. A parent enum must be declared before a child enum that inherits from
87 it; since enums are finalized while they are compiled, normal source ordering
88 and C<use> ordering satisfies this.
89  
90 =item C<:does(ROLE)>, C<:does(ROLE VERSION)>
91  
92 Composes a role into the enum class. May be repeated for multiple roles. The
93 role package is loaded automatically.
94  
95 =back
96  
97 The class attributes C<:abstract>, C<:strict>, C<:repr> and C<:lexical_new>
98 are not supported. C<:abstract> is semantically incompatible with C<item>
99 (singletons cannot be constructed for an abstract class); the others have no
100 public L<Object::Pad::MOP::Class> entry point and would require reaching into
101 private Object::Pad internals.
102  
103 =item * C<item NAME ( ARGS );>
104  
105 Declares a named singleton instance of the enclosing C<enum>. C<ARGS> is the
106 key/value list passed to the auto-generated constructor; the parentheses (and
107 the arg list) are optional, so C<item FOO;> is equivalent to C<item FOO();>.
108  
109 C<ARGS> is evaluated at compile time and therefore must not depend on runtime
110 state; see L</COMPILE-TIME SEMANTICS>.
111  
112 =back
113  
114 After the C<enum> block closes, the following class-level methods are
115 installed on the enum class for each declared singleton C<NAME>:
116  
117 $singleton = ClassName->NAME; # the named singleton
118 @all = ClassName->values; # all singletons in declaration order
119 $byord = ClassName->from_ordinal(0);
120 $byname = ClassName->from_name("RED");
121  
122 The per-item accessors are installed as inlinable constant subs (of the kind
123 created by L<constant>), so a function-style call such as
124 C<< ClassName::NAME() >> is folded to a constant at the caller's compile
125 time. Method-call syntax C<< ClassName->NAME >> dispatches through the same
126 sub and returns the same singleton.
127  
128 Direct construction via C<< ClassName->new(...) >> is blocked after the
129 C<enum> block closes; the only ways to obtain a singleton are the per-item
130 accessor, C<from_name>, and C<from_ordinal>. Subclasses (whether plain
131 C<class> or another C<enum>) may still call C<new> on themselves; the block
132 applies only to direct invocation on the enum class itself.
133  
134 =head1 COMPILE-TIME SEMANTICS
135  
136 Enums are static, fixed sets of values, and the implementation treats them
137 that way: the whole C<enum> block body is executed while it is being
138 compiled, much like a C<BEGIN> block. In particular:
139  
140 =over 4
141  
142 =item *
143  
144 The singletons are constructed, and all accessors installed, as soon as the
145 closing brace of the C<enum> block has been parsed. They are visible from any
146 code compiled afterwards, including C<BEGIN> blocks later in the same file.
147  
148 =item *
149  
150 C<item> args are ordinary Perl expressions, but they are evaluated at compile
151 time. An expression referencing runtime state (for example a lexical assigned
152 during normal runtime) sees the value that variable has at compile time,
153 usually C<undef>. Use only compile-time-computable expressions in C<item>
154 args.
155  
156 =item *
157  
158 Any other plain statements written inside the block also run at compile time.
159  
160 =back
161  
162 Because construction happens at compile time, values derived from item args
163 can be precomputed once per singleton instead of recomputed on every call.
164 Assign them to a field in an C<ADJUST> block:
165  
166 enum Raptor {
167 item VELOCIRAPTOR ( max_speed_kmh => 60, max_weight_kg => 15 );
168  
169 field $max_speed_kmh :param :reader;
170 field $max_weight_kg :param :reader;
171  
172 field $speed_per_kg :reader;
173 ADJUST { $speed_per_kg = $max_speed_kmh / $max_weight_kg }
174 }
175  
176 C<< Raptor->VELOCIRAPTOR->speed_per_kg >> then returns a value computed once,
177 at compile time, through a plain generated reader instead of a computing
178 method.
179  
180 =head1 CAVEATS
181  
182 =over 4
183  
184 =item *
185  
186 User C<field>s require explicit C<:param> if you intend to set them via
187 C<item> args. C<Object::PadX::Enum> does I<not> inject C<:param> automatically.
188  
189 =item *
190  
191 The C<enum> block body executes at compile time, so C<item> args (and any
192 other statements inside the block) must not depend on runtime state. See
193 L</COMPILE-TIME SEMANTICS>.
194  
195 =item *
196  
197 C<enum>-level C<:abstract>, C<:strict>, C<:repr> and C<:lexical_new> are not
198 supported. See the description of the C<enum> keyword above for the rationale;
199 C<:isa> and C<:does> I<are> supported.
200  
201 =item *
202  
203 The names C<values>, C<from_ordinal>, C<from_name>, C<ordinal> and C<name> are
204 reserved and must not be used as C<item> names.
205  
206 =back
207  
208 =cut
209  
210 # Per-class state captured during compilation.
211 # $Pending{$class} = { meta => $meta, items => [ [ $name, \@args, $line ], ... ], seen => { $name => 1 } }
212 9 my %Pending;
213  
214 # Permanent per-class registry of finalized enum item names, in declaration
215 # order. Populated by `_finalize_enum`. Queried by descendant enum finalizes
216 # (to shadow inherited item accessors) and could be useful for introspection
217 # in the future. Keys are class names; values are arrayrefs of item names.
218 my %EnumItems;
219  
220 9 my %RESERVED_ITEM_NAMES = map { $_ => 1 } qw(
221 values from_ordinal from_name ordinal name
222 new BUILD DOES META
223 );
224  
225 21 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
226 21 my $class = shift;
227 21 my $caller = caller;
228  
229 21 $^H{ 'Object::PadX::Enum/enum' } = 1;
230 21 $^H{ 'Object::PadX::Enum/item' } = 1;
231  
232 21 Object::Pad->import_into( $caller );
233 }
234  
235 # Attributes that have a documented public-MOP entry point.
236 9 my %ENUM_ATTR_HANDLERS = (
237 isa => \&_attr_isa,
238 extends => \&_attr_isa,
239 does => \&_attr_does,
240 );
241  
242 # Attributes that exist on Object::Pad's `class` keyword but are deliberately
243 # rejected here. The message explains why so users aren't left guessing.
244 9 my %ENUM_ATTR_REJECTED = (
245 abstract => "':abstract' is incompatible with enum: singleton values cannot be constructed for an abstract class",
246 strict => "':strict' is not supported on enum (no public Object::Pad MOP entry point); declare a plain 'class' instead",
247 repr => "':repr' is not supported on enum (no public Object::Pad MOP entry point); declare a plain 'class' instead",
248 lexical_new => "':lexical_new' is not supported on enum (no public Object::Pad MOP entry point); declare a plain 'class' instead",
249 );
250  
251 # Load $pkg via `require`, mirroring Object::Pad's :isa/:does autoload. Returns
252 # silently on success; croaks on failure.
253 14 ▶sub _require_package {
Subroutine _require_package: called CC=3 SCAR=11.2
254 14 my ( $pkg, $for ) = @_;
255  
256 # Skip require for packages already defined inline (no .pm needed).
257 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
258 14 ▶ keys %{ "${pkg}::" } and return;
Branch: 1 branch
BranchTrueFalseTotal
keys %{"${pkg}::";} and return 13 1 14
259  
260 1 ( my $file = "$pkg.pm" ) =~ s{::}{/}g;
261 0 ▶ eval { require $file; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $file; 1 } } or croak("Failed to load package '${pkg}' for ${for}: $@") 0 1 1
262 or croak "Failed to load package '$pkg' for $for: $@";
263  
264 0 return;
265 }
266  
267 # Parse "Pkg" or "Pkg VER" into ($pkg, $ver). $ver is undef when absent.
268 14 ▶sub _split_versioned_pkg {
Subroutine _split_versioned_pkg: called CC=4 SCAR=15.2
269 14 my ( $raw, $attr_name ) = @_;
270  
271 14 ▶ defined $raw && length $raw
Branch: 1 branch
BranchTrueFalseTotal
defined $raw and length $raw or croak("Attribute ':${attr_name}' requires a value") 14 0 14
Condition33%1/3
defined $raw and length $raw
!ll&&!rl&&r
0014
MC/DC0%0/2
defined $raw and length $raw
defined $raw length $raw
Truth table33%1/3
A and B
A= defined $rawB= length $raw
ABresult
0X0
100
111
272 or croak "Attribute ':$attr_name' requires a value";
273  
274 14 my ( $pkg, $ver, $extra ) = split /\s+/, $raw, 3;
275 14 ▶ defined $extra
Branch: 1 branch
BranchTrueFalseTotal
defined $extra and croak("Attribute ':$attr_name($raw)' has too many parts; expected 'PACKAGE' or 'PACKAGE VERSION'") 0 14 14
276 and croak "Attribute ':$attr_name($raw)' has too many parts; expected 'PACKAGE' or 'PACKAGE VERSION'";
277  
278 14 return ( $pkg, $ver );
279 }
280  
281 11 ▶sub _attr_isa {
Subroutine _attr_isa: called CC=3 SCAR=11.0
282 11 my ( $state, $value ) = @_;
283  
284 exists $state->{ isa }
285 11 ▶ and croak "Multiple ':isa' / ':extends' attributes on enum '$state->{name}'";
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"isa"} and croak(qq[Multiple ':isa' / ':extends' attributes on enum '$state->{"name"}']) 1 10 11
286  
287 10 my ( $pkg, $ver ) = _split_versioned_pkg( $value, 'isa' );
288 10 _require_package( $pkg, "':isa($pkg)' on enum '$state->{name}'" );
289 9 ▶ defined $ver and $pkg->VERSION( $ver );
Branch: 1 branch
BranchTrueFalseTotal
defined $ver and $pkg->VERSION($ver) 2 7 9
290  
291 8 $state->{ isa } = $pkg;
292 8 return;
293 }
294  
295 4 ▶sub _attr_does {
Subroutine _attr_does: called CC=2 SCAR=7.0
296 4 my ( $state, $value ) = @_;
297  
298 4 my ( $pkg, $ver ) = _split_versioned_pkg( $value, 'does' );
299 4 _require_package( $pkg, "':does($pkg)' on enum '$state->{name}'" );
300 4 ▶ defined $ver and $pkg->VERSION( $ver );
Branch: 1 branch
BranchTrueFalseTotal
defined $ver and $pkg->VERSION($ver) 0 4 4
301  
302 4 push @{ $state->{ roles } }, $pkg;
303 4 return;
304 }
305  
306 # Called by XS at compile-time when `enum NAME ATTRS? {` is encountered.
307 29 ▶sub _begin_enum {
Subroutine _begin_enum: called CC=8 SCAR=20.8
308 29 my ( $name, $attrs ) = @_;
309  
310 29 ▶ exists $Pending{ $name }
Branch: 1 branch
BranchTrueFalseTotal
exists $Pending{$name} and croak("Cannot declare enum '${name}'; already being defined") 0 29 29
311 and croak "Cannot declare enum '$name'; already being defined";
312  
313 29 my $state = { name => $name, roles => [] };
314 29 ▶ for my $pair ( @{ $attrs // [] } ) {
Condition50%1/2
$attrs // []
l!l
290
MC/DC0%0/1
$attrs // []
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
315 19 my ( $attr, $value ) = @$pair;
316  
317 19 ▶ if ( my $msg = $ENUM_ATTR_REJECTED{ $attr } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $ENUM_ATTR_REJECTED{$attr}) 3 16 19
318 3 croak "$msg (enum '$name')";
319 }
320  
321 16 ▶ my $handler = $ENUM_ATTR_HANDLERS{ $attr }
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $ENUM_ATTR_HANDLERS{$attr} or croak("Unrecognised attribute ':${attr}' on enum '${name}'") 15 1 16
322 or croak "Unrecognised attribute ':$attr' on enum '$name'";
323  
324 15 $handler->( $state, $value );
325 }
326  
327 22 my @begin_args = ( $name );
328 exists $state->{ isa }
329 22 ▶ and push @begin_args, ( isa => $state->{ isa } );
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"isa"} and push @begin_args, ("isa", $state->{"isa"}) 7 15 22
330  
331 22 my $meta = Object::Pad::MOP::Class->begin_class( @begin_args );
332  
333 22 $meta->add_role( $_ ) for @{ $state->{ roles } };
334  
335 # $ordinal and $_name are reader-only (not :param) so user item args cannot
336 # override them; both are stamped after construction in _finalize_enum.
337 22 ▶ $meta->add_field( '$ordinal', reader => 'ordinal' );
Subroutine ordinal: called CC=1 SCAR=0.0
338 22 ▶ $meta->add_field( '$_name', reader => 'name' );
Subroutine name: called CC=1 SCAR=0.0
339  
340 22 $Pending{ $name } = { meta => $meta, items => [], seen => {} };
341  
342 22 return;
343 }
344  
345 # Called at compile time (during execution of the enum body CV), in source
346 # order, for each `item NAME(args)` statement.
347 38 ▶sub _register_item {
Subroutine _register_item: called CC=4 SCAR=13.9
348 38 my ( $class, $name, $line, @args ) = @_;
349  
350 38 ▶ my $entry = $Pending{ $class }
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $Pending{$class} or croak("Internal error: item '${name}' for unknown enum '${class}' at line $line") 35 0 35
351 or croak "Internal error: item '$name' for unknown enum '$class' at line $line";
352  
353 35 ▶ $entry->{ seen }{ $name }
Branch: 1 branch
BranchTrueFalseTotal
$entry->{"seen"}{$name} and croak("Duplicate item '${name}' in enum '${class}' at line $line") 1 34 35
354 and croak "Duplicate item '$name' in enum '$class' at line $line";
355  
356 34 ▶ $RESERVED_ITEM_NAMES{ $name }
Branch: 1 branch
BranchTrueFalseTotal
$RESERVED_ITEM_NAMES{$name} and croak("item name '${name}' is reserved in enum '${class}' at line $line") 2 32 34
357 and croak "item name '$name' is reserved in enum '$class' at line $line";
358  
359 32 push @{ $entry->{ items } }, [ $name, \@args, $line ];
360 32 $entry->{ seen }{ $name } = 1;
361  
362 32 return;
363 }
364  
365 # Called at compile time, once, after all item statements for the enum have
366 # run, while the closing brace of the enum block is being processed.
367 19 ▶sub _finalize_enum {
Subroutine _finalize_enum: called CC=12 SCAR=24.9
368 19 my ( $class ) = @_;
369  
370 19 ▶ my $entry = delete $Pending{ $class }
Branch: 1 branch
BranchTrueFalseTotal
my $entry = delete $Pending{$class} or croak("Internal error: _finalize_enum on unknown enum '${class}'") 19 0 19
371 or croak "Internal error: _finalize_enum on unknown enum '$class'";
372  
373 19 my $meta = $entry->{ meta };
374 19 my $ord_field = $meta->get_field( '$ordinal' );
375 19 my $name_field = $meta->get_field( '$_name' );
376 19 my @ordered;
377  
378 # Instances cannot be constructed before the class is sealed. begin_class
379 # queues an auto-seal for UNITCHECK, but that is too late for compile-time
380 # construction; seal now. The later auto-seal is a no-op on an
381 # already-sealed class.
382 19 $meta->seal;
383  
384 19 my $n = 0;
385 19 for my $item ( @{ $entry->{ items } } ) {
386 31 my ( $name, $args, $line ) = @$item;
387  
388 31 my $instance = eval { $class->new( @$args ) };
389 31 ▶ $@ and croak "Failed to construct enum value '$name' of '$class' at line $line: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and croak("Failed to construct enum value '${name}' of '${class}' at line ${line}: $@") 0 31 31
390  
391 # Stamp the ordinal and name after construction so they aren't user-facing :params.
392 31 $ord_field->value( $instance ) = $n;
393 31 $name_field->value( $instance ) = $name;
394  
395 31 push @ordered, [ $name, $instance ];
396 31 $n++;
397 }
398  
399 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
400 9 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
401  
402 # Item accessors are installed as inlinable constant subs (the same
403 # stash-slot technique constant.pm uses): a reference to a readonly scalar
404 # in the stash slot makes `Colors::RED()` constant-fold at the caller's
405 # compile time. Method-call syntax `Colors->RED` dispatches through the
406 # same constant sub unchanged.
407 19 my $stash = \%{ "${class}::" };
408 19 my %own_names;
409 19 for my $pair ( @ordered ) {
410 31 my ( $name, $instance ) = @$pair;
411 31 $own_names{ $name } = 1;
412 31 my $const = $instance;
413 31 Internals::SvREADONLY( $const, 1 );
414 31 $stash->{ $name } = \$const;
415 }
416  
417 19 ▶ *{ "${class}::values" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
418 5 return map { $_->[1] } @ordered;
419 19 };
420  
421 19 ▶ *{ "${class}::from_ordinal" } = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
422 6 my ( undef, $idx ) = @_;
423 6 ▶ defined $idx or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $idx or return undef 6 0 6
424 6 ▶ $idx >= 0 && $idx < @ordered or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$idx >= 0 and $idx < @ordered or return undef 4 2 6
Condition100%3/3
$idx >= 0 and $idx < @ordered
!ll&&!rl&&r
114
MC/DC100%2/2
$idx >= 0 and $idx < @ordered
$idx >= 0 $idx < @ordered
Truth table100%3/3
A and B
A= $idx >= 0B= $idx < @ordered
ABresult
0X0
100
111
425 4 return $ordered[ $idx ][ 1 ];
426 19 };
427  
428 19 ▶ *{ "${class}::from_name" } = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
429 6 my ( undef, $want ) = @_;
430 6 ▶ defined $want or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $want or return undef 6 0 6
431 6 for my $pair ( @ordered ) {
432 13 ▶ return $pair->[1] if $pair->[0] eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if $pair->[0] eq $want 4 9 13
433 }
434 2 return undef;
435 19 };
436  
437 # Shadow ancestor enum items not redefined locally. A child enum inherits
438 # fields/methods from a parent enum but loses the parent's items: accessing
439 # a parent item name on the child raises a clear error rather than
440 # returning the parent's singleton via MRO.
441 19 require mro;
442 19 my $linear = mro::get_linear_isa( $class );
443 19 my %shadowed;
444 19 for my $ancestor ( @$linear ) {
445 46 ▶ next if $ancestor eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if $ancestor eq $class 19 27 46
446 27 ▶ my $ancestor_items = $EnumItems{ $ancestor } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ancestor_items = $EnumItems{$ancestor} or next 4 23 27
447 4 for my $aname ( @$ancestor_items ) {
448 8 ▶ next if $own_names{ $aname };
Branch: 1 branch
BranchTrueFalseTotal
if $own_names{$aname} 1 7 8
449 7 ▶ next if $shadowed{ $aname };
Branch: 1 branch
BranchTrueFalseTotal
if $shadowed{$aname} 0 7 7
450 7 $shadowed{ $aname } = $ancestor;
451 7 my $msg = "'$aname' is not an item of '$class' (inherited from '$ancestor', shadowed)";
452 7 ▶ *{ "${class}::${aname}" } = sub { croak $msg };
Subroutine __ANON__: called CC=1 SCAR=0.0
453 }
454 }
455  
456 # Register before installing the `new` override so any descendant enum
457 # whose finalize runs later (and which calls our `new` via MRO) sees us in
458 # the registry.
459 19 $EnumItems{ $class } = [ map { $_->[0] } @ordered ];
460  
461 # Block external construction. Capture the original Object::Pad-generated
462 # `new` so subclass enums (and plain subclasses) can pass through during
463 # their own construction; only direct calls on the enum class itself are
464 # rejected.
465 19 my @item_names = map { $_->[0] } @ordered;
466 19 my $orig_new = \&{ "${class}::new" };
467  
468 19 my $new_msg = "Cannot construct new instances of enum class '$class' directly";
469 19 ▶ if ( @item_names ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@item_names) 17 2 19
470 17 $new_msg .= '; use one of: ' . join( ', ', @item_names );
471 17 $new_msg .= " (or ${class}->from_name / ${class}->from_ordinal)";
472 }
473  
474 19 ▶ *{ "${class}::new" } = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
475 5 my $invocant = shift;
476 5 ▶ $invocant ne $class
Branch: 1 branch
BranchTrueFalseTotal
$invocant ne $class and return $invocant->$orig_new(@_) 0 5 5
477 and return $invocant->$orig_new( @_ );
478 5 croak $new_msg;
479 19 };
480  
481 19 return;
482 }
483  
484 9 0x55AA;
↑ Summary lib/Object/PadX/Enum.xs »