Simple/Cache.pm

filter: Statementstmt 88.8% 246 / 277 Branchbran 59.7% 80 / 134 Conditioncond 50.0% 15 / 30 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 96.6% 29 / 30 Podpod 100% 9 / 9 totaltotal 77.2% 384 / 497 CC 82 SCAR 22.6
CC
82
Cov
77%
_hit_disk
29.8
_hit_memory
16.7
_dump_structure
12.7
CRAP 9.6
« Simple.pm ↑ Summary Simple/Base/Parser.pm »
1 ## no critic (ProhibitUnusedPrivateSubroutines)
2 package Text::Template::Simple::Cache;
3 32 $Text::Template::Simple::Cache::VERSION = '0.91';
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Text::Template::Simple::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use Text::Template::Simple::Util qw( DEBUG LOG fatal );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 my $CACHE = {}; # in-memory template cache
12  
13 45 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
14 45 my $class = shift;
15 45 ▶ my $parent = shift || fatal('tts.cache.new.parent');
Condition33%1/3
shift() || fatal('tts.cache.new.parent')
l!l&&r!l&&!r
4500
MC/DC0%0/2
shift() || fatal('tts.cache.new.parent')
shift() fatal('tts.cache.new.parent')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.cache.new.parent')
ABresult
1X1
011
000
16 45 my $self = [undef];
17 45 bless $self, $class;
18 45 $self->[CACHE_PARENT] = $parent;
19 45 return $self;
20 }
21  
22 358 ▶sub id {
Subroutine id: called CC=2 SCAR=6.9
Pod documented
23 358 my $self = shift;
24 358 my $val = shift;
25 358 ▶ $self->[CACHE_PARENT][CID] = $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 9 349 358
26 358 return $self->[CACHE_PARENT][CID];
27 }
28  
29 5 ▶sub type {
Subroutine type: called CC=3 SCAR=11.0
Pod documented
30 5 my $self = shift;
31 5 my $parent = $self->[CACHE_PARENT];
32 5 ▶ return $parent->[CACHE] ? $parent->[CACHE_DIR] ? 'DISK'
Branch: 2 branches
BranchTrueFalseTotal
$parent->[8] ? : 4 1 5
$parent->[9] ? : 2 2 4
33 : 'MEMORY'
34 : 'OFF';
35 }
36  
37 2 ▶sub reset { ## no critic (ProhibitBuiltinHomonyms)
Subroutine reset: called CC=7 SCAR=19.5
Pod documented
38 2 my $self = shift;
39 2 my $parent = $self->[CACHE_PARENT];
40 2 %{$CACHE} = ();
41  
42 2 ▶ if ( $parent->[CACHE] && $parent->[CACHE_DIR] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->[8] and $parent->[9]) 1 1 2
Condition66%2/3
$parent->[8] and $parent->[9]
!ll&&!rl&&r
011
MC/DC50%1/2
$parent->[8] and $parent->[9]
$parent->[8] $parent->[9]
Truth table66%2/3
A and B
A= $parent->[8]B= $parent->[9]
ABresult
0X0
100
111
43  
44 1 my $cdir = $parent->[CACHE_DIR];
45 1 require Symbol;
46 1 my $CDIRH = Symbol::gensym();
47 1 ▶ opendir $CDIRH, $cdir or fatal( 'tts.cache.opendir' => $cdir, $! );
Branch: 1 branch
BranchTrueFalseTotal
opendir $CDIRH, $cdir or fatal("tts.cache.opendir", $cdir, $!) 1 0 1
48 1 require File::Spec;
49 1 my $ext = quotemeta CACHE_EXT;
50 1 my $file;
51  
52 1 ▶ while ( defined( $file = readdir $CDIRH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($file = readdir $CDIRH)) 3 1 4
53 3 ▶ if ( $file =~ m{ ( .* $ext) \z}xmsi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ / ( .* $ext) \z/imsx) 1 2 3
54 1 $file = File::Spec->catfile( $parent->[CACHE_DIR], $1 );
55 1 ▶ LOG( UNLINK => $file ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 1 1
56 1 unlink $file;
57 }
58 }
59  
60 1 closedir $CDIRH;
61 }
62 2 return 1;
63 }
64  
65 10 ▶sub dumper {
Subroutine dumper: called CC=5 SCAR=16.4
Pod documented
66 10 my $self = shift;
67 10 ▶ my $type = shift || 'structure';
Condition50%1/2
shift() || 'structure'
l!l
100
MC/DC0%0/1
shift() || 'structure'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
68 10 ▶ my $param = shift || {};
Condition100%2/2
shift() || {}
l!l
64
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
69 10 ▶ fatal('tts.cache.dumper.hash') if ref $param ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $param ne "HASH" 0 10 10
70 10 my %valid = map { ($_, $_) } qw( ids structure );
71 10 ▶ fatal('tts.cache.dumper.type', $type) if not $valid{ $type };
Branch: 1 branch
BranchTrueFalseTotal
unless $valid{$type} 0 10 10
72 10 my $method = '_dump_' . $type;
73 10 return $self->$method( $param ); # TODO: modify the methods to accept HASH
74 }
75  
76 4 ▶sub _dump_ids {
Subroutine _dump_ids: called CC=3 SCAR=11.0
77 4 my $self = shift;
78 4 my $parent = $self->[CACHE_PARENT];
79 4 my $p = shift;
80 4 ▶ my $VAR = $p->{varname} || q{$} . q{CACHE_IDS};
Condition100%2/2
$p->{'varname'} || '$CACHE_IDS'
l!l
22
MC/DC100%1/1
$p->{'varname'} || '$CACHE_IDS'
$p->{'varname'}
Truth table100%2/2
A
A= $p->{'varname'}
Aresult
11
00
81 4 my @rv;
82  
83 4 ▶ if ( $parent->[CACHE_DIR] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->[9]) { } 2 2 4
84  
85 2 require File::Find;
86 2 require File::Spec;
87 2 my $ext = quotemeta CACHE_EXT;
88 2 my $re = qr{ (.+?) $ext \z }xms;
89 2 my($id, @list);
90  
91 File::Find::find(
92 {
93 no_chdir => 1,
94 4 ▶ wanted => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
95 4 ▶ if ( $_ =~ $re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /$re/) 2 2 4
96 2 ($id = $1) =~ s{.*[\\/]}{}xms;
97 2 push @list, $id;
98 }
99 },
100 },
101 2 $parent->[CACHE_DIR]
102 );
103  
104 2 @rv = sort @list;
105  
106 }
107 else {
108 2 @rv = sort keys %{ $CACHE };
109 }
110  
111 4 require Data::Dumper;
112 4 my $d = Data::Dumper->new( [ \@rv ], [ $VAR ]);
113 4 return $d->Dump;
114 }
115  
116 6 ▶sub _dump_structure {
Subroutine _dump_structure: called CC=9 SCAR=25.4
117 6 my $self = shift;
118 6 my $parent = $self->[CACHE_PARENT];
119 6 my $p = shift;
120 6 ▶ my $VAR = $p->{varname} || q{$} . q{CACHE};
Condition100%2/2
$p->{'varname'} || '$CACHE'
l!l
24
MC/DC100%1/1
$p->{'varname'} || '$CACHE'
$p->{'varname'}
Truth table100%2/2
A
A= $p->{'varname'}
Aresult
11
00
121 6 ▶ my $deparse = $p->{no_deparse} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$p->{'no_deparse'} ? : 2 4 6
122 6 require Data::Dumper;
123 6 my $d;
124  
125 6 ▶ if ( $parent->[CACHE_DIR] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->[9]) { } 3 3 6
126 3 $d = Data::Dumper->new( [ $self->_dump_disk_cache ], [ $VAR ] );
127 }
128 else {
129 3 $d = Data::Dumper->new( [ $CACHE ], [ $VAR ]);
130 3 ▶ if ( $deparse ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deparse) 2 1 3
131 2 ▶ fatal('tts.cache.dumper' => $Data::Dumper::VERSION)
Branch: 1 branch
BranchTrueFalseTotal
unless $d->can("Deparse") 0 2 2
132 if !$d->can('Deparse');
133 2 $d->Deparse(1);
134 }
135 }
136  
137 6 my $str = eval { $d->Dump; };
138  
139 6 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 6 6
140 0 ▶ if ( $deparse && $error =~ RE_DUMP_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deparse and $error =~ / Can\'t\ locate\ object\ method\ \"first\"\ via\ package\ \"B\:\:SVOP\" /msx) { } 0 0 0
Condition0%0/3
$deparse and $error =~ / Can\'t\ locate\ object\ method\ \"first\"\ via\ package\ \"B\:\:SVOP\" /msx
!ll&&!rl&&r
000
MC/DC0%0/2
$deparse and $error =~ / Can\'t\ locate\ object\ method\ \"first\"\ via\ package\ \"B\:\:SVOP\" /msx
$deparse $error =~ / Can\'t\ locate\ object\ method\ \"first\"\ via\ package\ \"B\:\:SVOP\" /msx
Truth table0%0/3
A and B
A= $deparseB= $error =~ / Can\'t\ locate\ object\ method\ \"first\"\ via\ package\ \"B\:\:SVOP\" /msx
ABresult
0X0
100
111
141 0 my $name = ref($self) . '::dump_cache';
142 0 warn "$name: An error occurred when dumping with deparse "
143 ."(are you under mod_perl?). Re-Dumping without deparse...\n";
144 0 warn "$error\n";
145 0 my $nd = Data::Dumper->new( [ $CACHE ], [ $VAR ]);
146 0 $nd->Deparse(0);
147 0 $str = $nd->Dump;
148 }
149 else {
150 0 croak $error;
151 }
152 }
153  
154 6 return $str;
155 }
156  
157 3 ▶sub _dump_disk_cache {
Subroutine _dump_disk_cache: called CC=1 SCAR=0.0
158 3 require File::Find;
159 3 require File::Spec;
160 3 my $self = shift;
161 3 my $parent = $self->[CACHE_PARENT];
162 3 my $pattern = quotemeta DISK_CACHE_MARKER;
163 3 my $ext = quotemeta CACHE_EXT;
164 3 my $re = qr{(.+?) $ext \z}xms;
165 3 my(%disk_cache);
166  
167 6 ▶ my $process = sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
168 6 my $file = $_;
169 6 my @match = $file =~ $re;
170 6 ▶ return if ! @match;
Branch: 1 branch
BranchTrueFalseTotal
unless @match 3 3 6
171 3 (my $id = $match[0]) =~ s{.*[\\/]}{}xms;
172 3 my $content = $parent->io->slurp( File::Spec->canonpath($file) );
173 3 my $ok = 0; # reset
174 3 my $_temp = EMPTY_STRING; # reset
175  
176 3 foreach my $line ( split m{\n}xms, $content ) {
177 30 ▶ if ( $line =~ m{$pattern}xmso ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /$pattern/mosx) 0 30 30
178 0 $ok = 1;
179 0 next;
180 }
181 30 ▶ next if not $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 30 0 30
182 0 $_temp .= $line;
183 }
184  
185 3 $disk_cache{ $id } = {
186 MTIME => (stat $file)[STAT_MTIME],
187 CODE => $_temp,
188 };
189 3 };
190  
191 3 File::Find::find(
192 {
193 no_chdir => 1,
194 wanted => $process,
195 },
196 $parent->[CACHE_DIR]
197 );
198 3 return \%disk_cache;
199 }
200  
201 4 ▶sub size {
Subroutine size: called CC=7 SCAR=19.9
Pod documented
202 4 my $self = shift;
203 4 my $parent = $self->[CACHE_PARENT];
204  
205 4 ▶ return 0 if not $parent->[CACHE]; # calculate only if cache is enabled
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->[8] 0 4 4
206  
207 4 ▶ if ( my $cdir = $parent->[CACHE_DIR] ) { # disk cache
Branch: 1 branch
BranchTrueFalseTotal
if (my $cdir = $parent->[9]) { } 2 2 4
208 2 require File::Find;
209 2 my $total = 0;
210 2 my $ext = quotemeta CACHE_EXT;
211  
212 3 ▶ my $wanted = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
213 3 ▶ return if $_ !~ m{ $ext \z }xms; # only calculate "our" files
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ / $ext \z /msx 2 1 3
214 1 $total += (stat $_)[STAT_SIZE];
215 2 };
216  
217 2 File::Find::find( { wanted => $wanted, no_chdir => 1 }, $cdir );
218 2 return $total;
219  
220 }
221 else { # in-memory cache
222  
223 2 local $SIG{__DIE__};
224 2 ▶ if ( eval { require Devel::Size; 1; } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Devel::Size; 1 } }) { } 2 0 2
225 2 my $dsv = Devel::Size->VERSION;
226 2 ▶ LOG( DEBUG => "Devel::Size v$dsv is loaded." ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 2 2
227 2 ▶ fatal('tts.cache.develsize.buggy', $dsv) if $dsv < DEVEL_SIZE_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if $dsv < "0.72" 0 2 2
228 2 my $size = eval { Devel::Size::total_size( $CACHE ) };
229 2 ▶ fatal('tts.cache.develsize.total', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
230 2 return $size;
231 }
232 else {
233 0 warn "Failed to load Devel::Size: $@\n";
234 0 return 0;
235 }
236  
237 }
238 }
239  
240 12 ▶sub has {
Subroutine has: called CC=9 SCAR=24.4
Pod documented
241 12 my($self, @args ) = @_;
242 12 ▶ fatal('tts.cache.pformat') if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 12 12
243 12 my %opt = @args;
244 12 my $parent = $self->[CACHE_PARENT];
245  
246 12 ▶ if ( not $parent->[CACHE] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->[8]) 0 12 12
247 0 ▶ LOG( DEBUG => 'Cache is disabled!') if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
248 0 return;
249 }
250  
251  
252 12 my $id = $parent->connector('Cache::ID')->new;
253 my $cid = $opt{id} ? $id->generate($opt{id} , 'custom')
254 : $opt{data} ? $id->generate($opt{data} )
255 12 ▶ : fatal('tts.cache.incache');
Branch: 2 branches
BranchTrueFalseTotal
$opt{'id'} ? : 6 6 12
$opt{'data'} ? : 6 0 6
256  
257 12 ▶ if ( my $cdir = $parent->[CACHE_DIR] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cdir = $parent->[9]) { } 6 6 12
258 6 require File::Spec;
259 6 ▶ return -e File::Spec->catfile( $cdir, $cid . CACHE_EXT ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e 'File::Spec'->catfile($cdir, $cid . '.tts.cache') ? : 6 0 6
260 }
261 else {
262 6 ▶ return exists $CACHE->{ $cid } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $CACHE->{$cid} ? : 6 0 6
263 }
264 }
265  
266 1 ▶sub _is_meta_version_old {
Subroutine _is_meta_version_old: called CC=4 SCAR=14.0
267 1 my $self = shift;
268 1 my $v = shift;
269 1 ▶ return 1 if ! $v; # no version? archaic then
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 1 1
270 1 my $pv = PARENT->VERSION;
271 1 foreach my $i ( $v, $pv ) {
272 2 $i =~ tr/_//d; # underscore versions cause warnings
273 2 $i += 0; # force number
274 }
275 1 ▶ return 1 if $v < $pv;
Branch: 1 branch
BranchTrueFalseTotal
if $v < $pv 0 1 1
276 1 return;
277 }
278  
279 9 ▶sub hit {
Subroutine hit: called CC=3 SCAR=11.0
Pod documented
280 # TODO: return $CODE, $META;
281 9 my $self = shift;
282 9 my $cache_id = shift;
283 9 ▶ my $chkmt = shift || 0;
Condition50%1/2
shift() || 0
l!l
09
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
284  
285 9 ▶ my $method = $self->[CACHE_PARENT][CACHE_DIR] ? '_hit_disk' : '_hit_memory';
Branch: 1 branch
BranchTrueFalseTotal
$self->[0][9] ? : 4 5 9
286 9 return $self->$method( $cache_id, $chkmt );
287 }
288  
289 5 ▶sub _hit_memory {
Subroutine _hit_memory: called CC=6 SCAR=28.1
290 5 my($self, $cache_id, $chkmt) = @_;
291 5 ▶ if ( $chkmt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chkmt) 0 5 5
292 0 ▶ my $mtime = $CACHE->{$cache_id}{MTIME} || 0;
Condition0%0/2
$CACHE->{$cache_id}{'MTIME'} || 0
l!l
00
MC/DC0%0/1
$CACHE->{$cache_id}{'MTIME'} || 0
$CACHE->{$cache_id}{'MTIME'}
Truth table0%0/2
A
A= $CACHE->{$cache_id}{'MTIME'}
Aresult
11
00
293 0 ▶ if ( $mtime != $chkmt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime != $chkmt) 0 0 0
294 0 ▶ LOG( MTIME_DIFF => "\tOLD: $mtime\n\t\tNEW: $chkmt" ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
295 0 return; # i.e.: Update cache
296 }
297 }
298 5 ▶ LOG( MEM_CACHE => EMPTY_STRING ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 5 5
299 5 return $CACHE->{$cache_id}->{CODE};
300 }
301  
302 4 ▶sub _hit_disk {
Subroutine _hit_disk: called CC=15 SCAR=33.9
303 4 my($self, $cache_id, $chkmt) = @_;
304 4 my $parent = $self->[CACHE_PARENT];
305 4 my $cdir = $parent->[CACHE_DIR];
306 4 require File::Spec;
307 4 my $cache = File::Spec->catfile( $cdir, $cache_id . CACHE_EXT );
308 4 ▶ my $ok = -e $cache && ! -d _ && -f _;
Condition66%4/6
-e $cache && !-d(_) && -f _
!ll&&!rl&&r
301
-e $cache and not -d _
!ll&&!rl&&r
301
MC/DC33%1/3
-e $cache && !-d(_) && -f _
-e $cache not -d _ -f _
Truth table50%2/4
A and not(B) && C
A= -e $cacheB= not -d _C= -f _
ABCresult
0XX0
10X0
1100
1111
309 4 ▶ return if not $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 1 4
310  
311 1 my $disk_cache = $parent->io->slurp($cache);
312 1 my %meta;
313 1 ▶ if ( $disk_cache =~ m{ \A \#META: (.+?) \n }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($disk_cache =~ / \A \#META: (.+?) \n /msx) 1 0 1
314 1 %meta = $self->_get_meta( $1 );
315 1 ▶ fatal('tts.cache.hit.meta', $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
316 }
317 1 ▶ if ( $self->_is_meta_version_old( $meta{VERSION} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_meta_version_old($meta{"VERSION"})) 0 1 1
318 0 ▶ my $id = $parent->[FILENAME] || $cache_id;
Condition0%0/3
$parent->[25] || $cache_id
l!l&&r!l&&!r
000
MC/DC0%0/2
$parent->[25] || $cache_id
$parent->[25] $cache_id
Truth table0%0/3
A || B
A= $parent->[25]B= $cache_id
ABresult
1X1
011
000
319 0 warn "(This message will only appear once) $id was compiled with"
320 .' an old version of ' . PARENT . ". Resetting cache.\n";
321 0 return;
322 }
323 1 ▶ if ( my $mtime = $meta{CHKMT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mtime = $meta{"CHKMT"}) 0 1 1
324 0 ▶ if ( $mtime != $chkmt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtime != $chkmt) 0 0 0
325 0 ▶ LOG( MTIME_DIFF => "\tOLD: $mtime\n\t\tNEW: $chkmt") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
326 0 return; # i.e.: Update cache
327 }
328 }
329  
330 1 my($CODE, $error) = $parent->_wrap_compile($disk_cache);
331 1 ▶ $parent->[NEEDS_OBJECT] = $meta{NEEDS_OBJECT} if $meta{NEEDS_OBJECT};
Branch: 1 branch
BranchTrueFalseTotal
if $meta{"NEEDS_OBJECT"} 0 1 1
332 1 ▶ $parent->[FAKER_SELF] = $meta{FAKER_SELF} if $meta{FAKER_SELF};
Branch: 1 branch
BranchTrueFalseTotal
if $meta{"FAKER_SELF"} 1 0 1
333  
334 1 ▶ fatal('tts.cache.hit.cache', $error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 1 1
335 1 ▶ LOG( FILE_CACHE => EMPTY_STRING ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 1 1
336 #$parent->[COUNTER]++;
337 1 return $CODE;
338 }
339  
340 249 ▶sub populate {
Subroutine populate: called CC=4 SCAR=13.9
Pod documented
341 249 my($self, $cache_id, $parsed, $chkmt) = @_;
342 249 my $parent = $self->[CACHE_PARENT];
343 249 ▶ my $target = ! $parent->[CACHE] ? '_populate_no_cache'
Branch: 2 branches
BranchTrueFalseTotal
!$parent->[8] ? : 242 7 249
$parent->[9] ? : 3 4 7
344 : $parent->[CACHE_DIR] ? '_populate_disk'
345 : '_populate_memory'
346 ;
347  
348 249 my($CODE, $error) = $self->$target( $parsed, $cache_id, $chkmt );
349 249 ▶ $self->_populate_error( $parsed, $cache_id, $error ) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 248 249
350 248 ++$parent->[COUNTER];
351 248 return $CODE;
352 }
353  
354 1 ▶sub _populate_error {
Subroutine _populate_error: called CC=3 SCAR=13.1
355 1 my($self, $parsed, $cache_id, $error) = @_;
356 1 my $parent = $self->[CACHE_PARENT];
357 1 ▶ croak $parent->[VERBOSE_ERRORS]
Branch: 2 branches
BranchTrueFalseTotal
$parent->[33] ? : 0 1 1
$cache_id ? : 0 0 0
358 ? $parent->_mini_compiler(
359 $parent->_internal('compile_error'),
360 {
361 CID => $cache_id ? $cache_id : 'N/A',
362 ERROR => $error,
363 PARSED => $parsed,
364 TIDIED => $parent->_tidy( $parsed ),
365 }
366 )
367 : $error
368 ;
369 }
370  
371 242 ▶sub _populate_no_cache {
Subroutine _populate_no_cache: called CC=2 SCAR=7.0
372 # cache is disabled
373 242 my($self, $parsed, $cache_id, $chkmt) = @_;
374 242 my($CODE, $error) = $self->[CACHE_PARENT]->_wrap_compile($parsed);
375 242 ▶ LOG( NC_POPUL => $cache_id ) if DEBUG >= DEBUG_LEVEL_INSANE;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() >= 3 0 242 242
376 242 return $CODE, $error;
377 }
378  
379 4 ▶sub _populate_memory {
Subroutine _populate_memory: called CC=3 SCAR=11.1
380 4 my($self, $parsed, $cache_id, $chkmt) = @_;
381 4 my $parent = $self->[CACHE_PARENT];
382 4 my $c = $CACHE->{ $cache_id } = {}; # init
383 4 my($CODE, $error) = $parent->_wrap_compile($parsed);
384 4 $c->{CODE} = $CODE;
385 4 ▶ $c->{MTIME} = $chkmt if $chkmt;
Branch: 1 branch
BranchTrueFalseTotal
if $chkmt 0 4 4
386 4 $c->{NEEDS_OBJECT} = $parent->[NEEDS_OBJECT];
387 4 $c->{FAKER_SELF} = $parent->[FAKER_SELF];
388 4 ▶ LOG( MEM_POPUL => $cache_id ) if DEBUG >= DEBUG_LEVEL_INSANE;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() >= 3 0 4 4
389 4 return $CODE, $error;
390 }
391  
392 3 ▶sub _populate_disk {
Subroutine _populate_disk: called CC=5 SCAR=16.2
393 3 my($self, $parsed, $cache_id, $chkmt) = @_;
394  
395 3 require File::Spec;
396 3 require Fcntl;
397 3 require IO::File;
398  
399 3 my $parent = $self->[CACHE_PARENT];
400 3 my %meta = (
401 CHKMT => $chkmt,
402 NEEDS_OBJECT => $parent->[NEEDS_OBJECT],
403 FAKER_SELF => $parent->[FAKER_SELF],
404 VERSION => PARENT->VERSION,
405 );
406  
407 3 my $cache = File::Spec->catfile( $parent->[CACHE_DIR], $cache_id . CACHE_EXT);
408 3 my $fh = IO::File->new;
409 3 ▶ $fh->open($cache, '>') or fatal('tts.cache.populate.write', $cache, $!);
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($cache, ">") or fatal("tts.cache.populate.write", $cache, $!) 3 0 3
410 3 flock $fh, Fcntl::LOCK_EX();
411 3 $parent->io->layer($fh);
412 3 my $warn = $parent->_mini_compiler(
413 $parent->_internal('disk_cache_comment'),
414 {
415 NAME => PARENT->class_id,
416 DATE => scalar localtime time,
417 }
418 );
419 3 my $ok = print { $fh } '#META:' . $self->_set_meta(\%meta) . "\n",
420 $warn,
421 $parsed;
422 3 flock $fh, Fcntl::LOCK_UN();
423 3 ▶ close $fh or croak "Unable to close filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Unable to close filehandle: $!") 3 0 3
424 3 ▶ chmod(CACHE_FMODE, $cache) || fatal('tts.cache.populate.chmod');
Branch: 1 branch
BranchTrueFalseTotal
chmod 0600, $cache or fatal("tts.cache.populate.chmod") 3 0 3
425  
426 3 my($CODE, $error) = $parent->_wrap_compile($parsed);
427 3 ▶ LOG( DISK_POPUL => $cache_id ) if DEBUG >= DEBUG_LEVEL_INSANE;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() >= 3 0 3 3
428 3 return $CODE, $error;
429 }
430  
431 1 ▶sub _get_meta {
Subroutine _get_meta: called CC=1 SCAR=0.0
432 1 my $self = shift;
433 1 my $raw = shift;
434 1 my %meta = map { split m{:}xms, $_ } split m{[|]}xms, $raw;
435 1 return %meta;
436 }
437  
438 3 ▶sub _set_meta {
Subroutine _set_meta: called CC=1 SCAR=0.0
439 3 my $self = shift;
440 3 my $meta = shift;
441 3 my $rv = join q{|}, map { $_ . q{:} . $meta->{ $_ } } keys %{ $meta };
442 3 return $rv;
443 }
444  
445 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
446 0 my $self = shift;
447 0 ▶ LOG( DESTROY => ref $self ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
448 0 $self->[CACHE_PARENT] = undef;
449 0 @{$self} = ();
450 0 return;
451 }
452  
453 32 1;
454  
455 __END__
« Simple.pm ↑ Summary Simple/Base/Parser.pm »