persona.pm

filter: Statementstmt 74.7% 83 / 111 Branchbran 56.1% 46 / 82 Conditioncond 36.8% 7 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 62.3% 144 / 231 CC 50 SCAR 36.8
CC
50
Cov
64%
import
66.6
path2source
26.6
_inc_handler
15.4
CRAP 39.6
↑ Summary
1 package persona;
2  
3 $VERSION= '0.16';
4  
5 # be as strict and verbose as possible
6 use strict;
7 use warnings;
8  
9 # modules that we need
10 use List::Util qw( first );
11  
12 # persona and semaphore to indicate @INC watcher is installed for this process
13 my $process_persona;
14  
15 # regular expression to check
16 my $all;
17 my @only_for;
18  
19 # are we debugging?
20 BEGIN {
21 my $debug= $ENV{DEBUG} || 0;
22 $debug= 0 if $debug !~ m#^[0-9]+$#; ## only numeric constants
23 eval "sub DEBUG () { $debug }";
24 } #BEGIN
25  
26 # log pipe if we're debugging
27 BEGIN {
28 *TELL= DEBUG
29 ? sub {
30 my $format = shift() . "\n";
31 printf STDERR $format, @_;
32 }
33 4 ▶ : sub { };
Subroutine __ANON__: called CC=1 SCAR=0.0
34 } #BEGIN
35  
36 # satisfy -require-
37 1;
38  
39 #-------------------------------------------------------------------------------
40 #
41 # Class methods
42 #
43 #-------------------------------------------------------------------------------
44 # path2source
45 #
46 # Convert a given handle to source code for a given persona
47 #
48 # IN: 1 class (ignored)
49 # 2 path
50 # 3 persona to parse for (default: current)
51 # OUT: 1 scalar reference to source code or undef
52 # 2 number of lines skipped (optional)
53  
54 sub path2source {
55 4 ▶ my ( undef, $path, $persona )= @_;
Subroutine path2source: called CC=21 SCAR=32.8
Pod documented
56 4 ▶ $persona= $process_persona if !defined $persona;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $persona 0 4 4
57  
58 # could not open file, let the outside handle this
59 4 ▶ open( my $handle, '<', $path ) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $path or return 1 0 1
60 4 TELL 'Parsing %s', $path if DEBUG;
61  
62 # no persona, so we don't need to do any parsing at all
63 4 ▶ if ( !$persona ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($persona) 1 0 1
64  
65 # enable slurp mode
66 4 local $/;
67 4 my $source= readline $handle;
68  
69 # we're done
70 4 ▶ return wantarray ? ( \$source, 0 ) : \$source;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
71 }
72  
73 # initializations
74 4 my $done;
75 4 my $skipped= 0;
76 4 my $active= 1;
77 4 my $line_nr= 0;
78 4 my $source= '';
79  
80 # until we reach end of file
81 LINE:
82 4 while (1) {
83 4 my $line= readline $handle;
84 4 ▶ last LINE if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
85  
86 # seen __END__ or __DATA__, no further looking needed here
87 4 ▶ if ($done) {
Branch: 2 branches
BranchTrueFalseTotal
if ($done) { } 1 20 21
elsif ($line =~ /^__(?:DATA|END)__$/) { } 4 16 20
88 4 $source .= $line;
89 4 next LINE;
90 }
91  
92 # reached the end of logical code, continue without further looking
93 elsif ( $line =~ m#^__(?:DATA|END)__$# ) { ## syn hilite
94 0 $done= 1;
95 0 $source .= $line;
96 1 next LINE;
97 }
98  
99 # we've seen a line and want to remember that
100 1 $line_nr++;
101  
102 # code for new persona?
103 1 ▶ if ( $line =~ m/^#PERSONA\s*(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#PERSONA\s*(.*)/) 1 15 16
104 1 my $rest= $1;
105  
106 # all personas
107 1 ▶ if ( !$rest ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rest) { } 4 11 15
108  
109 # switching from inactive persona to all
110 0 ▶ if ( !$active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($active) 1 3 4
111 0 $active= 1;
112  
113 # make sure errors / stack traces have right line info
114 0 $source .= sprintf "#line %d %s (all personas)\n",
115 $line_nr + 1, $path;
116  
117 # don't bother adding the line with #PERSONA
118 1 next LINE;
119 }
120 }
121  
122 # we have an expression
123 else {
124  
125 # huh?
126 1 ▶ die "Found illegal characters in PERSONA specification:\n$rest"
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /[^\w\s\(\)\|!]/ 0 1 1
127 if $rest =~ m#[^\w\s\(\)\|!]#;
128  
129 # change simple list into expression
130 1 ▶ 1 while $rest =~ s#(?<!\!)(\w+)\s+(\w+)#$1 || $2#;
Branch: 1 branch
BranchTrueFalseTotal
$rest =~ s/(?<!\!)(\w+)\s+(\w+)/$1 || $2/ and do { '???' } 0 3 3
131  
132 # create evallable expression
133 1 my %value= ( $persona => 1 );
134 1 ▶ $rest =~ s#(\w+)# $value{$1} || 0 #ge;
Condition50%1/2
$value{$1} || 0
l!l
04
MC/DC0%0/1
$value{$1} || 0
$value{$1}
Truth table50%1/2
A
A= $value{$1}
Aresult
11
00
135  
136 # evaluate expression
137 21 my $ok= eval $rest;
138 21 ▶ die "Error in evaluation persona specification:\n'$rest'\n$@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
139 if $@;
140  
141 # stop copying code for now
142 20 ▶ if ( !$ok ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ok) { } 0 3 3
elsif (not $active) { } 2 1 3
143 4 $active= undef;
144 }
145  
146 # switching from inactive persona to all
147 elsif ( !$active ) {
148 4 $active= 1;
149  
150 # make sure errors / stack traces have right line info
151 1 $source .=
152 sprintf "#line %d %s (allowed by persona '%s')\n",
153 $line_nr + 1, $path, $persona;
154  
155 # don't bother adding the line with #PERSONA
156 1 next LINE;
157 }
158 }
159 }
160  
161 # we're not doing this line
162 1 ▶ $skipped++, next LINE if !$active;
Branch: 1 branch
BranchTrueFalseTotal
unless $active 1 0 1
163  
164 # new package, make sure it knows about PERSONA if it doesn't yet
165 15 ▶ if ( $line =~ m#^\s*package\s+([\w:]+)\s*;# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*package\s+([\w:]+)\s*;/) 4 10 14
166 no strict 'refs';
167 15 my $sub= $1 . '::PERSONA';
168 4 ▶ *{$sub}= \&main::PERSONA if !exists &$sub;
Branch: 1 branch
BranchTrueFalseTotal
unless exists &$sub 0 10 10
169 }
170  
171 # we'll do this line
172 1 $source .= $line;
173 }
174 # show source if *really* debugging
175 0 TELL $source if DEBUG > 2;
176  
177 0 ▶ return wantarray ? ( \$source, $skipped ) : \$source;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
178 } #path2source
179  
180 #-------------------------------------------------------------------------------
181 #
182 # Standard Perl features
183 #
184 #-------------------------------------------------------------------------------
185 # import
186 #
187 # IN: 1 class (ignored)
188 # 2 .. N attributes
189  
190 sub import {
191 0 ▶ my ( undef, @attr )= @_;
Subroutine import: called CC=23 SCAR=42.0
192  
193 # assume we want to set persona if only one parameter
194 3 ▶ unshift @attr, 'persona' if @attr == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @attr == 1 1 0 1
195  
196 # fetch name's name
197 3 ▶ my $name= $ENV{ENV_PERSONA} || 'PERSONA';
Condition50%1/2
$ENV{'ENV_PERSONA'} || 'PERSONA'
l!l
04
MC/DC0%0/1
$ENV{'ENV_PERSONA'} || 'PERSONA'
$ENV{'ENV_PERSONA'}
Truth table50%1/2
A
A= $ENV{'ENV_PERSONA'}
Aresult
11
00
198  
199 # fetch parameters we know
200 3 my @only_for_new;
201 my @huh;
202 3 ▶ while ( my ( $key, $value ) = splice @attr, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@attr, 0, 2)) 0 5 5
203  
204 # setting module specification
205 4 ▶ if ( $key eq 'only_for' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'only_for') { } 0 5 5
elsif ($key eq 'persona') { } 0 0 0
206 3 push @only_for_new, $value;
207 }
208  
209 # setting persona
210 elsif ( $key eq 'persona' ) {
211  
212 # huh?
213 3 ▶ die "Already have '$process_persona' as persona, "
Branch: 1 branch
BranchTrueFalseTotal
if defined $process_persona and $value ne $process_persona 0 0 0
Condition33%1/3
defined $process_persona and $value ne $process_persona
!ll&&!rl&&r
050
MC/DC0%0/2
defined $process_persona and $value ne $process_persona
defined $process_persona $value ne $process_persona
Truth table0%0/3
A and B
A= defined $process_personaB= $value ne $process_persona
ABresult
0X0
100
111
214 . "cannot specify '$value' now"
215 if defined $process_persona and $value ne $process_persona;
216  
217 3 $ENV{$name}= $value;
218 }
219  
220 # don't know what to do with this
221 else {
222 2 push @huh, $key;
223 }
224 }
225  
226 # find persona we need to work for
227 1 ▶ if ( !defined $process_persona ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $process_persona) 0 0 0
228 1 $process_persona= $ENV{$name};
229  
230 # too bad, we don't have a persona
231 1 ▶ $process_persona= '' if !defined $process_persona;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $process_persona 4 1 5
232  
233 # force some sanity
234 14 ▶ die "Persona may only contain alphanumeric characters,"
Branch: 1 branch
BranchTrueFalseTotal
if $process_persona =~ s/\W//gs 3 1 4
235 . " e.g. '$process_persona'"
236 if $process_persona =~ s#\W##sg;
237  
238 # create constant in main (for easy access later)
239 10 ▶ die $@ if !eval "sub main::PERSONA () { '$process_persona' }; 1";
Branch: 1 branch
BranchTrueFalseTotal
unless eval "sub main::PERSONA () { '${process_persona}' }; 1" 0 4 4
240  
241 # we have a persona, great!
242 0 ▶ if ($process_persona) {
Branch: 1 branch
BranchTrueFalseTotal
if ($process_persona) 0 4 4
243  
244 # install handler if we have a persona
245 0 unshift @INC, \&_inc_handler;
246  
247 # we're being called in a script
248 0 ▶ if ( !( ()= caller(3) ) # only 3 levels below us
Branch: 1 branch
BranchTrueFalseTotal
if (not +() = caller 3 and (caller 0)[1] ne "-e" and (caller 2)[3] eq "(eval)") 1 3 4
Condition16%1/6
not +() = caller 3 and (caller 0)[1] ne "-e" and (caller 2)[3] eq "(eval)"
!ll&&!rl&&r
000
not +() = caller 3 and (caller 0)[1] ne "-e"
!ll&&!rl&&r
001
MC/DC0%0/3
not +() = caller 3 and (caller 0)[1] ne "-e" and (caller 2)[3] eq "(eval)"
not +() = caller 3 (caller 0)[1] ne "-e" (caller 2)[3] eq "(eval)"
Truth table0%0/4
not(A) and B and C
A= not +() = caller 3B= (caller 0)[1] ne "-e"C= (caller 2)[3] eq "(eval)"
ABCresult
0XX0
10X0
1100
1111
249 and ( (caller(0))[1] ne '-e' ) # not a one liner
250 and ( (caller(2))[3] eq '(eval)' # but an eval at lowest level
251 ) ) {
252  
253 # make sure we will process this file
254 10 unshift @only_for, qr#^$0$#; ## syn hilite
255  
256 # do the script, but through the @INC handler
257 1 TELL 'Recursively calling script for "%s"', $process_persona
258 if DEBUG;
259 1 do $0;
260  
261 # we're done, nothing left to do at this level
262 5 exit;
263 }
264  
265 5 TELL 'Interpreting source code as "%s"', $process_persona if DEBUG;
266 }
267 }
268  
269 # extra parameters
270 5 ▶ die "Don't know what to do with @{[ sort @huh ]}" if @huh;
Branch: 1 branch
BranchTrueFalseTotal
if @huh 1 0 1
271  
272 # we have some kind of specification which modules to check
273 5 ▶ if (@only_for_new) {
Branch: 1 branch
BranchTrueFalseTotal
if (@only_for_new) 0 4 4
274  
275 # normalize all new settings
276 ONLY_FOR:
277 0 foreach my $only_for (@only_for_new) {
278  
279 # need to check what we have
280 0 ▶ if ( my $ref= ref $only_for ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $ref = ref $only_for) { } 0 4 4
elsif ($only_for eq '*') { } 0 0 0
281 0 ▶ die "Can only handle references of type '$ref'"
Branch: 1 branch
BranchTrueFalseTotal
if $ref ne "Regexp" 0 0 0
282 if $ref ne 'Regexp';
283
284 # it's ok
285 0 push @only_for, $only_for;
286 }
287  
288 # do all and everything
289 elsif ( $only_for eq '*' ) {
290 0 $all= 1;
291 5 @only_for= @only_for_new= ();
292 4 TELL "Look for personas in all files" if DEBUG;
293 4 last ONLY_FOR;
294 }
295
296 # just a string, make it a regexp
297 else {
298 4 push @only_for, qr#^$only_for#;
299 }
300 }
301  
302 4 TELL "Added regular expression for matching file names:\n %s",
303 join( "\n ", @only_for[ -@only_for_new .. $#only_for ] ),
304 if DEBUG and @only_for_new;
305 }
306  
307 # export constant to the caller if not done so already
308 no strict 'refs';
309 4 my $sub= caller() . '::PERSONA';
310 1 ▶ *{$sub}= \&main::PERSONA if !exists &$sub;
Branch: 1 branch
BranchTrueFalseTotal
unless exists &$sub 0 0 0
311  
312 1 return;
313 } #import
314  
315 #-------------------------------------------------------------------------------
316 #
317 # Internal subroutines
318 #
319 #-------------------------------------------------------------------------------
320 # _inc_handler
321 #
322 # IN: 1 code reference to this sub
323 # 2 file to look for
324 # OUT: 1 handle to read source from
325  
326 sub _inc_handler {
327 1 ▶ my ( $self, $file )= @_;
Subroutine _inc_handler: called CC=8 SCAR=27.3
328  
329 # shouldn't handle this file, let require handle it (again)
330 1 ▶ if ( !$all and !first { $file =~ m#$_# } @only_for ) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless ($all or &first(sub { $file =~ /$_/; } , @only_for)) 2 2 4
Condition33%1/3
$all or &first(sub { $file =~ /$_/; } , @only_for)
l!l&&r!l&&!r
001
MC/DC0%0/2
$all or &first(sub { $file =~ /$_/; } , @only_for)
$all &first(sub { $file =~ /$_/; } , @only_for)
Truth table33%1/3
A or B
A= $allB= &first(sub { $file =~ /$_/; } , @only_for)
ABresult
1X1
011
000
331 1 TELL 'Not handling %s', $file if DEBUG > 1;
332 0 return undef;
333 }
334  
335 # can't find ourselves?
336 4 ▶ my $first= first { $INC[$_] eq $self } 0 .. $#INC;
Subroutine __ANON__: called CC=1 SCAR=0.0
337 0 ▶ die "Could not find INC handler in @INC" if !defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 22 1 23
338  
339 # could not find file, let require handle it (again)
340 0 ▶ my $path= first { -e } map { "$INC[$_]/$file" } $first + 1 .. $#INC;
Subroutine __ANON__: called CC=1 SCAR=0.4
341 0 ▶ if ( !$path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 1 1
342 0 TELL 'Could not find %s', $file if DEBUG > 1;
343 0 return undef;
344 }
345  
346 # parse the source
347 0 my ( $source, $skipped )= __PACKAGE__->path2source($path);
348  
349 # could not open file, or nothing skipped, let -require- handle it
350 0 ▶ return undef if !$source or !$skipped;
Branch: 1 branch
BranchTrueFalseTotal
unless $source and $skipped 0 1 1
Condition66%2/3
$source and $skipped
!ll&&!rl&&r
0122
MC/DC0%0/2
$source and $skipped
$source $skipped
Truth table0%0/3
A and B
A= $sourceB= $skipped
ABresult
0X0
100
111
351  
352 # set %INC correctly
353 0 $path =~ s#^\./##; # normalize just as perl does
354 0 $INC{$file}=
355 "$path (skipped $skipped lines for persona '$process_persona')";
356  
357 # make sure that __FILE__ will be correct as well
358 0 $$source= "#line 1 $path\n$$source";
359  
360 # convert source to handle, so require can handle it
361 0 ▶ open( my $require, '<', $source )
Branch: 1 branch
BranchTrueFalseTotal
open my $require, "<", $source or die "Could not open in-memory source for reading: $!" 0 1 1
362 or die "Could not open in-memory source for reading: $!";
363  
364 4 return $require;
365 } #_inc_handler
366  
367 #-------------------------------------------------------------------------------
368  
369 __END__
↑ Summary