Constants.pm

filter: Statementstmt 92.6% 63 / 68 Branchbran 77.2% 17 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod n/a totaltotal 87.2% 89 / 102 CC 16 SCAR 17.9
CC
16
Cov
89%
import
11.1
_load_module
5.2
_load_all_modules
2.0
CRAP 6.0
↑ Summary Constants/XML/SAX/Handler.pm »
1  
2 package Config::Constants;
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 our $VERSION = '0.03';
8  
9 13 my %CONFIG_LOADER_CLASSES = (
10 perl => 'Config::Constants::Perl',
11 xml => 'Config::Constants::XML'
12 );
13  
14 # our config object
15 13 my $CONFIG;
16  
17 # these two hashes are used
18 # for checking to make sure
19 # no invalid constants are used
20 my %UNCHECKED_CONSTANTS;
21 13 my %CHECKED_CONSTANTS;
22  
23 31 ▶sub import {
Subroutine import: called CC=11 SCAR=24.0
24 31 shift;
25 31 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 30 31
26 30 my @args = @_;
27 30 ▶ if ($args[0] =~ /xml|perl/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] =~ /xml|perl/) { } 11 19 30
28 11 my ($type, $file) = @args;
29 11 my $config_loader_class = $CONFIG_LOADER_CLASSES{$type};
30 # try to load the config loader ...
31 11 ▶ eval "use $config_loader_class";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶ die "Failed to load config loader class ($config_loader_class) : $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
33 # try to load the config ...
34 11 $CONFIG = eval { $config_loader_class->new($file) };
35 11 ▶ die "Failed to load config (type => $type, file => $file) : $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 10 11
36 10 _load_all_modules();
37 }
38 else {
39 19 my $calling_pkg = caller();
40 19 ▶ if (exists $UNCHECKED_CONSTANTS{$calling_pkg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $UNCHECKED_CONSTANTS{$calling_pkg}) { } 13 6 19
41 # this means that the conf was loaded first, so we
42 # need to check the constants which were created
43 # against the ones which are allowed for this
44 # module, and if we find we have created extra
45 # ones we throw an exception
46 13 foreach my $arg (@args) {
47 delete $UNCHECKED_CONSTANTS{$calling_pkg}->{$arg}
48 20 ▶ if exists $UNCHECKED_CONSTANTS{$calling_pkg}->{$arg}
Branch: 1 branch
BranchTrueFalseTotal
if exists $UNCHECKED_CONSTANTS{$calling_pkg}{$arg} 11 9 20
49 }
50 die "Unchecked constants found in config for '$calling_pkg' -> (" .
51 0 join(", " => keys(%{$UNCHECKED_CONSTANTS{$calling_pkg}})) .
52 13 ▶ ")" if keys %{$UNCHECKED_CONSTANTS{$calling_pkg}};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$UNCHECKED_CONSTANTS{$calling_pkg};} 0 13 13
53 }
54 else {
55 # this means the conf has not been loaded yet,
56 # so we need to build a list of acceptable
57 # constants to check against.
58 6 $CHECKED_CONSTANTS{$calling_pkg} = { map { $_ => undef } @args };
59 }
60 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 19 foreach my $arg (@args) {
62 # skip it if it has already been defined
63 # NOTE: this means that the config was
64 # loaded before the module itself was
65 # loaded, so we don't want to overwrite
66 29 ▶ next if defined &{"${calling_pkg}::$arg"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${calling_pkg}::$arg";} 19 10 29
67 # However, if it hasn't been defined, then
68 # we want to do so. This will create a
69 # stub sub which will die if it is not
70 # configured.
71 # NOTE: the sub does not have the () prototype
72 # here so that we can prevent constant folding
73 # from happening. When the proper sub gets
74 # installed, it will have that prototype (and
75 # thus be folded in)
76 0 ▶ *{"${calling_pkg}::$arg"} = sub { die "undefined Config::Constant in ${calling_pkg}::$arg" };
Subroutine __ANON__: not called CC=1 SCAR=0.4
77 }
78 }
79 }
80  
81 ## For DEBUGGING
82 #INIT {
83 # use Data::Dumper;
84 # print "UNCHECKED: " . Dumper \%UNCHECKED_CONSTANTS;
85 # print "CHECKED: " . Dumper \%CHECKED_CONSTANTS;
86 #}
87  
88 ## Private Utility Functions
89  
90 10 ▶sub _load_all_modules {
Subroutine _load_all_modules: called CC=2 SCAR=6.9
91 10 foreach my $module ($CONFIG->modules()) {
92 17 _load_module($module);
93 }
94 }
95  
96 17 ▶sub _load_module {
Subroutine _load_module: called CC=5 SCAR=16.6
97 17 my $module = shift;
98 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 17 foreach my $constant ($CONFIG->constants($module)) {
100 26 my ($name, $value) = each %{$constant};
101 26 ▶ if (defined &{"${module}::$name"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"${module}::$name";}) { } 9 17 26
102 # since this already exists, we
103 # assume that it is a valid constant
104 # and that our module was already
105 # loaded
106 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 0 ▶ *{"${module}::$name"} = sub () { $value };
Subroutine __ANON__: not called CC=1 SCAR=0.4
108 9 delete $CHECKED_CONSTANTS{$module}->{$name};
109 }
110 else {
111 # since this does not exist, then our
112 # module may not have been loaded yet.
113 # but in order to determine this,
114 # we have to see if the constants have
115 # been registered yet ...
116 17 ▶ if (exists $CHECKED_CONSTANTS{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CHECKED_CONSTANTS{$module}) { } 0 17 17
117 # this means our module was loaded first, and
118 # the constants were registered. Now, if
119 # we do not see this particular constant
120 # in here, then we need to throw an exception
121 die "Unknown constant for '$module' -> ($name)"
122 0 ▶ unless exists $CHECKED_CONSTANTS{$module}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CHECKED_CONSTANTS{$module}{$name} 0 0 0
123 }
124 else {
125 # being in this block means that is is unlikely
126 # the conf has been loaded yet, so we will
127 # just create our constants, and make a note
128 # of each of them so that they can be
129 # checked later on.
130 17 $UNCHECKED_CONSTANTS{$module} = { $name => undef };
131 0 ▶ *{"${module}::$name"} = sub () { $value };
Subroutine __ANON__: not called CC=1 SCAR=0.4
132 }
133 }
134 }
135 }
136  
137  
138 13 1;
139  
140 __END__
↑ Summary Constants/XML/SAX/Handler.pm »