File Coverage

blib/lib/constant/more.pm
Criterion Covered Total %
statement 68 71 95.7
branch 20 30 66.6
condition 7 11 63.6
subroutine 7 8 87.5
pod n/a
total 102 120 85.0


line stmt bran cond sub pod time code
1             package constant::more;
2 5     5   118655 use strict;
  5         13  
  5         218  
3 5     5   56 use warnings;
  5         13  
  5         419  
4              
5             our $VERSION="v0.3.1";
6              
7 5     5   30 no warnings "experimental";
  5         12  
  5         1672  
8              
9             our %seen;
10              
11             sub import {
12              
13 12     12   753 my $package =shift;
14 12 100       86 return unless @_;
15              
16 11         23 my $flags;
17 11 100       39 if(ref($_[0]) eq "HASH"){
18             # Hash ref of keys and values
19 6         13 $flags=shift;
20             }
21             else{
22             # Flat list
23 5 100       16 if($_[0] =~/=/){
24             # Enumerations.
25 3         5 my $c=0;
26 3         4 my @out;
27              
28 3         6 for(@_){
29 8         16 my @f=split /=/;
30 8 100       23 if(@f == 2){
31             #update c to desired value
32 4         6 $c=$f[1];
33             }
34 8         24 push @out, $f[0], $c++;
35             }
36 3         14 $flags={@out};
37             }
38             else {
39             # Normal
40 2 50       7 die "constant::more note enough items" if @_ % 2;
41 2         11 $flags={@_};
42             }
43             }
44            
45 11   50     32 my $level=$Exporter::ExportLevel//0;
46 11         80 my $caller=(caller($level))[0];
47 5     5   39 no strict "refs";
  5         16  
  5         3830  
48 11         44 my %table;
49              
50 11         35 for my $name (keys %$flags){
51 19         42 my $entry;
52             my $value;
53 19         0 my @values;
54              
55              
56 19 100       45 if(ref($flags->{$name}) eq "HASH"){
57             #Full declaration
58 6         16 $entry=$flags->{$name};
59             }
60             else {
61             #assumed a short cut, just name and value
62 13         37 $entry={val=>$flags->{$name}, keep=>undef, opt=>undef, env=>undef};
63             }
64              
65             #Default sub is to return the key value pair
66             my $sub=$entry->{sub}//= sub {
67             #return name value pair
68 19     19   47 $name, $_[1];
69 19   66     93 };
70              
71             #Set the entry by name
72 19         32 $flags->{$name}=$entry;
73              
74 19         27 my $success;
75             my $wrapper= sub {
76 25     25   3158 my ($opt_name, $opt_value)=@_;
77              
78 25 50       66 return unless @_>=2;
79              
80 25         49 my @results=&$sub;
81              
82              
83             #set values in the table
84 25         112 my $i=0;
85 25         86 while($i<@results){
86 25         61 my $pair =[$results[$i++], $results[$i++]];
87 25         46 my $value=$pair->[1];
88 25         51 my $name=$pair->[0];
89 25 50       77 unless($name=~/::/){
90 25         61 $name=$caller."::".$name;
91             }
92             #Only configure contant for addition if it doesn't exist
93             #in target namespace
94 25         190 $table{$name}=$value unless(*{$name}{CODE})
95 25 50       38 }
96              
97 25         88 $success=1;
98              
99 19         77 };
100              
101              
102             #Select a value
103 19         97 $wrapper->("", $entry->{val}); #default
104            
105              
106             #CMD line argument override
107            
108 19 100 100     69 if($entry->{opt} and @ARGV){
109 4         3736 require Getopt::Long;
110 4 100       75285 if($entry->{keep}){
111 1         12 my $parser=Getopt::Long::Parser->new();
112            
113 1         1576 my @array=@ARGV; #copy
114 1 50       6 $parser->getoptionsfromarray(\@array, $entry->{opt}, $wrapper) or die "Invalid options";
115             }
116             else{
117 3         25 my $parser=Getopt::Long::Parser->new(
118             config=>[
119             "pass_through"
120             ]
121             );
122 3 50       3319 $parser->getoptions( $entry->{opt}, $wrapper) or die "Invalid options";
123             }
124             }
125              
126 19 0 33     310 if(!$success and $entry->{env}){
127             #Env override
128 0 0       0 if(defined $ENV{$entry->{env}}){
129 0         0 $wrapper->($ENV{$entry->{env}});
130             }
131             }
132             }
133              
134             #Actually
135             #Create the constants
136 11         108 while(my($name,$val)=each %table){
137 23     0   7300 *{$name}=sub (){$val}
  0            
138 23         163 }
139             }
140              
141             1;