blib/lib/Devel/Hide.pm

filter: Statementstmt 96.2% 103 / 107 Branchbran 89.5% 43 / 48 Conditioncond 91.6% 11 / 12 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 95.6% 22 / 23 Podpod n/a totaltotal 93.9% 186 / 198 CC 29 SCAR 15.8
CC
29
Cov
94%
import
10.3
_push_hidden
8.0
_get_config_ref
6.0
CRAP 4.8
↑ Summary
1 package Devel::Hide;
2  
3 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 our $VERSION = '0.0016';
8  
9 # blech! package variables
10 #
11 # @HIDDEN is one of the ways to populate the global hidden list
12 # $phase is used to identify which version of the hints hash to
13 # use - either %^H when we're updating it, or pulling it out
14 # of caller() when we want to read it
15 12 our @HIDDEN;
16 12 our $phase;
17  
18 12 ▶BEGIN { $phase = 'runtime'; }
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # settings are a comma- (and only comma, no quotes or spaces)
21 # -separated list of key,value,key,value,... There is no
22 # attempt to support data containing commas.
23 #
24 # The list of hidden modules is a comma (and *only* comma,
25 # no white space, no quotes) separated list of module
26 # names.
27 #
28 # yes, this is a ridiculous way of storing data. It is,
29 # however, compatible with what we're going to have to
30 # store in the hints hash for lexical hiding, as that
31 # only supports string data.
32 12 my %GLOBAL_SETTINGS;
33 12 _set_setting('global', children => 0);
34 _set_setting('global', verbose =>
35 defined $ENV{DEVEL_HIDE_VERBOSE}
36 ? $ENV{DEVEL_HIDE_VERBOSE}
37 12 ▶ : 1
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'DEVEL_HIDE_VERBOSE'} ? : 0 12 12
38 );
39  
40 # convert a mixed list of modules and filenames to a list of
41 # filenames
42 12 ▶sub _as_filenames {
Subroutine _as_filenames: called CC=2 SCAR=6.9
43 12 return map { /^(\w+::)*\w+$/
44 19 ▶ ? do { my $f = "$_.pm"; $f =~ s|::|/|g; $f }
Branch: 1 branch
BranchTrueFalseTotal
/^(\w+::)*\w+$/ ? : 13 6 19
45 : $_
46 } @_;
47 }
48  
49 # Pushes a list to the set of hidden modules/filenames
50 # warns about the modules which could not be hidden (always)
51 # and about the ones that were successfully hidden (if verbose)
52 #
53 # It works as a batch producing warning messages
54 # at each invocation (when appropriate).
55 #
56 # the first arg is a reference to the config hash to use,
57 # either global or lexical
58 22 ▶sub _push_hidden {
Subroutine _push_hidden: called CC=8 SCAR=20.8
59 22 my $config = shift;
60  
61 22 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 10 12 22
62  
63 12 my @too_late;
64 12 for ( _as_filenames(@_) ) {
65 19 ▶ if ( $INC{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{$_}) { } 2 17 19
66 2 push @too_late, $_;
67 }
68 else {
69 $config->{'Devel::Hide/hidden'} =
70 $config->{'Devel::Hide/hidden'}
71 17 ▶ ? join(',', $config->{'Devel::Hide/hidden'}, $_)
Branch: 1 branch
BranchTrueFalseTotal
$config->{'Devel::Hide/hidden'} ? : 6 11 17
72 : $_;
73 }
74 }
75 12 ▶ if ( @too_late ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@too_late) 2 10 12
76 2 warn __PACKAGE__, ': Too late to hide ', join( ', ', @too_late ), "\n";
77 }
78 12 ▶ if ( _get_setting('verbose') && $config->{'Devel::Hide/hidden'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_setting("verbose") and $config->{"Devel::Hide/hidden"}) 4 8 12
Condition66%2/3
_get_setting("verbose") and $config->{"Devel::Hide/hidden"}
!ll&&!rl&&r
804
MC/DC50%1/2
_get_setting("verbose") and $config->{"Devel::Hide/hidden"}
_get_setting("verbose") $config->{"Devel::Hide/hidden"}
Truth table66%2/3
A and B
A= _get_setting("verbose")B= $config->{"Devel::Hide/hidden"}
ABresult
0X0
100
111
79 12 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 warn __PACKAGE__ . ' hides ' .
81 join(
82 ', ',
83 sort split(
84 4 /,/, $config->{'Devel::Hide/hidden'}
85 )
86 ) . "\n";
87 }
88 }
89  
90 18 ▶sub _dont_load {
Subroutine _dont_load: called CC=2 SCAR=6.9
91 18 my $filename = shift;
92 18 ▶ my $hidden_by = _get_setting('verbose')
Branch: 1 branch
BranchTrueFalseTotal
_get_setting('verbose') ? : 12 6 18
93 ? 'hidden'
94 : 'hidden by ' . __PACKAGE__;
95 18 die "Can't locate $filename in \@INC ($hidden_by)\n";
96 }
97  
98 =begin private
99  
100 =item B<_core_modules>
101  
102 @core = _core_modules($perl_version);
103  
104 Returns the list of core modules according to
105 Module::CoreList.
106  
107 !!! UNUSED BY NOW
108  
109 It is aimed to expand the tag ':core' into all core
110 modules in the current version of Perl ($]).
111 Requires Module::CoreList.
112  
113 =end private
114  
115 =cut
116  
117 0 ▶sub _core_modules {
Subroutine _core_modules: not called CC=1 SCAR=6.9
118 0 require Module::CoreList; # XXX require 2.05 or newer
119 0 return Module::CoreList->find_modules( qr/.*/, shift );
120 }
121  
122 # _append_to_perl5opt(@to_be_hidden)
123 1 ▶sub _append_to_perl5opt {
Subroutine _append_to_perl5opt: called CC=2 SCAR=7.6
124  
125 $ENV{PERL5OPT} = join( ' ',
126 1 ▶ defined($ENV{PERL5OPT}) ? $ENV{PERL5OPT} : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PERL5OPT'} ? : 1 0 1
127 '-MDevel::Hide=' . join(',', @_)
128 );
129  
130 }
131  
132 179 ▶sub _is_hidden {
Subroutine _is_hidden: called CC=1 SCAR=0.0
133 12 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 179 my $module = shift;
135  
136 +{
137 379 map { $_ => 1 }
138 map {
139 358 split(',', _get_config_ref($_)->{'Devel::Hide/hidden'})
140 } qw(global lexical)
141 179 }->{$module};
142 }
143  
144 41 ▶sub _get_setting {
Subroutine _get_setting: called CC=2 SCAR=6.9
145 41 my $name = shift;
146 41 ▶ _exists_setting('lexical', $name)
Branch: 1 branch
BranchTrueFalseTotal
_exists_setting('lexical', $name) ? : 3 38 41
147 ? _get_setting_from('lexical', $name)
148 : _get_setting_from('global', $name)
149 }
150  
151 41 ▶sub _get_setting_from {
Subroutine _get_setting_from: called CC=1 SCAR=0.0
152 41 my($source, $name) = @_;
153  
154 41 my $config = _get_config_ref($source);
155 41 _setting_hashref($config)->{$name};
156 }
157  
158 41 ▶sub _exists_setting {
Subroutine _exists_setting: called CC=1 SCAR=0.0
159 41 my($source, $name) = @_;
160
161 41 my $config = _get_config_ref($source);
162 41 exists(_setting_hashref($config)->{$name});
163 }
164  
165 30 ▶sub _set_setting {
Subroutine _set_setting: called CC=1 SCAR=0.0
166 30 my($source, $name, $value) = @_;
167  
168 30 my $config = _get_config_ref($source);
169 my %hash = (
170 30 %{_setting_hashref($config)},
171 $name => $value
172 );
173 _get_config_ref($source)
174 30 ->{'Devel::Hide/settings'} = join(',', %hash);
175 }
176  
177 112 ▶sub _setting_hashref {
Subroutine _setting_hashref: called CC=1 SCAR=0.0
178 112 my $settings = shift->{'Devel::Hide/settings'};
179 12 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
180 112 +{ split(/,/, $settings) };
181 }
182  
183 522 ▶sub _get_config_ref {
Subroutine _get_config_ref: called CC=6 SCAR=17.9
184 522 my $type = shift;
185 522 ▶ if($type eq 'lexical') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'lexical') { } 227 295 522
186 227 ▶ if($phase eq 'compile') {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::Hide::phase eq 'compile') { } 26 201 227
187 26 return \%^H;
188 } else {
189 201 my $depth = 1;
190 201 ▶ while(my @fields = caller($depth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@fields) = caller $depth) 2805 193 2998
191 2805 my $hints_hash = $fields[10];
192 2805 ▶ if($hints_hash && grep { /^Devel::Hide\// } keys %{$hints_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hints_hash and grep {m[^Devel::Hide/];} keys %{$hints_hash;}) 8 2797 2805
Condition100%3/3
$hints_hash and grep {m[^Devel::Hide/];} keys %{$hints_hash;}
!ll&&!rl&&r
23524458
MC/DC100%2/2
$hints_hash and grep {m[^Devel::Hide/];} keys %{$hints_hash;}
$hints_hash grep {m[^Devel::Hide/];} keys %{$hints_hash;}
Truth table100%3/3
A and B
A= $hints_hashB= grep {m[^Devel::Hide/];} keys %{$hints_hash;}
ABresult
0X0
100
111
193 # return a copy
194 8 return { %{$hints_hash} };
195 }
196 2797 $depth++;
197 }
198 193 return {};
199 }
200 } else {
201 295 return \%GLOBAL_SETTINGS;
202 }
203 }
204  
205 14 ▶sub import {
Subroutine import: called CC=10 SCAR=23.3
206 14 shift;
207 14 my $which_config = 'global';
208 14 local $phase = 'compile';
209 14 ▶ while(@_ && $_[0] =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] =~ /^-/) 8 14 22
Condition100%3/3
@_ and $_[0] =~ /^-/
!ll&&!rl&&r
4108
MC/DC100%2/2
@_ and $_[0] =~ /^-/
@_ $_[0] =~ /^-/
Truth table100%3/3
A and B
A= @_B= $_[0] =~ /^-/
ABresult
0X0
100
111
210 8 ▶ if( $_[0] eq '-lexically' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0] eq '-lexically') { } 2 6 8
elsif ($_[0] eq '-from:children') { } 1 5 6
elsif ($_[0] eq '-quiet') { } 5 0 5
211 2 $which_config = 'lexical';
212 2 ▶ if($] < 5.010) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 2 2
213 0 die("Can't 'use Devel::Hide qw(-lexically ...)' on perl 5.8 and below\n");
214 }
215 } elsif( $_[0] eq '-from:children' ) {
216 1 _set_setting($which_config, children => 1);
217 } elsif( $_[0] eq '-quiet' ) {
218 5 _set_setting($which_config, verbose => 0);
219 } else {
220 0 die("Devel::Hide: don't recognize $_[0]\n");
221 }
222 8 shift;
223 }
224 14 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 4 14
225 10 _push_hidden(
226 _get_config_ref($which_config),
227 @_
228 );
229 10 ▶ if (_get_setting('children')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_get_setting("children")) 1 9 10
230 1 ▶ _append_to_perl5opt(
Branch: 1 branch
BranchTrueFalseTotal
_get_setting('verbose') ? : 0 1 1
231 (_get_setting('verbose') ? () : '-quiet'),
232 @_
233 );
234 }
235 }
236 }
237  
238 # $ENV{DEVEL_HIDE_PM} is split in ' '
239 # as well as @HIDDEN it accepts Module::Module as well as File/Names.pm
240 12 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
241 # unless @HIDDEN was user-defined elsewhere, set default
242 12 ▶ if ( !@HIDDEN && $ENV{DEVEL_HIDE_PM} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @Devel::Hide::HIDDEN and $ENV{'DEVEL_HIDE_PM'}) { } 1 11 12
Condition100%3/3
not @Devel::Hide::HIDDEN and $ENV{"DEVEL_HIDE_PM"}
!ll&&!rl&&r
1101
MC/DC100%2/2
not @Devel::Hide::HIDDEN and $ENV{"DEVEL_HIDE_PM"}
not @Devel::Hide::HIDDEN $ENV{"DEVEL_HIDE_PM"}
Truth table100%3/3
not(A) and B
A= not @Devel::Hide::HIDDENB= $ENV{"DEVEL_HIDE_PM"}
ABresult
0X0
100
111
243 # NOTE. "split ' ', $s" is special. Read "perldoc -f split".
244 _push_hidden(
245 _get_config_ref('global'),
246 split q{ }, $ENV{DEVEL_HIDE_PM}
247 1 );
248 }
249 else {
250 11 _push_hidden(
251 _get_config_ref('global'),
252 @HIDDEN
253 );
254 }
255 }
256  
257 179 ▶sub _inc_hook {
Subroutine _inc_hook: called CC=2 SCAR=6.9
258 179 my ( $coderef, $filename ) = @_;
259 179 ▶ if ( _is_hidden($filename) ) { _dont_load($filename); }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_hidden($filename)) { } 18 161 179
260 161 else { return undef; }
261 }
262  
263 12 ▶use lib ( \&_inc_hook );
Subroutine BEGIN: called CC=1 SCAR=0.0
264  
265 # TO DO:
266 # * write unimport() sub
267 # * write decent docs
268 # * refactor private function names
269 # * RT #25528
270  
271 =begin private
272  
273 perl -MDevel::Hide=!:core -e script.pl # hide all non-core modules
274 perl -MDevel::Hide=M,!N -e script.pl # hide all modules but N plus M
275  
276 how to implement
277  
278 %GLOBAL_SETTINGS
279 %IS_EXCEPTION if there is an exception, all but the set of exceptions are to be hidden
280 plus the set of hidden modules
281  
282 :core(5.8)
283 :core synonym to :core($])
284  
285  
286 =end private
287  
288 =cut
289  
290 12 1;
291  
292 __END__
↑ Summary