line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Constant::FromGlobal; |
2
|
|
|
|
|
|
|
$Constant::FromGlobal::VERSION = '0.09'; |
3
|
|
|
|
|
|
|
# ABSTRACT: declare constant(s) with value from global or environment variable |
4
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
63645
|
use strict; |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
113
|
|
6
|
5
|
|
|
5
|
|
14
|
use warnings; |
|
5
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
87
|
|
7
|
5
|
|
|
5
|
|
56
|
use 5.8.0; |
|
5
|
|
|
|
|
13
|
|
8
|
5
|
|
|
5
|
|
15
|
use Carp; |
|
5
|
|
|
|
|
1
|
|
|
5
|
|
|
|
|
302
|
|
9
|
5
|
|
|
5
|
|
1780
|
use Data::OptList; |
|
5
|
|
|
|
|
29480
|
|
|
5
|
|
|
|
|
23
|
|
10
|
5
|
|
|
5
|
|
108
|
use constant (); |
|
5
|
|
|
|
|
6
|
|
|
5
|
|
|
|
|
1587
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub import { |
13
|
15
|
|
|
15
|
|
3010
|
my ( $class, @args ) = @_; |
14
|
|
|
|
|
|
|
|
15
|
15
|
100
|
|
|
|
42
|
my $opt = ref($args[0]) eq 'HASH' ? shift @args : {}; |
16
|
|
|
|
|
|
|
|
17
|
15
|
|
|
|
|
54
|
@_ = ( |
18
|
|
|
|
|
|
|
"constant", |
19
|
|
|
|
|
|
|
$class->process_constants( |
20
|
|
|
|
|
|
|
package => scalar(caller), |
21
|
|
|
|
|
|
|
%$opt, |
22
|
|
|
|
|
|
|
constants => \@args |
23
|
|
|
|
|
|
|
) |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
13
|
|
|
|
|
5948
|
goto &constant::import; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub process_constants { |
30
|
15
|
|
|
15
|
0
|
38
|
my ( $class, %args ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
15
|
|
|
|
|
77
|
my $options = Data::OptList::mkopt(delete $args{constants}, "constant", 1, [qw(HASH ARRAY)]); |
33
|
|
|
|
|
|
|
|
34
|
15
|
|
|
|
|
479
|
my $constants = {}; |
35
|
|
|
|
|
|
|
|
36
|
15
|
|
|
|
|
18
|
my $caller = $args{package}; |
37
|
|
|
|
|
|
|
|
38
|
15
|
|
|
|
|
26
|
foreach my $constant ( @$options ) { |
39
|
26
|
|
|
|
|
31
|
my ( $name, $opt ) = @$constant; |
40
|
|
|
|
|
|
|
|
41
|
26
|
|
|
|
|
76
|
$constants->{$name} = $class->get_value( |
42
|
|
|
|
|
|
|
%args, |
43
|
|
|
|
|
|
|
name => $name, |
44
|
|
|
|
|
|
|
%$opt, |
45
|
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
13
|
|
|
|
|
38
|
return $constants; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub get_value { |
52
|
26
|
|
|
26
|
0
|
44
|
my ( $class, %args ) = @_; |
53
|
|
|
|
|
|
|
|
54
|
26
|
|
|
|
|
48
|
my $value = $class->get_var(%args); |
55
|
|
|
|
|
|
|
|
56
|
26
|
100
|
66
|
|
|
85
|
if ( not defined $value and $args{env} ) { |
57
|
20
|
|
|
|
|
40
|
$value = $class->get_env_var(%args); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
26
|
100
|
100
|
|
|
72
|
if ( not defined $value and defined $args{default} ) { |
61
|
4
|
|
|
|
|
5
|
$value = $args{default}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
26
|
100
|
|
|
|
55
|
if ( $args{bool} ) { |
|
|
100
|
|
|
|
|
|
65
|
8
|
|
|
|
|
23
|
return not not $value; |
66
|
|
|
|
|
|
|
} elsif ( defined $value ) { |
67
|
17
|
100
|
|
|
|
31
|
if ( $args{num} ) { |
|
|
100
|
|
|
|
|
|
68
|
5
|
|
|
|
|
20
|
require Scalar::Util; |
69
|
5
|
100
|
|
|
|
223
|
croak "'$value' does not look like a number" unless Scalar::Util::looks_like_number($value); |
70
|
4
|
|
|
|
|
16
|
return 0+$value; |
71
|
|
|
|
|
|
|
} elsif ( $args{int} ) { |
72
|
7
|
100
|
|
|
|
195
|
croak "'$value' does not look like an integer" unless $value =~ /^\s* -? \d+ \s*$/x; |
73
|
6
|
|
|
|
|
23
|
return int 0+$value; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
6
|
|
|
|
|
16
|
return $value; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub var_name { |
81
|
46
|
|
|
46
|
0
|
72
|
my ( $class, %args ) = @_; |
82
|
|
|
|
|
|
|
|
83
|
46
|
|
|
|
|
181
|
join "::", @args{qw(package name)}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub get_var { |
87
|
26
|
|
|
26
|
0
|
40
|
my ( $class, %args ) = @_; |
88
|
|
|
|
|
|
|
|
89
|
5
|
|
|
5
|
|
21
|
no strict 'refs'; |
|
5
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
477
|
|
90
|
26
|
|
|
|
|
18
|
return ${ $class->var_name(%args) } |
|
26
|
|
|
|
|
42
|
|
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub get_env_var { |
94
|
20
|
|
|
20
|
0
|
30
|
my ( $class, %args ) = @_; |
95
|
|
|
|
|
|
|
|
96
|
20
|
|
|
|
|
32
|
my $name = uc $class->var_name(%args); |
97
|
20
|
|
|
|
|
52
|
$name =~ s/^MAIN:://; |
98
|
20
|
|
|
|
|
26
|
$name =~ s/::/_/g; |
99
|
|
|
|
|
|
|
|
100
|
20
|
|
|
|
|
45
|
$ENV{$name}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# ex: set sw=4 et: |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__PACKAGE__ |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
__END__ |