lib/HTML/Mason/Interp.pm

filter: Statementstmt 92.1% 401 / 435 Branchbran 78.3% 130 / 166 Conditioncond 76.9% 30 / 39 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 91.8% 79 / 86 Podpod 44.8% 13 / 29 totaltotal 85.3% 664 / 778 SCAR 48.8
CC
109
Cov
88%
load
20.2
set_escape
14.1
_assign_comp_root
10.4
CRAP 131.3
« lib/HTML/Mason/FakeApache.pm ↑ Summary lib/HTML/Mason/Compiler.pm »
1 # -*- cperl-indent-level: 4; cperl-continued-brace-offset: -4; cperl-continued-statement-offset: 4 -*-
2  
3 # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6  
7 package HTML::Mason::Interp;
8 32 $HTML::Mason::Interp::VERSION = '1.58';
9 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use HTML::Mason;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶use HTML::Mason::Escapes;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 32 ▶use HTML::Mason::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 32 ▶use HTML::Mason::Resolver::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 32 ▶use HTML::Mason::Tools qw(read_file taint_is_on load_pkg);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 32 ▶use HTML::Mason::Exceptions( abbr => [qw(param_error system_error wrong_compiler_error compilation_error error)] );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 32 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 0 ▶Params::Validate::validation_options( on_fail => sub { param_error join '', @_ } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
26  
27 32 ▶use Class::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 32 ▶use base qw(Class::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 BEGIN
31 {
32 # Fields that can be set in new method, with defaults
33 32 ▶ __PACKAGE__->valid_params
Subroutine BEGIN: called CC=1 SCAR=0.0
34 (
35 autohandler_name =>
36 { parse => 'string', default => 'autohandler', type => SCALAR,
37 descr => "The filename to use for Mason's 'autohandler' capability" },
38  
39 buffer_preallocate_size =>
40 { parse => 'string', default => 0, type => SCALAR,
41 descr => "Number of bytes to preallocate in request buffer" },
42
43 code_cache_max_size =>
44 { parse => 'string', default => 'unlimited', type => SCALAR,
45 descr => "The maximum number of components in the code cache" },
46  
47 comp_root =>
48 { parse => 'list',
49 type => SCALAR|ARRAYREF,
50 default => File::Spec->rel2abs( Cwd::cwd ),
51 descr => "A string or array of arrays indicating the search path for component calls" },
52  
53 compiler =>
54 { isa => 'HTML::Mason::Compiler',
55 descr => "A Compiler object for compiling components" },
56  
57 data_dir =>
58 { parse => 'string', optional => 1, type => SCALAR,
59 descr => "A directory for storing cache files and other state information" },
60  
61 dynamic_comp_root =>
62 { parse => 'boolean', default => 0, type => BOOLEAN,
63 descr => "Indicates whether the comp_root may be changed between requests" },
64  
65 escape_flags =>
66 { parse => 'hash_list', optional => 1, type => HASHREF,
67 descr => "A list of escape flags to set (as if calling the set_escape() method" },
68  
69 object_file_extension =>
70 { parse => 'string', type => SCALAR, default => '.obj',
71 descr => "Extension to add to the end of object files" },
72  
73 # OBJECT cause qr// returns an object
74 ignore_warnings_expr =>
75 { parse => 'string', type => SCALAR|OBJECT, default => qr/Subroutine .* redefined/i,
76 descr => "A regular expression describing Perl warning messages to ignore" },
77  
78 preloads =>
79 { parse => 'list', optional => 1, type => ARRAYREF,
80 descr => "A list of components to load immediately when creating the Interpreter" },
81  
82 resolver =>
83 { isa => 'HTML::Mason::Resolver',
84 descr => "A Resolver object for fetching components from storage" },
85  
86 static_source =>
87 { parse => 'boolean', default => 0, type => BOOLEAN,
88 descr => "When true, we only compile source files once" },
89  
90 static_source_touch_file =>
91 { parse => 'string', optional => 1, type => SCALAR,
92 descr => "A file that, when touched, causes Mason to clear its component caches" },
93  
94 use_object_files =>
95 { parse => 'boolean', default => 1, type => BOOLEAN,
96 descr => "Whether to cache component objects on disk" },
97 );
98  
99 32 __PACKAGE__->contained_objects
100 (
101 resolver => { class => 'HTML::Mason::Resolver::File',
102 descr => "This class is expected to return component information based on a component path" },
103 compiler => { class => 'HTML::Mason::Compiler::ToObject',
104 descr => "This class is used to translate component source into code" },
105 request => { class => 'HTML::Mason::Request',
106 delayed => 1,
107 descr => "Objects returned by make_request are members of this class" },
108 );
109 }
110  
111 use HTML::Mason::MethodMaker
112 ( read_only => [ qw( autohandler_name
113 buffer_preallocate_size
114 code_cache
115 code_cache_min_size
116 code_cache_max_size
117 compiler
118 data_dir
119 dynamic_comp_root
120 object_file_extension
121 preallocated_output_buffer
122 preloads
123 resolver
124 source_cache
125 static_source
126 static_source_touch_file
127 use_internal_component_caches
128 use_object_files
129 ) ],
130  
131 32 read_write => [ map { [ $_ => __PACKAGE__->validation_spec->{$_} ] }
132 qw( ignore_warnings_expr
133 )
134 ],
135  
136 read_write_contained => { request =>
137 [ [ autoflush => { type => BOOLEAN } ],
138 [ data_cache_api => { type => SCALAR } ],
139 [ data_cache_defaults => { type => HASHREF } ],
140 [ dhandler_name => { type => SCALAR } ],
141 [ error_format => { type => SCALAR } ],
142 [ error_mode => { type => SCALAR } ],
143 [ max_recurse => { type => SCALAR } ],
144 [ out_method => { type => SCALARREF | CODEREF } ],
145 [ plugins => { type => ARRAYREF } ],
146 ]
147 },
148 32 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 sub new
151 {
152 414 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
153 414 my $self = $class->SUPER::new(@_);
154  
155 414 $self->_initialize;
156 414 return $self;
157 }
158  
159 sub _initialize
160 {
161 414 ▶ my ($self) = shift;
Subroutine _initialize: called CC=3 SCAR=11.0
162 414 $self->{code_cache} = {};
163 414 $self->{source_cache} = {};
164 414 $self->{files_written} = [];
165 414 $self->{static_source_touch_file_lastmod} = 0;
166  
167 414 $self->_assign_comp_root($self->{comp_root});
168 414 $self->_check_data_dir();
169 414 $self->_create_data_subdirs();
170 414 $self->_initialize_escapes();
171  
172 #
173 # Create preallocated buffer for requests.
174 #
175 414 $self->{preallocated_output_buffer} = ' ' x $self->buffer_preallocate_size;
176  
177 414 $self->_set_code_cache_attributes();
178  
179 #
180 # If static_source=1, unlimited_code_cache=1, and
181 # dynamic_comp_root=0, we can safely cache component objects keyed
182 # on path throughout the framework (e.g. within other component
183 # objects). These internal caches can be cleared in
184 # $interp->flush_code_cache (the only legimiate place for a
185 # component to be eliminated from the cache), eliminating any
186 # chance for leaked objects.
187 #
188 # static_source has to be on or else we might keep around
189 # old versions of components that have changed.
190 #
191 # unlimited_code_cache has to be on or else we might leak
192 # components when we discard.
193 #
194 # dynamic_comp_root has to be 0 because the cache would not be
195 # valid for different combinations of component root across
196 # different requests.
197 #
198 $self->{use_internal_component_caches} =
199 ($self->{static_source} &&
200 $self->{unlimited_code_cache} &&
201 414 ▶ !$self->{dynamic_comp_root});
Condition83%5/6
$self->{'static_source'} && $self->{'unlimited_code_cache'} && !$self->{'dynamic_comp_root'}
!ll&&!rl&&r
40815
$self->{"static_source"} and $self->{"unlimited_code_cache"}
!ll&&!rl&&r
40806
MC/DC66%2/3
$self->{'static_source'} && $self->{'unlimited_code_cache'} && !$self->{'dynamic_comp_root'}
$self->{"static_source"} $self->{"unlimited_code_cache"} !$self->{'dynamic_comp_root'}
Truth table75%3/4
A and B && not(C)
A= $self->{"static_source"}B= $self->{"unlimited_code_cache"}C= !$self->{'dynamic_comp_root'}
ABCresult
0XX0
10X0
1100
1111
202  
203 414 $self->_preload_components();
204 }
205  
206 sub _check_data_dir
207 {
208 414 ▶ my $self = shift;
Subroutine _check_data_dir: called CC=3 SCAR=11.0
209  
210 414 ▶ return unless $self->{data_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"data_dir"} 12 402 414
211  
212 402 $self->{data_dir} = File::Spec->canonpath( $self->{data_dir} );
213 param_error "data_dir '$self->{data_dir}' must be an absolute directory"
214 402 ▶ unless File::Spec->file_name_is_absolute( $self->{data_dir} );
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($self->{"data_dir"}) 0 402 402
215 }
216  
217 sub _create_data_subdirs
218 {
219 414 ▶ my $self = shift;
Subroutine _create_data_subdirs: called CC=2 SCAR=6.9
220  
221 414 ▶ if ($self->data_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_dir) { } 402 12 414
222 402 $self->_make_object_dir;
223 402 $self->_make_cache_dir;
224 } else {
225 12 $self->{use_object_files} = 0;
226 }
227 }
228  
229 sub _initialize_escapes
230 {
231 414 ▶ my $self = shift;
Subroutine _initialize_escapes: called CC=4 SCAR=13.9
232  
233 #
234 # Add the escape flags (including defaults)
235 #
236 414 foreach ( [ h => \&HTML::Mason::Escapes::html_entities_escape ],
237 [ u => \&HTML::Mason::Escapes::url_escape ],
238 )
239 {
240 828 $self->set_escape(@$_);
241 }
242  
243 414 ▶ if ( my $e = delete $self->{escape_flags} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = delete $self->{"escape_flags"}) 1 413 414
244 {
245 1 ▶ while ( my ($flag, $code) = each %$e )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$e) 1 1 2
246 {
247 1 $self->set_escape( $flag => $code );
248 }
249 }
250 }
251  
252 sub _set_code_cache_attributes
253 {
254 414 ▶ my $self = shift;
Subroutine _set_code_cache_attributes: called CC=2 SCAR=6.9
255  
256 414 $self->{unlimited_code_cache} = ($self->{code_cache_max_size} eq 'unlimited');
257 414 ▶ unless ($self->{unlimited_code_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"unlimited_code_cache"}) 5 409 414
258 5 $self->{code_cache_min_size} = $self->{code_cache_max_size} * 0.75;
259 }
260 }
261  
262 sub _preload_components
263 {
264 414 ▶ my $self = shift;
Subroutine _preload_components: called CC=9 SCAR=22.2
265  
266 414 ▶ return unless $self->preloads;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->preloads 412 2 414
267  
268 2 foreach my $pattern (@{$self->preloads}) {
269 3 ▶ error "preload pattern '$pattern' must be an absolute path"
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($pattern) 0 3 3
270 unless File::Spec->file_name_is_absolute($pattern);
271 3 my %path_hash;
272 3 foreach my $pair ($self->comp_root_array) {
273 3 my $root = $pair->[1];
274 3 foreach my $path ($self->resolver->glob_path($pattern, $root)) {
275 6 $path_hash{$path}++;
276 }
277 }
278 3 my @paths = keys(%path_hash);
279 3 ▶ warn "Didn't find any components for preload pattern '$pattern'"
Branch: 1 branch
BranchTrueFalseTotal
unless @paths 0 3 3
280 unless @paths;
281 3 foreach (@paths)
282 {
283 6 ▶ $self->load($_)
Branch: 1 branch
BranchTrueFalseTotal
$self->load($_) or error("Cannot load component $_, found via pattern $pattern") 0 6 6
284 or error "Cannot load component $_, found via pattern $pattern";
285 }
286 }
287 }
288  
289 #
290 # Functions for retrieving and creating data subdirectories.
291 #
292 1873 ▶sub object_dir { my $self = shift; return $self->data_dir ? File::Spec->catdir( $self->data_dir, 'obj' ) : ''; }
Subroutine object_dir: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->data_dir ? : 1873 0 1873
293 404 ▶sub object_create_marker_file { my $self = shift; return $self->object_dir ? File::Spec->catfile($self->object_dir, '.__obj_create_marker') : ''; }
Subroutine object_create_marker_file: called CC=2 SCAR=7.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->object_dir ? : 404 0 404
294 669 ▶sub cache_dir { my $self = shift; return $self->data_dir ? File::Spec->catdir( $self->data_dir, 'cache' ) : ''; }
Subroutine cache_dir: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->data_dir ? : 667 2 669
295  
296 sub _make_data_subdir
297 {
298 805 ▶ my ($self, $dir) = @_;
Subroutine _make_data_subdir: called CC=3 SCAR=12.3
299  
300 805 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 57 748 805
301 57 my @newdirs = eval { mkpath( $dir, 0, 0775 ) };
302 57 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 57 57
303 0 my $user = getpwuid($<);
304 0 my $group = getgrgid($();
305 0 my $data_dir = $self->data_dir;
306 0 error "Cannot create directory '$dir' ($@) for user '$user', group '$group'. " .
307 "Perhaps you need to create or set permissions on your data_dir ('$data_dir'). ";
308 }
309 57 $self->push_files_written(@newdirs);
310 }
311 }
312  
313 sub _make_object_dir
314 {
315 403 ▶ my ($self) = @_;
Subroutine _make_object_dir: called CC=3 SCAR=11.0
316  
317 403 my $object_dir = $self->object_dir;
318 403 $self->_make_data_subdir($object_dir);
319 403 my $object_create_marker_file = $self->object_create_marker_file;
320 403 ▶ unless (-f $object_create_marker_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $object_create_marker_file) 29 374 403
321 29 ▶ open my $fh, ">$object_create_marker_file"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$object_create_marker_file" or system_error("Could not create '${object_create_marker_file}': $!") 0 29 29
322 or system_error "Could not create '$object_create_marker_file': $!";
323 29 $self->push_files_written($object_create_marker_file);
324 }
325 }
326  
327 sub _make_cache_dir
328 {
329 402 ▶ my ($self) = @_;
Subroutine _make_cache_dir: called CC=1 SCAR=0.0
330  
331 402 my $cache_dir = $self->cache_dir;
332 402 $self->_make_data_subdir($cache_dir);
333 }
334  
335 #
336 # exec is the initial entry point for executing a component
337 # in a new request.
338 #
339 sub exec {
340 418 ▶ my $self = shift;
Subroutine exec: called CC=1 SCAR=0.0
Pod documented
341 418 my $comp = shift;
342 418 $self->make_request(comp=>$comp, args=>\@_)->exec;
343 }
344  
345 sub make_request {
346 506 ▶ my $self = shift;
Subroutine make_request: called CC=1 SCAR=0.0
Pod documented
347  
348 506 return $self->create_delayed_object( 'request', interp => $self, @_ );
349 }
350  
351 sub comp_exists {
352 0 ▶ my ($self, $path) = @_;
Subroutine comp_exists: not called CC=1 SCAR=6.9
Pod documented
353 0 return $self->resolve_comp_path_to_source($path);
354 }
355  
356 #
357 # Load <$path> into a component, possibly parsing the source and/or
358 # caching the code. Returns a component object or undef if the
359 # component was not found.
360 #
361 sub load {
362 2305 ▶ my ($self, $path) = @_;
Subroutine load: called CC=20 SCAR=30.1
Pod documented
363 2305 my ($maxfilemod, $objfile, $objfilemod);
364 2305 my $code_cache = $self->{code_cache};
365 2305 my $resolver = $self->{resolver};
366  
367 #
368 # Path must be absolute.
369 #
370 2305 ▶ unless (substr($path, 0, 1) eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($path, 0, 1) eq "/") 0 2305 2305
371 0 error "Component path given to Interp->load must be absolute (was given $path)";
372 }
373  
374 #
375 # Get source info from resolver.
376 #
377 2305 my $source = $self->resolve_comp_path_to_source($path);
378  
379 # No component matches this path.
380 2305 ▶ return unless defined $source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source 1098 1207 2305
381  
382 # comp_id is the unique name for the component, used for cache key
383 # and object file name.
384 1207 my $comp_id = $source->comp_id;
385  
386 #
387 # Get last modified time of source.
388 #
389 1207 my $srcmod = $source->last_modified;
390  
391 #
392 # If code cache contains an up to date entry for this path, use
393 # the cached comp. Always use the cached comp in static_source
394 # mode.
395 #
396 1207 ▶ if ( exists $code_cache->{$comp_id} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $code_cache->{$comp_id} and $self->static_source || $code_cache->{$comp_id}{"lastmod"} >= $srcmod) 529 678 1207
Condition100%6/6
exists $code_cache->{$comp_id} and $self->static_source || $code_cache->{$comp_id}{"lastmod"} >= $srcmod
!ll&&!rl&&r
67711519
$self->static_source or $code_cache->{$comp_id}{"lastmod"} >= $srcmod
l!l&&r!l&&!r
125171
MC/DC66%2/3
exists $code_cache->{$comp_id} and $self->static_source || $code_cache->{$comp_id}{"lastmod"} >= $srcmod
exists $code_cache->{$comp_id} $self->static_source $code_cache->{$comp_id}{"lastmod"} >= $srcmod
Truth table75%3/4
A and B or C
A= exists $code_cache->{$comp_id}B= $self->static_sourceC= $code_cache->{$comp_id}{"lastmod"} >= $srcmod
ABCresult
0XX0
1000
11X1
1011
397 ( $self->static_source || $code_cache->{$comp_id}->{lastmod} >= $srcmod )
398 ) {
399 529 return $code_cache->{$comp_id}->{comp};
400 }
401  
402 678 ▶ if ($self->{use_object_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_object_files"}) 658 20 678
403 658 $objfile = $self->comp_id_to_objfile($comp_id);
404  
405 658 my @stat = stat $objfile;
406 658 ▶ if ( @stat && ! -f _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat and not -f _) 0 658 658
Condition66%2/3
@stat and not -f _
!ll&&!rl&&r
5381200
MC/DC0%0/2
@stat and not -f _
@stat not -f _
Truth table66%2/3
A and not(B)
A= @statB= not -f _
ABresult
0X0
100
111
407 0 error "The object file '$objfile' exists but it is not a file!";
408 }
409  
410 658 ▶ if ($self->static_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->static_source) { } 21 637 658
411 # No entry in the code cache so if the object file exists,
412 # we will use it, otherwise we must create it. These
413 # values make that happen.
414 21 ▶ $objfilemod = @stat ? $srcmod : 0;
Branch: 1 branch
BranchTrueFalseTotal
@stat ? : 3 18 21
415 } else {
416 # If the object file exists, get its modification time.
417 # Otherwise (it doesn't exist or it is a directory) we
418 # must create it.
419 637 ▶ $objfilemod = @stat ? $stat[9] : 0;
Branch: 1 branch
BranchTrueFalseTotal
@stat ? : 117 520 637
420 }
421 }
422  
423 678 my $comp;
424 678 ▶ if ($objfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objfile) { } 658 20 678
425 #
426 # We are using object files. Update object file if necessary
427 # and load component from there.
428 #
429 # If loading the object file generates an error, or results in
430 # a non-component object, try regenerating the object file
431 # once before giving up and reporting an error. This can be
432 # handy in the rare case of an empty or corrupted object file.
433 # (But add an exception for "Compilation failed in require" errors, since
434 # the bad module will be added to %INC and the error will not occur
435 # the second time - RT #39803).
436 #
437 658 ▶ if ($objfilemod < $srcmod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objfilemod < $srcmod) 539 119 658
438 539 $self->compiler->compile_to_file( file => $objfile, source => $source);
439 }
440 642 $comp = eval { $self->eval_object_code( object_file => $objfile ) };
441  
442 642 ▶ if (!UNIVERSAL::isa($comp, 'HTML::Mason::Component')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($comp, "HTML::Mason::Component")) 15 627 642
443 15 ▶ if (!defined($@) || $@ !~ /failed in require/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $@ and $@ =~ /failed in require/) 13 2 15
Condition66%2/3
defined $@ and $@ =~ /failed in require/
!ll&&!rl&&r
0132
MC/DC0%0/2
defined $@ and $@ =~ /failed in require/
defined $@ $@ =~ /failed in require/
Truth table33%1/3
A and B
A= defined $@B= $@ =~ /failed in require/
ABresult
0X0
100
111
444 13 $self->compiler->compile_to_file( file => $objfile, source => $source);
445 13 $comp = eval { $self->eval_object_code( object_file => $objfile ) };
446 }
447  
448 15 ▶ if (!UNIVERSAL::isa($comp, 'HTML::Mason::Component')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($comp, "HTML::Mason::Component")) 10 5 15
449 10 ▶ my $error = $@ ? $@ : "Could not get HTML::Mason::Component object from object file '$objfile'";
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 10 0 10
450 10 $self->_compilation_error( $source->friendly_name, $error );
451 }
452 }
453 } else {
454 #
455 # Not using object files. Load component directly into memory.
456 #
457 20 my $object_code = $source->object_code( compiler => $self->compiler );
458 20 $comp = eval { $self->eval_object_code( object_code => $object_code ) };
459 20 ▶ $self->_compilation_error( $source->friendly_name, $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 18 20
460 }
461 650 $comp->assign_runtime_properties($self, $source);
462  
463 #
464 # Delete any stale cached version of this component, then
465 # cache it.
466 #
467 650 $self->delete_from_code_cache($comp_id);
468 650 $code_cache->{$comp_id} = { lastmod => $srcmod, comp => $comp };
469  
470 650 return $comp;
471 }
472  
473 sub delete_from_code_cache {
474 679 ▶ my ($self, $comp_id) = @_;
Subroutine delete_from_code_cache: called CC=2 SCAR=6.9
Pod undocumented
475 679 ▶ return unless defined $self->{code_cache}{$comp_id}{comp};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"code_cache"}{$comp_id}{"comp"} 649 30 679
476  
477 30 delete $self->{code_cache}{$comp_id};
478 30 return;
479 }
480  
481 sub comp_id_to_objfile {
482 661 ▶ my ($self, $comp_id) = @_;
Subroutine comp_id_to_objfile: called CC=1 SCAR=0.0
Pod undocumented
483  
484 661 return File::Spec->catfile
485 ( $self->object_dir,
486 $self->compiler->object_id,
487 ( split /\//, $comp_id ),
488 ) . $self->object_file_extension;
489 }
490  
491 #
492 # Empty in-memory code cache.
493 #
494 sub flush_code_cache {
495 11 ▶ my $self = shift;
Subroutine flush_code_cache: called CC=3 SCAR=11.0
Pod documented
496  
497 # Necessary for preventing memory leaks
498 11 ▶ if ($self->use_internal_component_caches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_internal_component_caches) 6 5 11
499 6 foreach my $entry (values %{$self->{code_cache}}) {
500 11 my $comp = $entry->{comp};
501 11 $comp->flush_internal_caches;
502 }
503 }
504 11 $self->{code_cache} = {};
505 11 $self->{source_cache} = {};
506 }
507  
508 #
509 # If code cache has exceeded maximum, remove least frequently used
510 # elements from cache until size falls below minimum.
511 #
512 sub purge_code_cache {
513 591 ▶ my ($self) = @_;
Subroutine purge_code_cache: called CC=7 SCAR=19.5
Pod documented
514  
515 591 ▶ return if $self->{unlimited_code_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"unlimited_code_cache"} 575 16 591
516 16 my $current_size = scalar(keys(%{$self->{code_cache}}));
517 16 ▶ if ($current_size > $self->code_cache_max_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_size > $self->code_cache_max_size) 9 7 16
518 9 my $code_cache = $self->{code_cache};
519 9 my $min_size = $self->code_cache_min_size;
520 9 my $decay_factor = 0.75;
521  
522 9 my @elems;
523 9 ▶ while (my ($path,$href) = each(%{$code_cache})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$code_cache;}) 38 9 47
524 38 push(@elems,[$path,$href->{comp}->mfu_count,$href->{comp}]);
525 }
526 9 @elems = sort { $a->[1] <=> $b->[1] } @elems;
527 9 ▶ while (($current_size > $min_size) and @elems) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_size > $min_size and @elems) 29 9 38
Condition66%2/3
$current_size > $min_size and @elems
!ll&&!rl&&r
9029
MC/DC50%1/2
$current_size > $min_size and @elems
$current_size > $min_size @elems
Truth table66%2/3
A and B
A= $current_size > $min_sizeB= @elems
ABresult
0X0
100
111
528 29 $self->delete_from_code_cache(shift(@elems)->[0]);
529 29 $current_size--;
530 }
531  
532 #
533 # Multiply each remaining cache item's count by a decay factor,
534 # to gradually reduce impact of old information.
535 #
536 9 foreach my $elem (@elems) {
537 9 $elem->[2]->mfu_count( $elem->[2]->mfu_count * $decay_factor );
538 }
539 }
540 }
541  
542 #
543 # Clear the object directory of all current files and subdirectories.
544 # Do this by renaming the object directory to a temporary name,
545 # immediately recreating an empty object directory, then removing
546 # the empty object directory. If another process tries to write
547 # the object file in between these steps, it'll create the top
548 # object directory instead.
549 #
550 # Would be nice to fork off a separate process to do the removing so
551 # that it doesn't affect a request's response time, but difficult to
552 # do this in an environment-generic way.
553 #
554 sub remove_object_files
555 {
556 1 ▶ my $self = shift;
Subroutine remove_object_files: called CC=3 SCAR=11.4
Pod undocumented
557  
558 1 my $object_dir = $self->object_dir;
559 1 ▶ if (-d $object_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $object_dir) { } 1 0 1
560 1 my $temp_dir = File::Temp::tempdir(DIR => $self->data_dir);
561 1 ▶ rename($object_dir, File::Spec->catdir( $temp_dir, 'target' ) )
Branch: 1 branch
BranchTrueFalseTotal
rename $object_dir, "File::Spec"->catdir($temp_dir, "target") or die "could not rename '${object_dir}' to '${temp_dir}': $@" 0 1 1
562 or die "could not rename '$object_dir' to '$temp_dir': $@";
563 1 $self->_make_object_dir();
564 1 rmtree($temp_dir);
565 } else {
566 0 $self->_make_object_dir();
567 }
568 }
569  
570 #
571 # Check the static_source_touch_file, if one exists, to see if it has
572 # changed since we last checked. If it has, clear the code cache and
573 # object files if appropriate.
574 #
575 sub check_static_source_touch_file
576 {
577 508 ▶ my $self = shift;
Subroutine check_static_source_touch_file: called CC=7 SCAR=20.1
Pod undocumented
578  
579 508 ▶ if (my $touch_file = $self->static_source_touch_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $touch_file = $self->static_source_touch_file) 3 505 508
580 3 ▶ return unless -f $touch_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $touch_file 2 1 3
581 1 my $touch_file_lastmod = (stat($touch_file))[9];
582 1 ▶ if ($touch_file_lastmod > $self->{static_source_touch_file_lastmod}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($touch_file_lastmod > $self->{"static_source_touch_file_lastmod"}) 1 0 1
583  
584 # File has been touched since we last checked. First,
585 # clear the object file directory if the last mod of
586 # its ._object_create_marker is earlier than the touch file,
587 # or if the marker doesn't exist.
588 #
589 1 ▶ if ($self->use_object_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_object_files) 1 0 1
590 1 my $object_create_marker_file = $self->object_create_marker_file;
591 1 ▶ if (!-e $object_create_marker_file ||
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $object_create_marker_file or (stat $object_create_marker_file)[9] < $touch_file_lastmod) 1 0 1
Condition33%1/3
not -e $object_create_marker_file or (stat $object_create_marker_file)[9] < $touch_file_lastmod
l!l&&r!l&&!r
010
MC/DC0%0/2
not -e $object_create_marker_file or (stat $object_create_marker_file)[9] < $touch_file_lastmod
not -e $object_create_marker_file (stat $object_create_marker_file)[9] < $touch_file_lastmod
Truth table33%1/3
not(A) or B
A= not -e $object_create_marker_fileB= (stat $object_create_marker_file)[9] < $touch_file_lastmod
ABresult
1X1
011
000
592 (stat($object_create_marker_file))[9] < $touch_file_lastmod) {
593 1 $self->remove_object_files;
594 }
595 }
596  
597 # Next, clear the in-memory component cache.
598 #
599 1 $self->flush_code_cache;
600  
601 # Reset lastmod value.
602 #
603 1 $self->{static_source_touch_file_lastmod} = $touch_file_lastmod;
604 }
605 }
606 }
607  
608 #
609 # Construct a component on the fly. Virtual if 'path' parameter is
610 # given, otherwise anonymous.
611 #
612 sub make_component {
613 15 ▶ my $self = shift;
Subroutine make_component: called CC=5 SCAR=16.2
Pod documented
614  
615 15 my %p = validate(@_, { comp_source => { type => SCALAR, optional => 1 },
616 comp_file => { type => SCALAR, optional => 1 },
617 name => { type => SCALAR, optional => 1 } });
618  
619 15 ▶ $p{comp_source} = read_file(delete $p{comp_file}) if exists $p{comp_file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"comp_file"} 0 15 15
620 param_error "Must specify either 'comp_source' or 'comp_file' parameter to 'make_component()'"
621 15 ▶ unless defined $p{comp_source};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p{"comp_source"} 0 15 15
622  
623 15 ▶ $p{name} ||= '<anonymous component>';
Condition100%2/2
$p{"name"} ||= "<anonymous component>"
l!l
114
MC/DC100%1/1
$p{"name"} ||= "<anonymous component>"
$p{"name"}
Truth table100%2/2
A
A= $p{"name"}
Aresult
11
00
624  
625 my $source = HTML::Mason::ComponentSource->new( friendly_name => $p{name},
626 comp_path => $p{name},
627 comp_id => undef,
628 last_modified => time,
629 comp_class => 'HTML::Mason::Component',
630 15 ▶ source_callback => sub { $p{comp_source} },
Subroutine __ANON__: called CC=1 SCAR=0.0
631 15 );
632  
633 15 my $object_code = $source->object_code( compiler => $self->compiler);
634  
635 13 my $comp = eval { $self->eval_object_code( object_code => $object_code ) };
636 13 ▶ $self->_compilation_error( $p{name}, $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
637  
638 13 $comp->assign_runtime_properties($self, $source);
639  
640 13 return $comp;
641 }
642  
643 sub set_global
644 {
645 8 ▶ my ($self, $decl, @values) = @_;
Subroutine set_global: called CC=5 SCAR=17.1
Pod documented
646 8 ▶ param_error "Interp->set_global: expects a variable name and one or more values"
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 8 8
647 unless @values;
648 8 ▶ my ($prefix, $name) = ($decl =~ s/^([\$@%])//) ? ($1, $decl) : ('$', $decl);
Branch: 1 branch
BranchTrueFalseTotal
$decl =~ s/^([\$\@%])// ? : 7 1 8
649  
650 8 my $varname = sprintf("%s::%s",$self->compiler->in_package,$name);
651 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
652 32 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
653 8 ▶ if ($prefix eq '$') {
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix eq '$') { } 8 0 8
elsif ($prefix eq '@') { } 0 0 0
654 8 $$varname = $values[0];
655 } elsif ($prefix eq '@') {
656 0 @$varname = @values;
657 } else {
658 0 %$varname = @values;
659 }
660 }
661  
662 sub comp_root
663 {
664 21 ▶ my $self = shift;
Subroutine comp_root: called CC=5 SCAR=16.1
Pod documented
665
666 21 ▶ if (my $new_comp_root = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_comp_root = shift()) 17 4 21
667 17 ▶ die "cannot assign new comp_root unless dynamic_comp_root parameter is set"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dynamic_comp_root 1 16 17
668 unless $self->dynamic_comp_root;
669 16 $self->_assign_comp_root($new_comp_root);
670 }
671 18 ▶ if (@{$self->{comp_root}} == 1 and $self->{comp_root}[0][0] eq 'MAIN') {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'comp_root'};} == 1 and $self->{'comp_root'}[0][0] eq 'MAIN') { } 4 14 18
Condition66%2/3
@{$self->{"comp_root"};} == 1 and $self->{"comp_root"}[0][0] eq "MAIN"
!ll&&!rl&&r
1404
MC/DC50%1/2
@{$self->{"comp_root"};} == 1 and $self->{"comp_root"}[0][0] eq "MAIN"
@{$self->{"comp_root"};} == 1 $self->{"comp_root"}[0][0] eq "MAIN"
Truth table66%2/3
A and B
A= @{$self->{"comp_root"};} == 1B= $self->{"comp_root"}[0][0] eq "MAIN"
ABresult
0X0
100
111
672 4 return $self->{comp_root}[0][1];
673 } else {
674 14 return $self->{comp_root};
675 }
676 }
677  
678 sub comp_root_array
679 {
680 2242 ▶ return @{ $_[0]->{comp_root} };
Subroutine comp_root_array: called CC=1 SCAR=0.0
Pod undocumented
681 }
682  
683 sub _assign_comp_root
684 {
685 430 ▶ my ($self, $new_comp_root) = @_;
Subroutine _assign_comp_root: called CC=10 SCAR=23.4
686  
687 # Force into lol format.
688 430 ▶ if (!ref($new_comp_root)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $new_comp_root) { } 411 19 430
elsif (ref $new_comp_root ne 'ARRAY') { } 0 19 19
689 411 $new_comp_root = [[ MAIN => $new_comp_root ]];
690 } elsif (ref($new_comp_root) ne 'ARRAY') {
691 0 die "Component root $new_comp_root must be a scalar or array reference";
692 }
693  
694 # Validate key/path pairs, and check to see if any of them
695 # conflict with old pairs.
696 430 ▶ my $comp_root_key_map = $self->{comp_root_key_map} ||= {};
Condition100%2/2
$self->{"comp_root_key_map"} ||= {}
l!l
16414
MC/DC100%1/1
$self->{"comp_root_key_map"} ||= {}
$self->{"comp_root_key_map"}
Truth table100%2/2
A
A= $self->{"comp_root_key_map"}
Aresult
11
00
697 430 foreach my $pair (@$new_comp_root) {
698 473 ▶ param_error "Multiple-path component root must consist of a list of two-element lists"
Branch: 1 branch
BranchTrueFalseTotal
if ref $pair ne "ARRAY" 0 473 473
699 if ref($pair) ne 'ARRAY';
700 473 ▶ param_error "Component root key '$pair->[0]' cannot contain slash"
Branch: 1 branch
BranchTrueFalseTotal
if $pair->[0] =~ m[/] 0 473 473
701 if $pair->[0] =~ /\//;
702 473 $pair->[1] = File::Spec->canonpath( $pair->[1] );
703 473 ▶ param_error "comp_root path '$pair->[1]' is not an absolute directory"
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($pair->[1]) 0 473 473
704 unless File::Spec->file_name_is_absolute( $pair->[1] );
705
706 473 my ($key, $path) = @$pair;
707 473 ▶ if (my $orig_path = $comp_root_key_map->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig_path = $comp_root_key_map->{$key}) { } 39 434 473
708 39 ▶ if ($path ne $orig_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path ne $orig_path) 2 37 39
709 2 die "comp_root key '$key' was originally associated with '$path', cannot change to '$orig_path'";
710 }
711 } else {
712 434 $comp_root_key_map->{$key} = $path;
713 }
714 }
715 430 $self->{comp_root} = $new_comp_root;
716 }
717  
718 sub resolve_comp_path_to_source
719 {
720 2305 ▶ my ($self, $path) = @_;
Subroutine resolve_comp_path_to_source: called CC=8 SCAR=20.8
Pod undocumented
721
722 2305 my $source;
723 2305 ▶ if ($self->{static_source}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'static_source'}) { } 73 2232 2305
724 # Maintain a separate source_cache for each component root,
725 # because the set of active component roots can change
726 # from request to request.
727 #
728 73 my $source_cache = $self->{source_cache};
729 73 foreach my $pair (@{$self->{comp_root}}) {
730 151 ▶ my $source_cache_for_root = $source_cache->{$pair->[0]} ||= {};
Condition100%2/2
$$source_cache{$pair->[0]} ||= {}
l!l
13318
MC/DC100%1/1
$$source_cache{$pair->[0]} ||= {}
$$source_cache{$pair->[0]}
Truth table100%2/2
A
A= $$source_cache{$pair->[0]}
Aresult
11
00
731 151 ▶ unless (exists($source_cache_for_root->{$path})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $source_cache_for_root->{$path}) 71 80 151
732 $source_cache_for_root->{$path}
733 71 = $self->{resolver}->get_info($path, @$pair);
734 }
735 151 ▶ last if $source = $source_cache_for_root->{$path};
Branch: 1 branch
BranchTrueFalseTotal
if $source = $source_cache_for_root->{$path} 33 118 151
736 }
737 } else {
738 2232 my $resolver = $self->{resolver};
739 2232 foreach my $pair ($self->comp_root_array) {
740 2322 ▶ last if $source = $resolver->get_info($path, @$pair);
Branch: 1 branch
BranchTrueFalseTotal
if $source = $resolver->get_info($path, @$pair) 1174 1148 2322
741 }
742 }
743 2305 return $source;
744 }
745  
746 sub files_written
747 {
748 0 ▶ my $self = shift;
Subroutine files_written: not called CC=1 SCAR=6.9
Pod undocumented
749 0 return @{$self->{files_written}};
750 }
751  
752 #
753 # Push onto list of written files.
754 #
755 sub push_files_written
756 {
757 86 ▶ my $self = shift;
Subroutine push_files_written: called CC=1 SCAR=0.0
Pod undocumented
758 86 my $fref = $self->{'files_written'};
759 86 push(@$fref,@_);
760 }
761  
762 #
763 # Look for component <$name> starting in <$startpath> and moving upwards
764 # to the root. Return component object or undef.
765 #
766 sub find_comp_upwards
767 {
768 562 ▶ my ($self, $startpath, $name) = @_;
Subroutine find_comp_upwards: called CC=3 SCAR=11.0
Pod undocumented
769 562 $startpath =~ s{/+$}{};
770  
771 # Don't use File::Spec here, this is a URL path.
772 562 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$startpath =~ s[/+[^/]*$][] and do { do { my $comp = $self->load("$startpath/$name"); return $comp if $comp } } 597 461 1058
773 1159 my $comp = $self->load("$startpath/$name");
774 1159 ▶ return $comp if $comp;
Branch: 1 branch
BranchTrueFalseTotal
if $comp 101 1058 1159
775 } while $startpath =~ s{/+[^/]*$}{};
776  
777 461 return; # Nothing found
778 }
779  
780 ###################################################################
781 # The eval_object_code & write_object_file methods used to be in
782 # Parser.pm. This is a temporary home only. They need to be moved
783 # again at some point in the future (during some sort of interp
784 # re-architecting).
785 ###################################################################
786  
787 #
788 # eval_object_code
789 # (object_code, object_file, error)
790 # Evaluate an object file or object text. Return a component object
791 # or undef if error.
792 #
793 # I think this belongs in the resolver (or comp loader) - Dave
794 #
795 sub eval_object_code
796 {
797 689 ▶ my ($self, %p) = @_;
Subroutine eval_object_code: called CC=5 SCAR=16.1
Pod undocumented
798  
799 #
800 # Evaluate object file or text with warnings on, unless
801 # ignore_warnings_expr is '.'.
802 #
803 689 my $ignore_expr = $self->ignore_warnings_expr;
804 689 my ($comp, $err);
805 689 my $warnstr = '';
806  
807 {
808 689 ▶ local $^W = $ignore_expr eq '.' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ignore_expr eq '.' ? : 1 688 689
809 local $SIG{__WARN__} =
810 ( $ignore_expr ?
811 ( $ignore_expr eq '.' ?
812 ▶ sub { } :
Subroutine __ANON__: not called CC=1 SCAR=0.0
813 6 ▶ sub { $warnstr .= $_[0] if $_[0] !~ /$ignore_expr/ }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if not $_[0] =~ /$ignore_expr/ 3 3 6
814 ) :
815 689 ▶ sub { $warnstr .= $_[0] } );
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 2 branches
BranchTrueFalseTotal
$ignore_expr ? : 689 0 689
$ignore_expr eq '.' ? : 1 688 689
816
817 689 $comp = $self->_do_or_eval(\%p);
818 }
819  
820 689 $err = $warnstr . $@;
821  
822 #
823 # Return component or error
824 #
825 689 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 22 667 689
826 # attempt to stem very long eval errors
827 22 $err =~ s/has too many errors\..+/has too many errors./s;
828 22 compilation_error $err;
829 } else {
830 667 return $comp;
831 }
832 }
833  
834 sub _do_or_eval
835 {
836 689 ▶ my ($self, $p) = @_;
Subroutine _do_or_eval: called CC=3 SCAR=11.0
837  
838 689 ▶ if ($p->{object_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{'object_file'}) { } 655 34 689
839 655 return do $p->{object_file};
840 } else {
841 # If in taint mode, untaint the object text
842 34 ▶ (${$p->{object_code}}) = ${$p->{object_code}} =~ /^(.*)/s if taint_is_on;
Branch: 1 branch
BranchTrueFalseTotal
if taint_is_on() 1 33 34
843  
844 34 ▶ return eval ${$p->{object_code}};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
845 }
846 }
847  
848 sub _compilation_error {
849 12 ▶ my ($self, $filename, $err) = @_;
Subroutine _compilation_error: called CC=1 SCAR=0.0
850  
851 12 HTML::Mason::Exception::Compilation->throw(error=>$err, filename=>$filename);
852 }
853  
854  
855 sub object_file {
856 3 ▶ my ($self, $comp) = @_;
Subroutine object_file: called CC=2 SCAR=7.2
Pod undocumented
857 3 ▶ return $comp->persistent ?
Branch: 1 branch
BranchTrueFalseTotal
$comp->persistent ? : 3 0 3
858 $self->comp_id_to_objfile($comp->comp_id) :
859 undef;
860 }
861  
862 sub use_autohandlers
863 {
864 764 ▶ my $self = shift;
Subroutine use_autohandlers: called CC=2 SCAR=7.1
Pod undocumented
865 764 ▶ return (defined $self->{autohandler_name} and length $self->{autohandler_name});
Condition66%2/3
defined $self->{'autohandler_name'} && length $self->{'autohandler_name'}
!ll&&!rl&&r
02762
MC/DC50%1/2
defined $self->{'autohandler_name'} && length $self->{'autohandler_name'}
defined $self->{'autohandler_name'} length $self->{'autohandler_name'}
Truth table66%2/3
A && B
A= defined $self->{'autohandler_name'}B= length $self->{'autohandler_name'}
ABresult
0X0
100
111
866 }
867  
868 # Generate HTML that describes Interp's current status.
869 # This is used in things like Apache::Status reports. Currently shows:
870 # -- Interp properties
871 # -- loaded (cached) components
872 sub status_as_html {
873 0 ▶ my ($self, %p) = @_;
Subroutine status_as_html: not called CC=1 SCAR=6.9
Pod undocumented
874  
875 # Should I be scared about this? =)
876  
877 0 my $comp_source = <<'EOF';
878 <h3>Interpreter properties:</h3>
879 <blockquote>
880 <h4>Startup options:</h4>
881 <tt>
882 <table width="100%">
883 <%perl>
884 foreach my $property (sort keys %$interp) {
885 my $val = $interp->{$property};
886  
887 my $default = ( defined $val && defined $valid{$property}{default} && $val eq $valid{$property}{default} ) || ( ! defined $val && exists $valid{$property}{default} && ! defined $valid{$property}{default} );
888  
889 my $display = $val;
890 if (ref $val) {
891 $display = '<font color="darkred">';
892 # only object can ->can, others die
893 my $is_object = eval { $val->can('anything'); 1 };
894 if ($is_object) {
895 $display .= ref $val . ' object';
896 } else {
897 if (UNIVERSAL::isa($val, 'ARRAY')) {
898 $display .= 'ARRAY reference - [ ';
899 $display .= join ', ', @$val;
900 $display .= '] ';
901 } elsif (UNIVERSAL::isa($val, 'HASH')) {
902 $display .= 'HASH reference - { ';
903 my @pairs;
904 while (my ($k, $v) = each %$val) {
905 push @pairs, "$k => $v";
906 }
907 $display .= join ', ', @pairs;
908 $display .= ' }';
909 } else {
910 $display = ref $val . ' reference';
911 }
912 }
913 $display .= '</font>';
914 }
915  
916 defined $display && $display =~ s,([\x00-\x1F]),'<font color="purple">control-' . chr( ord('A') + ord($1) - 1 ) . '</font>',eg; # does this work for non-ASCII?
917 </%perl>
918 <tr valign="top" cellspacing="10">
919 <td>
920 <% $property | h %>
921 </td>
922 <td>
923 <% defined $display ? $display : '<i>undef</i>' %>
924 <% $default ? '<font color=green>(default)</font>' : '' %>
925 </td>
926 </tr>
927 % }
928 </table>
929 </tt>
930  
931 <h4>Components in memory cache:</h4>
932 <tt>
933 % my $cache;
934 % if ($cache = $interp->code_cache and %$cache) {
935 % foreach my $key (sort keys %$cache) {
936 <% $key |h%> (modified <% scalar localtime $cache->{$key}->{lastmod} %>)
937 <br>
938 % }
939 % } else {
940 <I>None</I>
941 % }
942 </tt>
943 </blockquote>
944  
945 <%args>
946 $interp # The interpreter we'll elucidate
947 %valid # Default values for interp member data
948 </%args>
949 EOF
950  
951 0 my $comp = $self->make_component(comp_source => $comp_source);
952 0 my $out;
953  
954 0 my $args = [interp => $self, valid => $self->validation_spec];
955 0 $self->make_request(comp=>$comp, args=>$args, out_method=>\$out, %p)->exec;
956  
957 0 return $out;
958 }
959  
960 sub set_escape
961 {
962 832 ▶ my $self = shift;
Subroutine set_escape: called CC=8 SCAR=26.5
Pod documented
963 832 my %p = @_;
964  
965 832 ▶ while ( my ($name, $sub) = each %p )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %p) 832 831 1663
966 {
967 832 my $flag_regex = $self->compiler->lexer->escape_flag_regex;
968  
969 832 ▶ param_error "Invalid escape name ($name)"
Branch: 1 branch
BranchTrueFalseTotal
if not $name =~ /^$flag_regex$/ or $name =~ /^n$/ 1 831 832
Condition66%2/3
not $name =~ /^$flag_regex$/ or $name =~ /^n$/
l!l&&r!l&&!r
10831
MC/DC50%1/2
not $name =~ /^$flag_regex$/ or $name =~ /^n$/
not $name =~ /^$flag_regex$/ $name =~ /^n$/
Truth table66%2/3
not(A) or B
A= not $name =~ /^$flag_regex$/B= $name =~ /^n$/
ABresult
1X1
011
000
970 if $name !~ /^$flag_regex$/ || $name =~ /^n$/;
971  
972 831 my $coderef;
973 831 ▶ if ( ref $sub )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub) { } 831 0 831
974 {
975 831 $coderef = $sub;
976 }
977 else
978 {
979 0 ▶ if ( $sub =~ /^\w+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /^\w+$/) { } 0 0 0
980 {
981 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
982 0 ▶ unless ( defined &{"HTML::Mason::Escapes::$sub"} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{"HTML::Mason::Escapes::$sub";}) 0 0 0
983 {
984 0 param_error "Invalid escape: $sub (no matching subroutine in HTML::Mason::Escapes";
985 }
986  
987 0 $coderef = \&{"HTML::Mason::Escapes::$sub"};
988 }
989 else
990 {
991 0 $coderef = eval $sub;
992 0 ▶ param_error "Invalid escape: $sub ($@)" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
993 }
994 }
995  
996 831 $self->{escapes}{$name} = $coderef;
997 }
998 }
999  
1000 sub remove_escape
1001 {
1002 0 ▶ my $self = shift;
Subroutine remove_escape: not called CC=1 SCAR=6.9
Pod documented
1003  
1004 0 delete $self->{escapes}{ shift() };
1005 }
1006  
1007 sub apply_escapes
1008 {
1009 24 ▶ my $self = shift;
Subroutine apply_escapes: called CC=3 SCAR=11.0
Pod documented
1010 24 my $text = shift;
1011  
1012 24 foreach my $flag (@_)
1013 {
1014 param_error "Invalid escape flag: $flag"
1015 24 ▶ unless exists $self->{escapes}{$flag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"escapes"}{$flag} 1 23 24
1016  
1017 23 $self->{escapes}{$flag}->(\$text);
1018 }
1019  
1020 23 return $text;
1021 }
1022  
1023 32 1;
1024  
1025 __END__
« lib/HTML/Mason/FakeApache.pm ↑ Summary lib/HTML/Mason/Compiler.pm »