| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $value and $args{"env"}
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"}
| |||||||||||||||||||||||||||||
| 57 | 20 | $value = $class->_get_env_var(%args); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 26 | ▶ | if ( not defined $value and defined $args{default} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value and defined $args{"default"}
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"}
| |||||||||||||||||||||||||||||
| 61 | 4 | $value = $args{default}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 26 | ▶ | if ( $args{bool} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 65 | 8 | return not not $value; | |||||||||||||||||||||||||||
| 66 | } elsif ( defined $value ) { | ||||||||||||||||||||||||||||
| 67 | 17 | ▶ | if ( $args{num} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||