File Coverage

blib/lib/exported/constants.pm
Criterion Covered Total %
statement 27 29 93.1
branch 2 4 50.0
condition n/a
subroutine 6 7 85.7
pod n/a
total 35 40 87.5


line stmt bran cond sub pod time code
1             package exported::constants;
2              
3 1     1   15657 use strict;
  1         1  
  1         22  
4 1     1   3 use warnings;
  1         2  
  1         39  
5              
6 1     1   3 use vars '$VERSION'; $VERSION = '1.0';
  1         5  
  1         33  
7              
8 1     1   3 use Carp qw/ carp /;
  1         0  
  1         59  
9              
10             sub import {
11 1     1   6 my ($class, @args) = @_;
12              
13 1         2 my $pkg = caller();
14              
15 1     1   4 no strict 'refs';
  1         1  
  1         135  
16              
17 1 50       2 push @{$pkg.'::ISA'}, 'Exporter' unless grep { $_ eq 'Exporter' } @{$pkg.'::ISA'};
  1         6  
  0         0  
  1         7  
18              
19 1         3 while (@args) {
20 2 50       4 carp "Last constant $args[0] is missing a definition" unless @args >= 2;
21              
22 2         3 my ($name, $value) = splice @args, 0, 2;
23              
24 2     0   9 *{$pkg.'::'.$name} = sub () { $value };
  2         7  
  0         0  
25              
26 2         1 push @{$pkg.'::EXPORT'}, $name;
  2         41  
27             }
28             }
29              
30             1;
31              
32             =head1 NAME
33              
34             exported::constants - Declare constants and export them automatically
35              
36             =head1 SYNOPSIS
37              
38             package MyProg::Constants;
39              
40             use exported::constants
41             USER_TYPE_USER => 'U',
42             USER_TYPE_APPLICATION => 'A',
43             USER_TYPE_ROBOT => 'B',
44             ;
45              
46             package MyProg::App;
47              
48             use MyProg::Constants;
49              
50             my @real_users = $users->search({ user_type => USER_TYPE_USER, });
51              
52             =head1 DESCRIPTION
53              
54             This is a boilerplate-removal module for creating modules of just constants in your program.
55             This is useful if you have a lot of magic numbers you want to eliminate,
56             especially things that show up in database schemas or APIs that you want to re-use across multiple modules.
57              
58             It's pretty simple to use;
59             just say
60              
61             use exported::constants
62             CONSTANT1 => $value1,
63             CONSTANT2 => $value2,
64             ;
65              
66             and your package is automatically an exporter,
67             and automatically exports (by default) all the constants listed.
68              
69             =head1 RESTRICTIONS
70              
71             =over
72              
73             =item * List constants don't work, because C is intended to always create multiple constants in a single invocation.
74              
75             =item * This module always works using C<@EXPORT> in L; this is unfortunate for developers who want to explicitly import all their constants.
76              
77             =back
78              
79             =head1 SEE ALSO
80              
81             =over
82              
83             =item * L
84              
85             =item * L
86              
87             =back
88              
89             =head1 AUTHOR
90              
91             Jonathan Cast
92              
93             =head1 COPYRIGHT AND LICENSE
94              
95             Copyright 2017 Jonathan Cast
96              
97             Licensed under the Apache License, Version 2.0 (the "License").