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