blib/lib/Env/Export.pm

filter: Statementstmt 100% 93 / 93 Branchbran 100% 40 / 40 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 100% 155 / 155 CC 27 SCAR 29.4
CC
27
Cov
100%
import
27.0
BEGIN
1.0
BEGIN
1.0
CRAP 19.0
↑ Summary
1 ###############################################################################
2 #
3 # This file copyright (c) 2009-2011 by Randy J. Ray, all rights reserved
4 #
5 # Copying and distribution are permitted under the terms of the Artistic
6 # License 2.0 (http://www.opensource.org/licenses/artistic-license-2.0.php) or
7 # the GNU LGPL (http://www.opensource.org/licenses/lgpl-2.1.php).
8 #
9 ###############################################################################
10 #
11 # Description: Export environment variables as constant subs
12 #
13 # Functions: import
14 #
15 # Environment: Umm, yeah... that's kind of the point of it all...
16 #
17 ###############################################################################
18  
19 package Env::Export;
20  
21 9 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 9 ▶use subs qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 9 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 9 $VERSION = '0.22';
30 9 $VERSION = eval $VERSION; ## no critic(ProhibitStringyEval)
31  
32 ###############################################################################
33 #
34 # Sub Name: import
35 #
36 # Description: Do the actual import work, namespace wrangling, etc.
37 #
38 # Arguments: NAME IN/OUT TYPE DESCRIPTION
39 # $class in scalar Class we're called in
40 # @patterns in list One or more patterns or
41 # keywords used to select %ENV
42 # keys to export
43 #
44 # Environment: Yeah
45 #
46 # Returns: void
47 #
48 ###############################################################################
49 sub import ## no critic(ProhibitExcessComplexity)
50 58 ▶{
Subroutine import: called CC=27 SCAR=33.0
51 58 my ($class, @patterns) = @_;
52 58 my $me = "${class}::import";
53  
54 ## no critic(ProhibitNoStrict)
55 ## no critic(ProhibitProlongedStrictureOverride)
56 ## no critic(ProhibitNoWarnings)
57 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 9 ▶ no warnings qw(redefine prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 58 ▶ if (! @patterns)
Branch: 1 branch
BranchTrueFalseTotal
unless (@patterns) 1 57 58
61 {
62 1 return; # Nothing to do if they didn't request anything
63 }
64  
65 57 my ($calling_pkg) = caller;
66 57 my $callersym = \%{"${calling_pkg}::"};
67  
68 # Values that are tweaked by keywords that may appear in the @patterns
69 # stream:
70 57 my $warn = 1;
71 57 my $link = 0;
72 57 my $prefix = q{};
73 57 my $override = 0;
74 57 my $split = q{};
75  
76 # Establish the set of allowable %ENV keys that are eligible for export.
77 # This will avoid repeated iterations over %ENV later, and will remove
78 # any keys that could not be used to create valid sub names
79 57 my @choices = grep { /^[A-Za-z_]\w*$/ } keys %ENV;
80 # This list will accumulate the set of subs to be created, in the form of
81 # metadata:
82 57 my @subs = ();
83  
84 57 ▶ while (my $pat = shift @patterns)
Branch: 1 branch
BranchTrueFalseTotal
if (my $pat = shift @patterns) 98 57 155
85 {
86 # This would be a lot cleaner if I could assume the presence of the
87 # "switch" statement. But I'm not ready to limit this code to 5.10+
88  
89 # Because ":split" only applies to the very next argument after it,
90 # we have to handle it specially. It gets cleared at the end of every
91 # iteration of this loop, so if it is here, peel off the next argument
92 # then re-assign $pat to the one after that.
93 98 ▶ if ($pat eq ':split')
Branch: 1 branch
BranchTrueFalseTotal
if ($pat eq ":split") 5 93 98
94 {
95 5 $split = shift @patterns;
96 5 $pat = shift @patterns;
97 }
98  
99 # Do the keywords first, in most cases they just flip flags back and
100 # forth
101 98 ▶ if ($pat =~ /^:(no)?warn$/) ## no critic(ProhibitCascadingIfElse)
Branch: 8 branches
BranchTrueFalseTotal
if ($pat =~ /^:(no)?warn$/) { } 7 91 98
elsif ($pat =~ /^:(no)?prefix$/) { } 6 85 91
elsif ($pat =~ /^:(no)?override$/) { } 6 79 85
elsif ($pat =~ /^:(no)?link$/) { } 7 72 79
elsif ($pat eq ':all') { } 3 69 72
elsif (ref $pat eq 'Regexp') { } 3 66 69
elsif ($pat =~ /[*?]/) { } 4 62 66
elsif ($pat =~ /^[A-Za-z_]\w*$/) { } 61 1 62
102 {
103 7 ▶ $warn = $1 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 6 1 7
104 }
105 elsif ($pat =~ /^:(no)?prefix$/)
106 {
107 6 ▶ $prefix = $1 ? q{} : shift @patterns;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 1 5 6
108 }
109 elsif ($pat =~ /^:(no)?override$/)
110 {
111 6 ▶ $override = $1 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 2 4 6
112 }
113 elsif ($pat =~ /^:(no)?link$/)
114 {
115 7 ▶ $link = $1 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 2 5 7
116 }
117 elsif ($pat eq ':all')
118 {
119 3 for (@choices)
120 {
121 84 push @subs, { key => $_,
122 warn => $warn,
123 prefix => $prefix,
124 override => $override,
125 link => $link,
126 split => $split, };
127 }
128 }
129 # Now handle explicit names, shell-style patterns and regexen:
130 # Pre-compiled Perl regexen:
131 elsif (ref($pat) eq 'Regexp')
132 {
133 # Add an entry to @subs for each matching key
134 3 for (grep { $_ =~ $pat } @choices)
135 {
136 9 push @subs, { key => $_,
137 warn => $warn,
138 prefix => $prefix,
139 override => $override,
140 link => $link,
141 split => $split, };
142 }
143 }
144 # Shell style (* => .*, ? => ., ?* => .+):
145 elsif ($pat =~ /[*?]/)
146 {
147 # Change the shell-style globbing patterns to regex equivalents
148 4 $pat =~ s/[?][*]/.+/g;
149 4 $pat =~ s/[*]/.*/g;
150 4 $pat =~ s/[?]/./g;
151 4 $pat = qr/^$pat$/;
152  
153 # Add an entry to @subs for each matching key
154 4 for (grep { $_ =~ $pat } @choices)
155 {
156 288 push @subs, { key => $_,
157 warn => $warn,
158 prefix => $prefix,
159 override => $override,
160 link => $link,
161 split => $split, };
162 }
163 }
164 # Lastly, acceptable strings:
165 elsif ($pat =~ /^[A-Za-z_]\w*$/)
166 {
167 # Just add a single entry to @subs for the string
168 61 push @subs, { key => $pat,
169 warn => $warn,
170 prefix => $prefix,
171 override => $override,
172 link => $link,
173 split => $split, };
174 }
175 # And if we got here it was almost certainly a pattern that would not
176 # be a valid Perl subname. Note that this is not suppressed by :nowarn.
177 else
178 {
179 1 carp "$me: Unrecognized pattern or keyword '$pat', skipped";
180 }
181  
182 # Since :split is defined to apply to only the next name or pattern,
183 # we have to clear it every iteration just to be safe...
184 98 $split = q{};
185 }
186  
187 57 foreach (@subs)
188 {
189 442 my $subname = "$_->{prefix}$_->{key}";
190 442 my $envkey = $_->{key};
191  
192 442 ▶ if (exists($callersym->{$subname}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $callersym->{$subname} and defined &{$callersym->{$subname};} and not $_->{"override"}) 7 435 442
Condition100%6/6
exists $callersym->{$subname} and defined &{$callersym->{$subname};} and not $_->{"override"}
!ll&&!rl&&r
42967
exists $callersym->{$subname} and defined &{$callersym->{$subname};}
!ll&&!rl&&r
422713
MC/DC100%3/3
exists $callersym->{$subname} and defined &{$callersym->{$subname};} and not $_->{"override"}
exists $callersym->{$subname} defined &{$callersym->{$subname};} not $_->{"override"}
Truth table100%4/4
A and B and not(C)
A= exists $callersym->{$subname}B= defined &{$callersym->{$subname};}C= not $_->{"override"}
ABCresult
0XX0
10X0
1100
1111
193 20 defined(&{$callersym->{$subname}}) &&
194 ! $_->{override})
195 {
196 # We don't overwrite existing subroutines unless they OK'd it
197 # with :override
198 7 ▶ if ($_->{warn})
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"warn"}) 2 5 7
199 {
200 2 carp "$me: Will not redefine ${calling_pkg}::$subname, " .
201 'skipping';
202 }
203 7 next;
204 }
205  
206 435 $subname = "${calling_pkg}::$subname";
207  
208 # This may look like a great candidate for a lookup-table of code
209 # blocks to eval, but I'd actually prefer to avoid that as it would
210 # also require multiple substitutions each iteration as well...
211 435 ▶ if ($_->{link})
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'link'}) { } 8 427 435
212 {
213 8 ▶ if ($_->{split})
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'split'}) { } 1 7 8
214 {
215 1 my $localsplit = $_->{split};
216 1 ▶ *{$subname} = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
217 2 return split $localsplit, $ENV{$envkey};
218 1 };
219 }
220 else
221 {
222 7 ▶ *{$subname} = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 7 return $ENV{$envkey};
224 7 };
225 }
226 }
227 else
228 {
229 427 ▶ if ($_->{split})
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'split'}) { } 5 422 427
230 {
231 5 my @value = split $_->{split}, $ENV{$envkey};
232 5 ▶ *{$subname} = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
233 10 return @value;
234 5 };
235 }
236 else
237 {
238 422 my $value = $ENV{$envkey};
239 422 ▶ *{$subname} = sub () {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 394 return $value;
241 422 };
242 }
243 }
244 }
245  
246 57 return;
247 }
248  
249 9 1;
250  
251 __END__
↑ Summary