blib/lib/Constant/FromGlobal.pm

filter: Statementstmt 100% 59 / 59 Branchbran 100% 18 / 18 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 100% 100 / 100 CC 13 SCAR 17.2
CC
13
Cov
100%
_get_value
11.0
import
2.0
_process_constants
2.0
CRAP 5.6
↑ Summary
1 package Constant::FromGlobal;
2 5 $Constant::FromGlobal::VERSION = '0.10';
3 # ABSTRACT: declare constant(s) with value from global or environment variable
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Data::OptList;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use constant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
13 15 my ( $class, @args ) = @_;
14  
15 15 ▶ my $opt = ref($args[0]) eq 'HASH' ? shift @args : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] eq 'HASH' ? : 10 5 15
16  
17 15 @_ = (
18 "constant",
19 $class->_process_constants(
20 package => scalar(caller),
21 %$opt,
22 constants => \@args
23 )
24 );
25  
26 13 goto &constant::import;
27 }
28  
29 15 ▶sub _process_constants {
Subroutine _process_constants: called CC=2 SCAR=6.9
30 15 my ( $class, %args ) = @_;
31  
32 15 my $options = Data::OptList::mkopt(delete $args{constants}, "constant", 1, [qw(HASH ARRAY)]);
33  
34 15 my $constants = {};
35  
36 15 my $caller = $args{package};
37  
38 15 foreach my $constant ( @$options ) {
39 26 my ( $name, $opt ) = @$constant;
40  
41 26 $constants->{$name} = $class->_get_value(
42 %args,
43 name => $name,
44 %$opt,
45 );
46 }
47  
48 13 return $constants;
49 }
50  
51 26 ▶sub _get_value {
Subroutine _get_value: called CC=11 SCAR=24.0
52 26 my ( $class, %args ) = @_;
53  
54 26 my $value = $class->_get_var(%args);
55  
56 26 ▶ if ( not defined $value and $args{env} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and $args{"env"}) 20 6 26
Condition100%3/3
not defined $value and $args{"env"}
!ll&&!rl&&r
4220
MC/DC100%2/2
not defined $value and $args{"env"}
not defined $value $args{"env"}
Truth table100%3/3
not(A) and B
A= not defined $valueB= $args{"env"}
ABresult
0X0
100
111
57 20 $value = $class->_get_env_var(%args);
58 }
59  
60 26 ▶ if ( not defined $value and defined $args{default} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and defined $args{"default"}) 4 22 26
Condition100%3/3
not defined $value and defined $args{"default"}
!ll&&!rl&&r
2024
MC/DC100%2/2
not defined $value and defined $args{"default"}
not defined $value defined $args{"default"}
Truth table100%3/3
not(A) and B
A= not defined $valueB= defined $args{"default"}
ABresult
0X0
100
111
61 4 $value = $args{default};
62 }
63  
64 26 ▶ if ( $args{bool} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'bool'}) { } 8 18 26
elsif (defined $value) { } 17 1 18
65 8 return not not $value;
66 } elsif ( defined $value ) {
67 17 ▶ if ( $args{num} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'num'}) { } 5 12 17
elsif ($args{'int'}) { } 7 5 12
68 5 require Scalar::Util;
69 5 ▶ croak "'$value' does not look like a number" unless Scalar::Util::looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($value) 1 4 5
70 4 return 0+$value;
71 } elsif ( $args{int} ) {
72 7 ▶ croak "'$value' does not look like an integer" unless $value =~ /^\s* -? \d+ \s*$/x;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\s* -? \d+ \s*$/x 1 6 7
73 6 return int 0+$value;
74 }
75 }
76  
77 6 return $value;
78 }
79  
80 46 ▶sub _var_name {
Subroutine _var_name: called CC=1 SCAR=0.0
81 46 my ( $class, %args ) = @_;
82  
83 46 join "::", @args{qw(package name)};
84 }
85  
86 26 ▶sub _get_var {
Subroutine _get_var: called CC=1 SCAR=0.0
87 26 my ( $class, %args ) = @_;
88  
89 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 26 return ${ $class->_var_name(%args) }
91 }
92  
93 20 ▶sub _get_env_var {
Subroutine _get_env_var: called CC=1 SCAR=0.0
94 20 my ( $class, %args ) = @_;
95  
96 20 my $name = uc $class->_var_name(%args);
97 20 $name =~ s/^MAIN:://;
98 20 $name =~ s/::/_/g;
99  
100 20 $ENV{$name};
101 }
102  
103 # ex: set sw=4 et:
104  
105 5 __PACKAGE__
106  
107 __END__
↑ Summary