File Coverage

blib/lib/Readonly/Enum.pm
Criterion Covered Total %
statement 27 27 100.0
branch 8 8 100.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 0 1 0.0
total 44 46 95.6


line stmt bran cond sub pod time code
1             package Readonly::Enum;
2              
3 2     2   473139 use v5.10;
  2         10  
4              
5 2     2   12 use strict;
  2         4  
  2         53  
6 2     2   12 use warnings;
  2         4  
  2         144  
7              
8 2     2   1047 use version 0.77; our $VERSION = version->declare('v0.1.5');
  2         4973  
  2         15  
9              
10 2     2   271 use Carp;
  2         4  
  2         231  
11 2     2   1123 use Scalar::Readonly qw/ readonly_on /;
  2         1713  
  2         657  
12              
13             =head1 NAME
14              
15             Readonly::Enum - enumerated scalar values
16              
17             =head1 SYNOPSIS
18              
19             use Readonly::Enum;
20              
21             # $foo = 1, $bar = 2, etc.
22              
23             Readonly::Enum my ($foo, $bar, $baz);
24              
25             # $foo = 0, $bar = 1, etc.
26              
27             Readonly::Enum my ($foo, $bar, $baz) => 0;
28              
29             # $foo = 0, $bar = 5, $baz = 6, etc.
30              
31             Readonly::Enum my ($foo, $bar, $baz) => (0, 5);
32              
33             =head1 DESCRIPTION
34              
35             This module provides some syntactic sugar for defining enumerated
36             scalar values.
37              
38             It is to L what the L package is to L.
39             Unlike enumerated constants, these scalars can be used as hash keys
40             and interpolated in strings.
41              
42             Unlike L, only integers are supported in this version.
43              
44             =head1 AUTHOR
45              
46             Robert Rothenberg C.
47              
48             Current Maintainer, Nigel Horne, C<< >>
49              
50             =head1 LICENSE AND COPYRIGHT
51              
52             Copyright 2013-2014 Robert Rothenberg.
53              
54             This program is free software; you can redistribute it and/or modify it
55             under the terms of the the Artistic License (2.0). You may obtain a
56             copy of the full license at:
57              
58             L
59              
60             Any use, modification, and distribution of the Standard or Modified
61             Versions is governed by this Artistic License. By using, modifying or
62             distributing the Package, you accept this license. Do not use, modify,
63             or distribute the Package, if you do not accept this license.
64              
65             If your Modified Version has been derived from a Modified Version made
66             by someone other than you, you are nevertheless required to ensure that
67             your Modified Version complies with the requirements of this license.
68              
69             This license does not grant you the right to use any trademark, service
70             mark, tradename, or logo of the Copyright Holder.
71              
72             This license includes the non-exclusive, worldwide, free-of-charge
73             patent license to make, have made, use, offer to sell, sell, import and
74             otherwise transfer the Package with respect to any patent claims
75             licensable by the Copyright Holder that are necessarily infringed by the
76             Package. If you institute patent litigation (including a cross-claim or
77             counterclaim) against any party alleging that the Package constitutes
78             direct or contributory patent infringement, then this Artistic License
79             to you shall terminate on the date that such litigation is filed.
80              
81             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
82             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
83             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
84             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
85             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
86             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
87             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
88             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
89              
90             =cut
91              
92             sub Readonly::Enum
93             {
94 9     9 0 576051 my @vals = grep { defined $_ } @_;
  31         80  
95              
96 9         19 my $i;
97              
98 9         27 my $start = 0;
99              
100 9         47 for($i = 0; $i < @_; $i++) {
101 25 100       68 last if defined $_[$i];
102              
103 20 100       53 $start = @vals ? (shift @vals) : ++$start;
104              
105 20 100 66     162 Carp::croak('Initial values must be defined integers') unless(defined $start && $start =~ /^-?\d+\z/);
106              
107 19         77 readonly_on($_[$i] = $start);
108             }
109              
110 8 100       51 Carp::croak('Too many initial values') if(scalar(@vals));
111              
112             }
113              
114             1;