Genius.pm

filter: Statementstmt 90.0% 135 / 150 Branchbran 75.8% 44 / 58 Conditioncond 50.0% 11 / 22 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 92.5% 25 / 27 Podpod 5.8% 1 / 17 totaltotal 76.6% 220 / 287 CC 41 SCAR 16.1
CC
41
Cov
83%
inext
20.0
run_once
8.4
convert_pregex_to_dfa
7.2
CRAP 5.0
« Genius/Util.pm ↑ Summary x.pl »
1 package Sub::Genius;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use FLAT::PFA;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use FLAT::Regex::WithExtraOps;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 our $VERSION = q{0.314006};
12  
13 22 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
14 22 my $pkg = shift;
15 22 my %self = @_;
16 22 my $self = \%self;
17 22 bless $self, $pkg;
18 22 ▶ die qq{'preplan' parameter required!\n} if not defined $self->{preplan};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"preplan"} 0 22 22
19  
20 # set to undef to disable preprocessing
21 22 ▶ if ( not exists $self->{preprocess} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"preprocess"}) 16 6 22
22 16 $self->{preprocess} = 1;
23 }
24  
25 # set to undef to disable caching
26 22 ▶ if ( not exists $self->{cachedir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"cachedir"}) 19 3 22
27 19 $self->cachedir( sprintf( qq{%s/%s}, Cwd::cwd(), q{_Sub::Genius} ) );
28 }
29  
30 # keep a historical record
31 22 $self->original_preplan($self->preplan);
32  
33 # 'pre-process' plan - this step maximizes the chance of capturing
34 # the same checksum for identical PREs that may just be formatted differently
35 22 ▶ if ( $self->{preprocess} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"preprocess"}) 16 6 22
36 16 $self->_trim;
37 16 $self->_balkanize;
38 16 $self->_normalize;
39 }
40  
41 # generates checksum based on post-preprocessed form
42 22 $self->checksum( Digest::MD5::md5_hex( $self->preplan ) );
43  
44 22 $self->pregex( FLAT::Regex::WithExtraOps->new( $self->preplan ) );
45 22 return $self;
46 }
47  
48 68 ▶sub cachefile {
Subroutine cachefile: called CC=2 SCAR=6.9
Pod undocumented
49 68 my $self = shift;
50 68 ▶ return ( $self->cachedir ) ? sprintf( qq{%s/%s}, $self->cachedir, $self->checksum ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->cachedir ? : 67 1 68
51 }
52  
53 180 ▶sub cachedir {
Subroutine cachedir: called CC=3 SCAR=11.0
Pod undocumented
54 180 my ( $self, $dir ) = @_;
55 180 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 19 161 180
56 19 $self->{cachedir} = $dir;
57 19 ▶ if ( not -d $self->{cachedir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"cachedir"}) 1 18 19
58 1 mkdir $self->{cachedir}, 0700 || die $!;
59 }
60 }
61 180 return $self->{cachedir};
62 }
63  
64 115 ▶sub checksum {
Subroutine checksum: called CC=2 SCAR=6.9
Pod undocumented
65 115 my ( $self, $sum ) = @_;
66 115 ▶ if ($sum) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sum) 22 93 115
67 22 $self->{checksum} = $sum;
68 }
69 115 return $self->{checksum};
70 }
71  
72 25 ▶sub do_cache {
Subroutine do_cache: called CC=3 SCAR=11.4
Pod undocumented
73 25 my $self = shift;
74 25 ▶ return ( $self->cachedir and $self->checksum and $self->cachefile );
Condition66%4/6
$self->cachedir && $self->checksum && $self->cachefile
!ll&&!rl&&r
2023
$self->cachedir and $self->checksum
!ll&&!rl&&r
2023
MC/DC33%1/3
$self->cachedir && $self->checksum && $self->cachefile
$self->cachedir $self->checksum $self->cachefile
Truth table50%2/4
A and B && C
A= $self->cachedirB= $self->checksumC= $self->cachefile
ABCresult
0XX0
10X0
1100
1111
75 }
76  
77 # strips comments and empty lines
78 16 ▶sub _trim {
Subroutine _trim: called CC=3 SCAR=11.0
79 16 my $self = shift;
80 16 my $_pre = q{};
81 16 my @pre = ();
82 STRIP:
83 16 foreach my $line ( split /\n/, $self->{preplan} ) {
84 52 ▶ next STRIP if ( $line =~ m/^\s*#|^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#|^\s*$/ 7 45 52
85 45 my @line = split /\s*#/, $line;
86 45 push @pre, $line[0];
87 }
88 16 $self->preplan( join qq{\n}, @pre );
89 16 return $self->preplan;
90 }
91  
92 16 ▶sub _balkanize {
Subroutine _balkanize: called CC=3 SCAR=11.1
93 16 my $self = shift;
94 16 ▶ if ( $self->{preplan} =~ m/[#\[\]]+/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"preplan"} =~ /[#\[\]]+/) 0 16 16
95 0 die qq{plan to be bracketized must not contain '#', '[', or ']'};
96 }
97 16 my $_pre = q{};
98 16 my @pre = ();
99 STRIP:
100 16 foreach my $line ( split /\n/, $self->{preplan} ) {
101  
102 # supports strings with namespace delim, '::'
103 45 $line =~ s/([a-zA-Z:_\d]+)/\[$1\]/g;
104 45 push @pre, $line;
105 }
106 16 $self->preplan( join qq{\n}, @pre );
107 16 return $self->preplan;
108 }
109  
110 # currently, removes all spaces and newlines
111 16 ▶sub _normalize {
Subroutine _normalize: called CC=1 SCAR=0.0
112 16 my $self = shift;
113 16 my @pre = split /\n/, $self->{preplan};
114 16 my $minified = join qq{}, @pre;
115 16 $minified =~ s/[\s]+//g;
116 16 $self->preplan($minified);
117 16 return $self->preplan;
118 }
119  
120 # accessor for original plan
121 22 ▶sub original_preplan {
Subroutine original_preplan: called CC=2 SCAR=7.0
Pod undocumented
122 22 my ( $self, $pp ) = @_;
123 22 ▶ if ($pp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pp) 22 0 22
124 22 $self->{original_preplan} = $pp;
125 }
126 22 return $self->{original_preplan};
127 }
128  
129 # accessor for original plan
130 174 ▶sub preplan {
Subroutine preplan: called CC=2 SCAR=6.9
Pod undocumented
131 174 my ( $self, $pp ) = @_;
132 174 ▶ if ($pp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pp) 48 126 174
133 48 $self->{preplan} = $pp;
134 }
135 174 return $self->{preplan};
136 }
137  
138 # accessor for original
139 47 ▶sub pregex {
Subroutine pregex: called CC=2 SCAR=6.9
Pod undocumented
140 47 my ( $self, $pregex ) = @_;
141 47 ▶ if ($pregex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pregex) 22 25 47
142 22 $self->{pregex} = $pregex;
143 }
144 47 return $self->{pregex};
145 }
146  
147 # set/updated whenever ->next() and friends are called, simple way to
148 # query what plan was last created; RO, not destructive on current 'plan'
149 104 ▶sub plan {
Subroutine plan: called CC=1 SCAR=0.0
Pod documented
150 104 my $self = shift;
151 104 return $self->{plan};
152 }
153  
154 # setter/getter for DFA
155 163 ▶sub dfa {
Subroutine dfa: called CC=2 SCAR=6.9
Pod undocumented
156 163 my ( $self, $dfa ) = @_;
157 163 ▶ if ($dfa) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dfa) 16 147 163
158 16 $self->{DFA} = $dfa;
159 }
160 163 return $self->{DFA};
161 }
162  
163 # Converts plan -> PFA -> NFA -> DFA:
164 # NOTE: plan is not generated here, much call ->next()
165 # can pass param to underlying ->dfa also, like 'reset => 1'
166 16 ▶sub init_plan {
Subroutine init_plan: called CC=4 SCAR=17.9
Pod undocumented
167 16 my ( $self, %opts ) = @_;
168  
169 # requires plan (duh)
170 16 ▶ die qq{Need to call 'new' with 'preplan => q{PRE...}' to initialize\n} if not $self->pregex;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pregex 0 16 16
171  
172 # convert PRE to DFA
173 16 $self->convert_pregex_to_dfa(%opts);
174  
175 # warn if DFA is not acyclic (infinite strings accepted)
176 16 ▶ if ( $self->dfa->is_infinite ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dfa->is_infinite) 0 16 16
177 0 ▶ if ( not $self->{'allow-infinite'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow-infinite"}) 0 0 0
178 0 warn qq{(fatal) Infinite language detected. To avoid, do not use Kleene Star (*).\n};
179 0 die qq{ pass in 'allow-infinite => 1' to constructor to disable this warning.\n};
180 }
181 }
182  
183 # else - currently no meaningful way to control 'infinite' languages, this needs to
184 # be investigated
185  
186 # returns $self, for chaining in __PACKAGE__->run_any
187 16 return $self;
188 }
189  
190 # to force a reset, pass in, C<reset => 1>.; this makes a lot of cool things
191 16 ▶sub convert_pregex_to_dfa {
Subroutine convert_pregex_to_dfa: called CC=7 SCAR=19.8
Pod undocumented
192 16 my ( $self, %opts ) = @_;
193  
194 # look for cached DFA
195 16 ▶ if ( not $self->{reset} and $self->do_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"reset"} and $self->do_cache) 15 1 16
Condition66%2/3
not $self->{"reset"} and $self->do_cache
!ll&&!rl&&r
0115
MC/DC50%1/2
not $self->{"reset"} and $self->do_cache
not $self->{"reset"} $self->do_cache
Truth table66%2/3
not(A) and B
A= not $self->{"reset"}B= $self->do_cache
ABresult
0X0
100
111
196 15 ▶ if ( -e $self->cachefile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->cachefile) 7 8 15
197 7 $self->dfa( Storable::retrieve( $self->cachefile ) );
198 7 return $self->dfa;
199 }
200 }
201  
202 9 ▶ if ( not $self->dfa or defined $opts{reset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->dfa or defined $opts{"reset"}) 9 0 9
Condition33%1/3
not $self->dfa or defined $opts{"reset"}
l!l&&r!l&&!r
900
MC/DC0%0/2
not $self->dfa or defined $opts{"reset"}
not $self->dfa defined $opts{"reset"}
Truth table33%1/3
not(A) or B
A= not $self->dfaB= defined $opts{"reset"}
ABresult
1X1
011
000
203 9 $self->dfa( $self->pregex->as_pfa->as_nfa->as_dfa->as_min_dfa->trim_sinks );
204  
205 # save to cache
206 9 ▶ if ( $self->do_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_cache) 8 1 9
207 8 Storable::store( $self->dfa, $self->cachefile );
208 }
209 }
210 9 return $self->dfa;
211 }
212  
213 # Acyclic String Iterator
214 # force a reset, pass in, C<reset => 1>.
215 77 ▶sub next {
Subroutine next: called CC=4 SCAR=13.9
Pod undocumented
216 77 my ( $self, %opts ) = @_;
217  
218 77 ▶ die qq{(fatal) Use 'inext' instead of 'next' for infinite languages.\n} if $self->dfa->is_infinite;
Branch: 1 branch
BranchTrueFalseTotal
if $self->dfa->is_infinite 0 77 77
219  
220 77 ▶ if ( not defined $self->{_acyclical_iterator} or $opts{reset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_acyclical_iterator"} or $opts{"reset"}) 9 68 77
Condition100%3/3
not defined $self->{"_acyclical_iterator"} or $opts{"reset"}
l!l&&r!l&&!r
8168
MC/DC100%2/2
not defined $self->{"_acyclical_iterator"} or $opts{"reset"}
not defined $self->{"_acyclical_iterator"} $opts{"reset"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"_acyclical_iterator"}B= $opts{"reset"}
ABresult
1X1
011
000
221 9 $self->{_acyclical_iterator} = $self->dfa->init_acyclic_iterator(q{ });
222 }
223  
224 77 $self->{plan} = $self->{_acyclical_iterator}->();
225  
226 77 return $self->{plan};
227 }
228  
229 # accepts the same parameters as a constructor, used to re-initialize
230 # the current reference
231 0 ▶sub plan_nein {
Subroutine plan_nein: not called CC=1 SCAR=6.9
Pod undocumented
232 0 my $pkg = shift;
233 0 my $self = __PACKAGE__->new(@_);
234  
235 # also returns $self for convenience
236 0 return $self;
237 }
238  
239 # wrapper that combines C<init_plan> and C<run_once> to present an idiom,
240 # my $final_scope = Sub::Genius->new($plan)->run_any( scope => { ... });
241 3 ▶sub run_any {
Subroutine run_any: called CC=1 SCAR=0.0
Pod undocumented
242 3 my ( $self, %opts ) = @_;
243 3 $self->init_plan;
244 3 my $final_scope = $self->run_once(%opts);
245 3 return $final_scope;
246 }
247  
248 # Runs any single serialization ONCE
249 # defaults to main::, specify namespace of $sub
250 #
251 # * ns => q{Some::NS} # specify name space
252 # * scope => { } # specify initial state of pipeline accumulator
253 # * verbose => 0|1 # output runtime diagnostics
254 52 ▶sub run_once {
Subroutine run_once: called CC=8 SCAR=21.3
Pod undocumented
255 52 my ( $self, %opts ) = @_;
256  
257 # initialize scope
258 52 ▶ $opts{scope} //= {};
Condition50%1/2
$opts{"scope"} //= {}
l!l
520
MC/DC0%0/1
$opts{"scope"} //= {}
$opts{"scope"}
Truth table50%1/2
A
A= $opts{"scope"}
Aresult
11
00
259  
260 # appends '::' (no check if '::' is at the end to encourage a standard idiom)
261 52 ▶ if ( not defined $opts{ns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{'ns'}) { } 51 1 52
262 51 $opts{ns} = q{main::};
263 }
264 else {
265 1 $opts{ns} .= q{::};
266 }
267  
268 # only call interator if $self->{plan} has not yet been set
269 52 ▶ $self->next if not $self->plan;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->plan 5 47 52
270  
271 # check plan is set, just to be sure
272 52 ▶ if ( my $plan = $self->plan ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $plan = $self->plan) 52 0 52
273 52 ▶ if ( $opts{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"verbose"}) 0 52 52
274 0 print qq{plan: "$plan" <<<\n\nExecute:\n\n};
275 }
276 52 my @seq = split( / /, $plan );
277  
278 # main run loop - run once
279 52 local $@;
280 52 foreach my $sub (@seq) {
281 311 eval sprintf( qq{%s%s(\$opts{scope});}, $opts{ns}, $sub );
282 311 ▶ die $@ if $@; # be nice and die for easier debuggering
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 311 311
283 }
284 }
285 52 return $opts{scope};
286 }
287  
288 #
289 # D R A G O N S
290 # ~~~> *E X P E R I M E N T A L* (not even in POD yet)
291 #
292  
293 # Deep (Infinite) String Iterator
294 # force a reset, pass in, C<reset => 1>.
295 #
296 # To us:
297 # my $sg = Sub::Genius=->new(preplan => q{a&b*c}, => 'allow-infinite' => 1);
298 # $sg->init_plan;
299 #
300 #
301 0 ▶sub inext {
Subroutine inext: not called CC=4 SCAR=30.0
Pod undocumented
302 0 my ( $self, %opts ) = @_;
303 0 local $| = 1;
304 0 ▶ $opts{max} //= 5;
Condition0%0/2
$opts{"max"} //= 5
l!l
00
MC/DC0%0/1
$opts{"max"} //= 5
$opts{"max"}
Truth table0%0/2
A
A= $opts{"max"}
Aresult
11
00
305 0 ▶ if ( not defined $self->{_deepdft_iterator} or $opts{reset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_deepdft_iterator"} or $opts{"reset"}) 0 0 0
Condition0%0/3
not defined $self->{"_deepdft_iterator"} or $opts{"reset"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"_deepdft_iterator"} or $opts{"reset"}
not defined $self->{"_deepdft_iterator"} $opts{"reset"}
Truth table0%0/3
not(A) or B
A= not defined $self->{"_deepdft_iterator"}B= $opts{"reset"}
ABresult
1X1
011
000
306 0 $self->{_deepdft_iterator} = $self->dfa->init_deepdft_iterator( $opts{max}, q{ } );
307 }
308  
309 0 $self->{plan} = $self->{_deepdft_iterator}->();
310  
311 0 return $self->{plan};
312 }
313  
314 9 1;
315  
316 __END__
« Genius/Util.pm ↑ Summary x.pl »