lib/HTML/Mason/Request.pm

filter: Statementstmt 96.4% 657 / 681 Branchbran 86.7% 300 / 346 Conditioncond 81.8% 81 / 99 MC/DCmcdc 67.8% 40 / 59 Subroutinesub 97.7% 86 / 88 Podpod 87.8% 36 / 41 totaltotal 91.3% 1200 / 1314 CC 222 SCAR 25.9
CC
222
Cov
92%
_cache_1_x
35.3
_initialize
25.6
exec
24.0
CRAP 13.3
« lib/HTML/Mason/CGIHandler.pm ↑ Summary lib/HTML/Mason/Lexer.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  
8 #
9 # A note about the internals:
10 #
11 # Because request is the single most intensively used piece of the
12 # Mason architecture, this module is often the best target for
13 # optimization.
14 #
15 # By far, the two methods called most often are comp() and print().
16 # We have attempted to optimize the parts of these methods that handle
17 # the _normal_ path through the code.
18 #
19 # Code paths that are followed less frequently (like the path that
20 # handles the $mods{store} parameter in comp, for example) are
21 # intentionally not optimized because doing so would clutter the code
22 # while providing a minimal benefit.
23 #
24 # Many of the optimizations consist of ignoring defined interfaces for
25 # accessing parts of the request object's internal data structure, and
26 # instead accessing it directly.
27 #
28 # We have attempted to comment these various optimizations
29 # appropriately, so that future hackers understand that we did indeed
30 # mean to not use the relevant interface in that particular spot.
31 #
32  
33 package HTML::Mason::Request;
34 32 $HTML::Mason::Request::VERSION = '1.60';
35 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 32 ▶use HTML::Mason::Cache::BaseCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 32 ▶use HTML::Mason::Plugin::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 32 ▶use HTML::Mason::Tools qw(can_weaken read_file compress_path load_pkg pkg_loaded absolute_comp_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 32 ▶use HTML::Mason::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 32 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 32 ▶use Class::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 32 ▶use base qw(Class::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 # Stack frame constants
48 32 ▶use constant STACK_COMP => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 32 ▶use constant STACK_ARGS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 32 ▶use constant STACK_BUFFER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 32 ▶use constant STACK_MODS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 32 ▶use constant STACK_PATH => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 32 ▶use constant STACK_BASE_COMP => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 32 ▶use constant STACK_IN_CALL_SELF => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 32 ▶use constant STACK_BUFFER_IS_FLUSHABLE => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 32 ▶use constant STACK_HIDDEN_BUFFER => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # HTML::Mason::Exceptions always exports rethrow_exception() and isa_mason_exception()
59 32 ▶use HTML::Mason::Exceptions( abbr => [qw(error param_error syntax_error
Subroutine BEGIN: called CC=1 SCAR=0.0
60 32 top_level_not_found_error error)] );
61  
62 32 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 0 ▶Params::Validate::validation_options( on_fail => sub { param_error( join '', @_ ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
64  
65 BEGIN
66 32 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
67 __PACKAGE__->valid_params
68 (
69 args =>
70 { type => ARRAYREF, default => [],
71 descr => "Array of arguments to initial component",
72 public => 0 },
73  
74 autoflush =>
75 { parse => 'boolean', default => 0, type => SCALAR,
76 descr => "Whether output should be buffered or sent immediately" },
77  
78 comp =>
79 { type => SCALAR | OBJECT, optional => 0,
80 descr => "Initial component, either an absolute path or a component object",
81 public => 0 },
82  
83 data_cache_api =>
84 { parse => 'string', default => '1.1', type => SCALAR,
85 regex => qr/^(?:1\.0|1\.1|chi)$/,
86 descr => "Data cache API to use: 1.0, 1.1, or chi" },
87  
88 data_cache_defaults =>
89 { parse => 'hash_list', type => HASHREF|UNDEF, optional => 1,
90 descr => "A hash of default parameters for Cache::Cache or CHI" },
91  
92 declined_comps =>
93 { type => HASHREF, optional => 1,
94 descr => "Hash of components that have been declined in previous parent requests",
95 public => 0 },
96  
97 dhandler_name =>
98 { parse => 'string', default => 'dhandler', type => SCALAR,
99 descr => "The filename to use for Mason's 'dhandler' capability" },
100  
101 interp =>
102 { isa => 'HTML::Mason::Interp',
103 descr => "An interpreter for Mason control functions",
104 public => 0 },
105  
106 error_format =>
107 { parse => 'string', type => SCALAR, default => 'text',
108 callbacks => { "HTML::Mason::Exception->can( method )'" =>
109 99 ▶ sub { HTML::Mason::Exception->can("as_$_[0]"); } },
Subroutine __ANON__: called CC=1 SCAR=0.0
110 descr => "How error conditions are returned to the caller (brief, text, line or html)" },
111  
112 error_mode =>
113 { parse => 'string', type => SCALAR, default => 'fatal',
114 regex => qr/^(?:output|fatal)$/,
115 descr => "How error conditions are manifest (output or fatal)" },
116  
117 component_error_handler =>
118 { parse => 'code', type => CODEREF|SCALAR, default => \&rethrow_exception,
119 descr => "A subroutine reference called on component compilation or runtime errors" },
120  
121 max_recurse =>
122 { parse => 'string', default => 32, type => SCALAR,
123 descr => "The maximum recursion depth for component, inheritance, and request stack" },
124  
125 out_method =>
126 { parse => 'code' ,type => CODEREF|SCALARREF,
127 6 ▶ default => sub { print STDOUT $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
128 32 descr => "A subroutine or scalar reference through which all output will pass" },
129  
130 # Only used when creating subrequests
131 parent_request =>
132 { isa => __PACKAGE__,
133 default => undef,
134 public => 0,
135 },
136  
137 plugins =>
138 { parse => 'list', default => [], type => ARRAYREF,
139 descr => 'List of plugin classes or objects to run hooks around components and requests' },
140  
141 # Only used when creating subrequests
142 request_depth =>
143 { type => SCALAR,
144 default => 1,
145 public => 0,
146 },
147  
148 );
149 }
150  
151 32 my @read_write_params;
152 32 ▶BEGIN { @read_write_params = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
153 autoflush
154 component_error_handler
155 data_cache_api
156 data_cache_defaults
157 dhandler_name
158 error_format
159 error_mode
160 max_recurse
161 out_method
162 ); }
163  
164 use HTML::Mason::MethodMaker
165 32 ▶ ( read_only => [ qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
166 count
167 dhandler_arg
168 initialized
169 interp
170 parent_request
171 plugin_instances
172 request_depth
173 request_comp
174 ) ],
175  
176 32 read_write => [ map { [ $_ => __PACKAGE__->validation_spec->{$_} ] }
177 @read_write_params ]
178 32 );
179  
180 84 ▶sub _properties { @read_write_params }
Subroutine _properties: called CC=1 SCAR=0.0
181  
182 sub new
183 506 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod documented
184 506 my $class = shift;
185 506 my $self = $class->SUPER::new(@_);
186  
187 # These are mandatory values for all requests.
188 #
189 506 %$self = (%$self,
190 dhandler_arg => undef,
191 execd => 0,
192 initialized => 0,
193 stack => [],
194 top_stack => undef,
195 wrapper_chain => undef,
196 wrapper_index => undef,
197 notes => {},
198 );
199  
200 506 $self->{request_comp} = delete($self->{comp});
201 506 $self->{request_args} = delete($self->{args});
202 506 ▶ if (UNIVERSAL::isa($self->{request_args}, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->{"request_args"}, "HASH")) 0 506 506
203 0 $self->{request_args} = [%{$self->{request_args}}];
204 }
205 506 $self->{count} = ++$self->{interp}{request_count};
206 506 ▶ if (ref($self->{out_method}) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"out_method"} eq "SCALAR") 17 489 506
207 17 my $bufref = $self->{out_method};
208 17 ▶ $self->{out_method} = sub { $$bufref .= $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
209 }
210 $self->{use_internal_component_caches} =
211 506 $self->{interp}->use_internal_component_caches;
212 506 $self->_initialize;
213  
214 471 return $self;
215 }
216  
217 # in the future this method may do something completely different but
218 # for now this works just fine.
219 29 ▶sub instance {
Subroutine instance: called CC=1 SCAR=0.0
Pod documented
220 29 return $HTML::Mason::Commands::m; #; this comment fixes a parsing bug in Emacs cperl-mode
221 }
222  
223 # Attempt to load each plugin module once per process
224 my %plugin_loaded;
225  
226 506 ▶sub _initialize {
Subroutine _initialize: called CC=25 SCAR=32.4
227 506 my ($self) = @_;
228  
229 506 ▶ local $SIG{'__DIE__'} = $self->component_error_handler
Branch: 1 branch
BranchTrueFalseTotal
if $self->component_error_handler 503 3 506
230 if $self->component_error_handler;
231  
232 506 eval {
233 # Check the static_source touch file, if it exists, before the
234 # first component is loaded.
235 #
236 506 $self->interp->check_static_source_touch_file();
237  
238 # request_comp can be an absolute path or component object. If a path,
239 # load into object.
240 506 my $request_comp = $self->{request_comp};
241 506 my ($path);
242 506 ▶ if (!ref($request_comp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $request_comp) { } 498 8 506
elsif (not UNIVERSAL::isa($request_comp, 'HTML::Mason::Component')) { } 0 8 8
243 498 $request_comp =~ s{/+}{/}g;
244 498 $self->{top_path} = $path = $request_comp;
245 $log->debugf("top path is '%s'", $self->{top_path})
246 498 ▶ if $log->is_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Request::log->is_debug 1 497 498
247  
248 498 my $retry_count = 0;
249 search: {
250 498 $request_comp = $self->interp->load($path);
251  
252 482 ▶ last search unless $self->use_dhandlers;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->use_dhandlers 2 480 482
253  
254 # If path was not found, check for dhandler.
255 480 ▶ unless ($request_comp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request_comp) 26 454 480
256 26 ▶ if ( $request_comp = $self->interp->find_comp_upwards($path, $self->dhandler_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_comp = $self->interp->find_comp_upwards($path, $self->dhandler_name)) 21 5 26
257 21 my $parent_path = $request_comp->dir_path;
258 21 ($self->{dhandler_arg} = $self->{top_path}) =~ s{^$parent_path/?}{};
259 $log->debugf("found dhandler '%s', dhandler_arg '%s'", $parent_path, $self->{dhandler_arg})
260 21 ▶ if $log->is_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Request::log->is_debug 0 21 21
261 }
262 }
263  
264 # If the component was declined previously in this
265 # request, look for the next dhandler up the
266 # tree.
267 480 ▶ if ($request_comp and $self->{declined_comps}->{$request_comp->comp_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_comp and $self->{"declined_comps"}{$request_comp->comp_id}) 13 467 480
Condition100%3/3
$request_comp and $self->{"declined_comps"}{$request_comp->comp_id}
!ll&&!rl&&r
546213
MC/DC100%2/2
$request_comp and $self->{"declined_comps"}{$request_comp->comp_id}
$request_comp $self->{"declined_comps"}{$request_comp->comp_id}
Truth table100%3/3
A and B
A= $request_compB= $self->{"declined_comps"}{$request_comp->comp_id}
ABresult
0X0
100
111
268 13 $path = $request_comp->dir_path;
269 13 ▶ if ($request_comp->name eq $self->dhandler_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_comp->name eq $self->dhandler_name) 6 7 13
270 6 ▶ if ($path eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '/') { } 1 5 6
271 1 undef $request_comp;
272 1 last search; # End search if /dhandler declined
273 } else {
274 5 $path =~ s:/[^\/]+$::;
275 5 ▶ $path ||= '/';
Condition100%2/2
$path ||= "/"
l!l
23
MC/DC100%1/1
$path ||= "/"
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
276 }
277 }
278 12 ▶ if ($retry_count++ > $self->max_recurse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry_count++ > $self->max_recurse) 0 12 12
279 0 error "could not find dhandler after " . $self->max_recurse . " tries (infinite loop bug?)";
280 }
281 12 redo search;
282 }
283 }
284  
285 470 ▶ unless ($self->{request_comp} = $request_comp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"request_comp"} = $request_comp) 7 463 470
286 top_level_not_found_error "could not find component for initial path '$self->{top_path}' " .
287 "(component roots are: " .
288 7 join(", ", map { "'" . $_->[1] . "'" } $self->{interp}->comp_root_array) .
289 ")";
290 }
291  
292 } elsif ( ! UNIVERSAL::isa( $request_comp, 'HTML::Mason::Component' ) ) {
293 0 param_error "comp ($request_comp) must be a component path or a component object";
294 }
295  
296 # Construct a plugin instance for each plugin class in each request.
297 #
298 471 $self->{has_plugins} = 0;
299 471 $self->{plugin_instances} = [];
300 471 foreach my $plugin (@{ delete $self->{plugins} }) {
301 25 $self->{has_plugins} = 1;
302 25 my $plugin_instance = $plugin;
303 25 ▶ unless (ref $plugin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $plugin) 23 2 25
304  
305 # Load information about each plugin class once per
306 # process. Right now the only information we need is
307 # whether there is a new() method.
308 #
309 23 ▶ unless ($plugin_loaded{$plugin}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin_loaded{$plugin}) 13 10 23
310 # Load plugin package if it isn't already loaded.
311 #
312 {
313 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
314 13 ▶ unless (keys %{$plugin . "::"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$plugin . '::';}) 0 13 13
315 0 eval "use $plugin;";
316 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
317 }
318 }
319 13 $plugin_loaded{$plugin} = 1;
320 }
321 23 $plugin_instance = $plugin->new();
322 }
323 25 push @{$self->{plugin_instances}}, $plugin_instance;
324 }
325 471 $self->{plugin_instances_reverse} = [reverse(@{$self->{plugin_instances}})];
326  
327 # Check for autoflush and !enable_autoflush
328 #
329 471 ▶ if ($self->{autoflush} && !$self->interp->compiler->enable_autoflush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autoflush"} and not $self->interp->compiler->enable_autoflush) 1 470 471
Condition100%3/3
$self->{"autoflush"} and not $self->interp->compiler->enable_autoflush
!ll&&!rl&&r
458121
MC/DC100%2/2
$self->{"autoflush"} and not $self->interp->compiler->enable_autoflush
$self->{"autoflush"} not $self->interp->compiler->enable_autoflush
Truth table100%3/3
A and not(B)
A= $self->{"autoflush"}B= not $self->interp->compiler->enable_autoflush
ABresult
0X0
100
111
330 1 die "Cannot use autoflush unless enable_autoflush is set";
331 }
332  
333 };
334  
335 506 my $err = $@;
336 506 ▶ if ($err and !$self->_aborted_or_declined($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and not $self->_aborted_or_declined($err)) { } 36 470 506
Condition66%2/3
$err and not $self->_aborted_or_declined($err)
!ll&&!rl&&r
470036
MC/DC50%1/2
$err and not $self->_aborted_or_declined($err)
$err not $self->_aborted_or_declined($err)
Truth table66%2/3
A and not(B)
A= $errB= not $self->_aborted_or_declined($err)
ABresult
0X0
100
111
337 36 $self->_handle_error($err);
338 } else {
339 470 $self->{initialized} = 1;
340 }
341 }
342  
343 sub use_dhandlers
344 483 ▶{
Subroutine use_dhandlers: called CC=2 SCAR=7.1
Pod undocumented
345 483 my $self = shift;
346 483 ▶ return (defined $self->{dhandler_name} and length $self->{dhandler_name});
Condition66%2/3
defined $self->{'dhandler_name'} && length $self->{'dhandler_name'}
!ll&&!rl&&r
03480
MC/DC50%1/2
defined $self->{'dhandler_name'} && length $self->{'dhandler_name'}
defined $self->{'dhandler_name'} length $self->{'dhandler_name'}
Truth table66%2/3
A && B
A= defined $self->{'dhandler_name'}B= length $self->{'dhandler_name'}
ABresult
0X0
100
111
347 }
348  
349 sub alter_superclass
350 2 ▶{
Subroutine alter_superclass: called CC=4 SCAR=13.9
Pod undocumented
351 2 my $self = shift;
352 2 my $new_super = shift;
353  
354 2 my $class = caller;
355  
356 2 my $isa_ref;
357 {
358 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
359 2 my @isa = @{ $class . '::ISA' };
360 2 $isa_ref = \@isa;
361 }
362  
363 # handles multiple inheritance properly and preserve
364 # inheritance order
365 2 ▶ for ( my $x = 0; $x <= $#{$isa_ref} ; $x++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= $#{$isa_ref;}) 2 0 2
366 {
367 2 ▶ if ( $isa_ref->[$x]->isa('HTML::Mason::Request') )
Branch: 1 branch
BranchTrueFalseTotal
if ($isa_ref->[$x]->isa("HTML::Mason::Request")) 2 0 2
368 {
369 2 my $old_super = $isa_ref->[$x];
370  
371 2 ▶ if ( $old_super ne $new_super )
Branch: 1 branch
BranchTrueFalseTotal
if ($old_super ne $new_super) 0 2 2
372 {
373 0 $isa_ref->[$x] = $new_super;
374 }
375  
376 2 last;
377 }
378 }
379  
380 {
381 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
382 2 @{ $class . '::ISA' } = @{ $isa_ref };
383 }
384  
385 2 $class->valid_params( %{ $class->valid_params } );
386 }
387  
388 470 ▶sub exec {
Subroutine exec: called CC=24 SCAR=31.8
Pod documented
389 470 my ($self) = @_;
390  
391 # If the request failed to initialize, the error has already been handled
392 # at the bottom of _initialize(); just return.
393 470 ▶ return unless $self->initialized();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->initialized 1 469 470
394  
395 469 ▶ local $SIG{'__DIE__'} = $self->component_error_handler
Branch: 1 branch
BranchTrueFalseTotal
if $self->component_error_handler 466 3 469
396 if $self->component_error_handler;
397  
398 # Cheap way to prevent users from executing the same request twice.
399 #
400 469 ▶ if ($self->{execd}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"execd"}++) 1 468 469
401 1 error "Can only call exec() once for a given request object. Did you want to use a subrequest?";
402 }
403  
404 # Check for infinite subrequest loop.
405 #
406 468 ▶ error "subrequest depth > " . $self->max_recurse . " (infinite subrequest loop?)"
Branch: 1 branch
BranchTrueFalseTotal
if $self->request_depth > $self->max_recurse 1 467 468
407 if $self->request_depth > $self->max_recurse;
408  
409 #
410 # $m is a dynamically scoped global containing this
411 # request. This needs to be defined in the HTML::Mason::Commands
412 # package, as well as the component package if that is different.
413 #
414 467 local $HTML::Mason::Commands::m = $self;
415  
416 # Dynamically scoped global pointing at the top of the request stack.
417 #
418 467 $self->{top_stack} = undef;
419  
420 # Save context of subroutine for use inside eval.
421 467 my $wantarray = wantarray;
422 467 my @result;
423  
424 # Initialize output buffer to interpreter's preallocated buffer
425 # before clearing, to reduce memory reallocations.
426 #
427 467 $self->{request_buffer} = $self->interp->preallocated_output_buffer;
428 467 $self->{request_buffer} = '';
429  
430 467 ▶ $log->debugf("starting request for '%s'", $self->request_comp->title)
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Request::log->is_debug 1 466 467
431 if $log->is_debug;
432  
433 467 eval {
434 # Build wrapper chain and index.
435 467 my $request_comp = $self->request_comp;
436 467 my $first_comp;
437 {
438 467 my @wrapper_chain = ($request_comp);
439  
440 467 ▶ for (my $parent = $request_comp->parent; $parent; $parent = $parent->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 26 467 493
441 26 unshift(@wrapper_chain,$parent);
442 26 ▶ error "inheritance chain length > " . $self->max_recurse . " (infinite inheritance loop?)"
Branch: 1 branch
BranchTrueFalseTotal
if @wrapper_chain > $self->max_recurse 0 26 26
443 if (@wrapper_chain > $self->max_recurse);
444 }
445  
446 467 $first_comp = $wrapper_chain[0];
447 467 $self->{wrapper_chain} = [@wrapper_chain];
448 $self->{wrapper_index} = { map
449 467 { $wrapper_chain[$_]->comp_id => $_ }
450 (0..$#wrapper_chain)
451 };
452 }
453  
454 # Get original request_args array reference to avoid copying.
455 467 my $request_args = $self->{request_args};
456 {
457 467 local *SELECTED;
458 467 tie *SELECTED, 'Tie::Handle::Mason';
459  
460 467 my $old = select SELECTED;
461 467 my $mods = {base_comp => $request_comp, store => \($self->{request_buffer}), flushable => 1};
462  
463 467 ▶ if ($self->{has_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_plugins"}) 20 447 467
464 20 my $context = bless
465 [$self, $request_args],
466 'HTML::Mason::Plugin::Context::StartRequest';
467 20 eval {
468 20 foreach my $plugin_instance (@{$self->plugin_instances}) {
469 25 $plugin_instance->start_request_hook( $context );
470 }
471 };
472 20 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 19 20
473 1 select $old;
474 1 rethrow_exception $@;
475 }
476 }
477  
478 466 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 2 464 466
elsif (defined $wantarray) { } 17 447 464
479 2 @result = eval {$self->comp($mods, $first_comp, @$request_args)};
480 } elsif (defined($wantarray)) {
481 17 $result[0] = eval {$self->comp($mods, $first_comp, @$request_args)};
482 } else {
483 447 eval {$self->comp($mods, $first_comp, @$request_args)};
484 }
485
486 466 my $error = $@;
487  
488 466 ▶ if ($self->{has_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_plugins"}) 19 447 466
489 # plugins called in reverse order when exiting.
490 my $context = bless
491 19 [$self, $request_args, \$self->{request_buffer}, $wantarray, \@result, \$error],
492 'HTML::Mason::Plugin::Context::EndRequest';
493 19 eval {
494 19 foreach my $plugin_instance (@{$self->{plugin_instances_reverse}}) {
495 24 $plugin_instance->end_request_hook( $context );
496 }
497 };
498 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 18 19
499 # plugin errors take precedence over component errors
500 1 $error = $@;
501 }
502 }
503
504 466 select $old;
505 466 rethrow_exception $error;
506 }
507 };
508  
509 467 ▶ $log->debugf("finishing request for '%s'", $self->request_comp->title)
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Mason::Request::log->is_debug 1 466 467
510 if $log->is_debug;
511  
512 # Purge code cache if necessary.
513 467 $self->interp->purge_code_cache;
514  
515 # Handle errors.
516 467 my $err = $@;
517 467 ▶ if ($err and !$self->_aborted_or_declined($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and not $self->_aborted_or_declined($err)) 84 383 467
Condition100%3/3
$err and not $self->_aborted_or_declined($err)
!ll&&!rl&&r
3701384
MC/DC100%2/2
$err and not $self->_aborted_or_declined($err)
$err not $self->_aborted_or_declined($err)
Truth table100%3/3
A and not(B)
A= $errB= not $self->_aborted_or_declined($err)
ABresult
0X0
100
111
518 84 $self->_handle_error($err);
519 6 return;
520 }
521  
522 # If there's anything in the output buffer, send it to out_method.
523 # Otherwise skip out_method call to avoid triggering side effects
524 # (e.g. HTTP header sending).
525 383 ▶ if (length($self->{request_buffer}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"request_buffer"} > 0) 360 23 383
526 360 $self->out_method->($self->{request_buffer});
527 }
528  
529 # Return aborted value or result.
530 383 ▶ @result = ($err->aborted_value) if $self->aborted($err);
Branch: 1 branch
BranchTrueFalseTotal
if $self->aborted($err) 5 378 383
531 383 ▶ @result = ($err->declined_value) if $self->declined($err);
Branch: 1 branch
BranchTrueFalseTotal
if $self->declined($err) 8 375 383
532 383 ▶ return $wantarray ? @result : defined($wantarray) ? $result[0] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$wantarray ? : 2 381 383
defined $wantarray ? : 15 366 381
533 }
534  
535 #
536 # Display or die with error as dictated by error_mode and error_format.
537 #
538 sub _handle_error
539 120 ▶{
Subroutine _handle_error: called CC=6 SCAR=18.5
540 120 my ($self, $err) = @_;
541  
542 120 $self->interp->purge_code_cache;
543  
544 120 ▶ rethrow_exception $err if $self->is_subrequest;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_subrequest 40 80 120
545  
546 # Set error format for when error is stringified.
547 80 ▶ if (UNIVERSAL::can($err, 'format')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($err, "format")) 79 1 80
548 79 $err->format($self->error_format);
549 }
550  
551 # In fatal mode, die with error. In display mode, output stringified error.
552 80 ▶ if ($self->error_mode eq 'fatal') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error_mode eq 'fatal') { } 73 7 80
553 73 rethrow_exception $err;
554 } else {
555 7 ▶ if ( UNIVERSAL::isa( $self->out_method, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($self->out_method, 'CODE')) { } 7 0 7
556 # This may not be set if an error occurred in
557 # _initialize(), for example with a compilation error.
558 # But the output method may rely on being able to get at
559 # the request object. This is a nasty code smell but
560 # fixing it properly is probably out of scope.
561 #
562 # Previously this method could only be called from exec().
563 #
564 # Without this one of the tests in 16-live_cgi.t was
565 # failing.
566 7 ▶ local $HTML::Mason::Commands::m ||= $self;
Condition33%1/3
local $HTML::Mason::Commands::m ||= $self
l!l&&r!l&&!r
070
MC/DC0%0/2
local $HTML::Mason::Commands::m ||= $self
local $HTML::Mason::Commands::m $self
Truth table33%1/3
A ||= B
A= local $HTML::Mason::Commands::mB= $self
ABresult
1X1
011
000
567 7 $self->out_method->("$err");
568 } else {
569 0 ${ $self->out_method } = "$err";
570 }
571 }
572 }
573  
574 sub subexec
575 67 ▶{
Subroutine subexec: called CC=1 SCAR=0.0
Pod documented
576 67 my $self = shift;
577 67 my $comp = shift;
578  
579 67 $self->make_subrequest(comp=>$comp, args=>\@_)->exec;
580 }
581  
582 sub make_subrequest
583 84 ▶{
Subroutine make_subrequest: called CC=4 SCAR=13.9
Pod documented
584 84 my ($self, %params) = @_;
585 84 my $interp = $self->interp;
586  
587 # Coerce a string 'comp' parameter into an absolute path. Don't
588 # create it if it's missing, though - it's required, but for
589 # consistency we let exceptions be thrown later.
590 $params{comp} = absolute_comp_path($params{comp}, $self->current_comp->dir_path)
591 84 ▶ if exists $params{comp} && !ref($params{comp});
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"comp"} and not ref $params{"comp"} 82 2 84
Condition66%2/3
exists $params{"comp"} and not ref $params{"comp"}
!ll&&!rl&&r
0282
MC/DC50%1/2
exists $params{"comp"} and not ref $params{"comp"}
exists $params{"comp"} not ref $params{"comp"}
Truth table66%2/3
A and not(B)
A= exists $params{"comp"}B= not ref $params{"comp"}
ABresult
0X0
100
111
592  
593 # Give subrequest the same values as parent request for read/write params
594 84 my %defaults = map { ($_, $self->$_()) } $self->_properties;
595  
596 84 ▶ unless ( $params{out_method} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"out_method"}) 82 2 84
597 {
598 37 ▶ $defaults{out_method} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
599 37 $self->print($_[0]);
600 82 };
601 }
602  
603 # Make subrequest, and set parent_request and request_depth appropriately.
604 84 my $subreq =
605 $interp->make_request(%defaults, %params,
606 parent_request => $self,
607 request_depth => $self->request_depth + 1);
608  
609 79 return $subreq;
610 }
611  
612 sub is_subrequest
613 129 ▶{
Subroutine is_subrequest: called CC=2 SCAR=6.9
Pod undocumented
614 129 my ($self) = @_;
615  
616 129 ▶ return $self->parent_request ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->parent_request ? : 41 88 129
617 }
618  
619 sub clear_and_abort
620 1 ▶{
Subroutine clear_and_abort: called CC=1 SCAR=0.0
Pod documented
621 1 my $self = shift;
622  
623 1 $self->clear_buffer;
624 1 $self->abort(@_);
625 }
626  
627 sub abort
628 13 ▶{
Subroutine abort: called CC=1 SCAR=0.0
Pod documented
629 13 my ($self, $aborted_value) = @_;
630 13 HTML::Mason::Exception::Abort->throw( error => 'Request->abort was called', aborted_value => $aborted_value );
631 }
632  
633 #
634 # Determine whether $err (or $@ by default) is an Abort exception.
635 #
636 556 ▶sub aborted {
Subroutine aborted: called CC=2 SCAR=6.9
Pod documented
637 556 my ($self, $err) = @_;
638 556 ▶ $err = $@ if !defined($err);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 30 526 556
639 556 return isa_mason_exception( $err, 'Abort' );
640 }
641  
642 #
643 # Determine whether $err (or $@ by default) is an Decline exception.
644 #
645 544 ▶sub declined {
Subroutine declined: called CC=2 SCAR=6.9
Pod documented
646 544 my ($self, $err) = @_;
647 544 ▶ $err = $@ if !defined($err);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 27 517 544
648 544 return isa_mason_exception( $err, 'Decline' );
649 }
650  
651 168 ▶sub _aborted_or_declined {
Subroutine _aborted_or_declined: called CC=2 SCAR=6.9
652 168 my ($self, $err) = @_;
653 168 ▶ return $self->aborted($err) || $self->declined($err);
Condition100%3/3
$self->aborted($err) || $self->declined($err)
l!l&&r!l&&!r
710151
MC/DC100%2/2
$self->aborted($err) || $self->declined($err)
$self->aborted($err) $self->declined($err)
Truth table100%3/3
A || B
A= $self->aborted($err)B= $self->declined($err)
ABresult
1X1
011
000
654 }
655  
656 #
657 # Return a new cache object specific to this component.
658 #
659 sub cache
660 136 ▶{
Subroutine cache: called CC=20 SCAR=30.5
Pod documented
661 136 my ($self, %options) = @_;
662  
663 # If using 1.0x cache API, save off options for end of routine.
664 136 my %old_cache_options;
665 136 ▶ if ($self->data_cache_api eq '1.0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_cache_api eq "1.0") 30 106 136
666 30 %old_cache_options = %options;
667 30 %options = ();
668 }
669  
670 # Combine defaults with options passed in here.
671 136 ▶ if ($self->data_cache_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_cache_defaults) 5 131 136
672 5 %options = (%{$self->data_cache_defaults}, %options);
673 }
674  
675 # If using the CHI API, just create and return a CHI handle. Namespace will be escaped by CHI.
676 136 ▶ if ($self->data_cache_api eq 'chi') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_cache_api eq "chi") 54 82 136
677 54 ▶ my $chi_root_class = delete($options{chi_root_class}) || 'CHI';
Condition50%1/2
delete $options{'chi_root_class'} || 'CHI'
l!l
054
MC/DC0%0/1
delete $options{'chi_root_class'} || 'CHI'
delete $options{'chi_root_class'}
Truth table50%1/2
A
A= delete $options{'chi_root_class'}
Aresult
11
00
678 54 load_pkg($chi_root_class);
679 54 ▶ if (!exists($options{namespace})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"namespace"}) 54 0 54
680 54 $options{namespace} = $self->current_comp->comp_id;
681 }
682 54 ▶ if (!exists($options{driver}) && !exists($options{driver_class})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $options{"driver"} or exists $options{"driver_class"}) 49 5 54
Condition66%2/3
exists $options{"driver"} or exists $options{"driver_class"}
l!l&&r!l&&!r
5049
MC/DC50%1/2
exists $options{"driver"} or exists $options{"driver_class"}
exists $options{"driver"} exists $options{"driver_class"}
Truth table66%2/3
A or B
A= exists $options{"driver"}B= exists $options{"driver_class"}
ABresult
1X1
011
000
683 49 ▶ $options{driver} = $self->interp->cache_dir ? 'File' : 'Memory';
Branch: 1 branch
BranchTrueFalseTotal
$self->interp->cache_dir ? : 49 0 49
684 49 ▶ $options{global} = 1 if $options{driver} eq 'Memory';
Branch: 1 branch
BranchTrueFalseTotal
if $options{"driver"} eq "Memory" 0 49 49
685 }
686 54 ▶ $options{root_dir} ||= $self->interp->cache_dir;
Condition33%1/3
$options{"root_dir"} ||= $self->interp->cache_dir
l!l&&r!l&&!r
0540
MC/DC0%0/2
$options{"root_dir"} ||= $self->interp->cache_dir
$options{"root_dir"} $self->interp->cache_dir
Truth table33%1/3
A ||= B
A= $options{"root_dir"}B= $self->interp->cache_dir
ABresult
1X1
011
000
687 54 return $chi_root_class->new(%options);
688 }
689  
690 82 ▶ $options{cache_root} ||= $self->interp->cache_dir;
Condition66%2/3
$options{"cache_root"} ||= $self->interp->cache_dir
l!l&&r!l&&!r
0811
MC/DC50%1/2
$options{"cache_root"} ||= $self->interp->cache_dir
$options{"cache_root"} $self->interp->cache_dir
Truth table66%2/3
A ||= B
A= $options{"cache_root"}B= $self->interp->cache_dir
ABresult
1X1
011
000
691 82 ▶ $options{namespace} ||= compress_path($self->current_comp->comp_id);
Condition33%1/3
$options{"namespace"} ||= compress_path($self->current_comp->comp_id)
l!l&&r!l&&!r
0820
MC/DC0%0/2
$options{"namespace"} ||= compress_path($self->current_comp->comp_id)
$options{"namespace"} compress_path($self->current_comp->comp_id)
Truth table33%1/3
A ||= B
A= $options{"namespace"}B= compress_path($self->current_comp->comp_id)
ABresult
1X1
011
000
692  
693 # Determine cache_class, adding 'Cache::' in front of user's
694 # specification if necessary.
695 82 ▶ my $cache_class = $self->interp->cache_dir ? 'Cache::FileCache' : 'Cache::MemoryCache';
Branch: 1 branch
BranchTrueFalseTotal
$self->interp->cache_dir ? : 81 1 82
696 82 ▶ if ($options{cache_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"cache_class"}) 2 80 82
697 2 $cache_class = $options{cache_class};
698 2 ▶ $cache_class = "Cache::$cache_class" unless $cache_class =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_class =~ /::/ 1 1 2
699 2 delete($options{cache_class});
700 }
701  
702 # Now prefix cache class with "HTML::Mason::". This will be a
703 # dynamically constructed package that simply inherits from
704 # HTML::Mason::Cache::BaseCache and the chosen cache class.
705 82 my $mason_cache_class = "HTML::Mason::$cache_class";
706 82 ▶ unless (pkg_loaded($mason_cache_class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (pkg_loaded($mason_cache_class)) 4 78 82
707 4 load_pkg('Cache::Cache', '$m->cache requires the Cache::Cache module, available from CPAN.');
708 4 load_pkg($cache_class, 'Fix your Cache::Cache installation or choose another cache class.');
709 # need to break up mention of VERSION var or else CPAN/EU::MM can choke when running 'r'
710 4 ▶ eval sprintf('package %s; use base qw(HTML::Mason::Cache::BaseCache %s); use vars qw($' . 'VERSION); $' . 'VERSION = 1.0;',
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
711 $mason_cache_class, $cache_class);
712 4 ▶ error "Error constructing mason cache class $mason_cache_class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
713 }
714  
715 82 ▶ my $cache = $mason_cache_class->new (\%options)
Branch: 1 branch
BranchTrueFalseTotal
my $cache = $mason_cache_class->new(\%options) or error("could not create cache object") 82 0 82
716 or error "could not create cache object";
717  
718 # Implement 1.0x cache API or just return cache object.
719 82 ▶ if ($self->data_cache_api eq '1.0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_cache_api eq '1.0') { } 30 52 82
720 30 return $self->_cache_1_x($cache, %old_cache_options);
721 } else {
722 52 return $cache;
723 }
724 }
725  
726 #
727 # Implement 1.0x cache API in terms of Cache::Cache.
728 # Supported: action, busy_lock, expire_at, expire_if, expire_in, expire_next, key, value
729 # Silently not supported: keep_in_memory, tie_class
730 #
731 sub _cache_1_x
732 30 ▶{
Subroutine _cache_1_x: called CC=23 SCAR=35.6
733 30 my ($self, $cache, %options) = @_;
734  
735 30 ▶ my $action = $options{action} || 'retrieve';
Condition100%2/2
$options{'action'} || 'retrieve'
l!l
1020
MC/DC100%1/1
$options{'action'} || 'retrieve'
$options{'action'}
Truth table100%2/2
A
A= $options{'action'}
Aresult
11
00
736 30 ▶ my $key = $options{key} || 'main';
Condition100%2/2
$options{'key'} || 'main'
l!l
1812
MC/DC100%1/1
$options{'key'} || 'main'
$options{'key'}
Truth table100%2/2
A
A= $options{'key'}
Aresult
11
00
737
738 30 ▶ if ($action eq 'retrieve') {
Branch: 4 branches
BranchTrueFalseTotal
if ($action eq 'retrieve') { } 20 10 30
elsif ($action eq 'store') { } 8 2 10
elsif ($action eq 'expire') { } 1 1 2
elsif ($action eq 'keys') { } 1 0 1
739
740 # Validate parameters.
741 20 ▶ if (my @invalids = grep(!/^(expire_if|action|key|busy_lock|keep_in_memory|tie_class)$/, keys(%options))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@invalids) = grep((!/^(expire_if|action|key|busy_lock|keep_in_memory|tie_class)$/), keys %options)) 0 20 20
742 0 param_error "cache: invalid parameter '$invalids[0]' for action '$action'\n";
743 }
744  
745 # Handle expire_if.
746 20 ▶ if (my $sub = $options{expire_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $options{"expire_if"}) 2 18 20
747 2 ▶ if (my $obj = $cache->get_object($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $cache->get_object($key)) 2 0 2
748 2 ▶ if ($sub->($obj->get_created_at)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$sub($obj->get_created_at)) 1 1 2
749 1 $cache->expire($key);
750 }
751 }
752 }
753  
754 # Return the value or undef, handling busy_lock.
755 20 ▶ if (my $result = $cache->get($key, ($options{busy_lock} ? (busy_lock=>$options{busy_lock}) : ()))) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $result = $cache->get($key, $options{'busy_lock'} ? ('busy_lock', $options{'busy_lock'}) : ())) { } 11 9 20
$options{'busy_lock'} ? : 2 18 20
756 11 return $result;
757 } else {
758 9 return undef;
759 }
760  
761 } elsif ($action eq 'store') {
762  
763 # Validate parameters
764 8 ▶ if (my @invalids = grep(!/^(expire_(at|next|in)|action|key|value|keep_in_memory|tie_class)$/, keys(%options))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@invalids) = grep((!/^(expire_(at|next|in)|action|key|value|keep_in_memory|tie_class)$/), keys %options)) 0 8 8
765 0 param_error "cache: invalid parameter '$invalids[0]' for action '$action'\n";
766 }
767 8 ▶ param_error "cache: no store value provided" unless exists($options{value});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"value"} 0 8 8
768  
769 # Determine $expires_in if expire flag given. For the "next"
770 # options, we're jumping through hoops to find the *top* of
771 # the next hour or day.
772 #
773 8 my $expires_in;
774 8 my $time = time;
775 8 ▶ if (exists($options{expire_at})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $options{'expire_at'}) { } 2 6 8
elsif (exists $options{'expire_next'}) { } 0 6 6
elsif (exists $options{'expire_in'}) { } 0 6 6
776 2 ▶ param_error "cache: invalid expire_at value '$options{expire_at}' - must be a numeric time value\n" if $options{expire_at} !~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"expire_at"} =~ /^[0-9]+$/ 0 2 2
777 2 $expires_in = $options{expire_at} - $time;
778 } elsif (exists($options{expire_next})) {
779 0 my $term = $options{expire_next};
780 0 my ($sec, $min, $hour) = localtime($time);
781 0 ▶ if ($term eq 'hour') {
Branch: 2 branches
BranchTrueFalseTotal
if ($term eq 'hour') { } 0 0 0
elsif ($term eq 'day') { } 0 0 0
782 0 $expires_in = 60*(59-$min)+(60-$sec);
783 } elsif ($term eq 'day') {
784 0 $expires_in = 3600*(23-$hour)+60*(59-$min)+(60-$sec);
785 } else {
786 0 param_error "cache: invalid expire_next value '$term' - must be 'hour' or 'day'\n";
787 }
788 } elsif (exists($options{expire_in})) {
789 0 $expires_in = $options{expire_in};
790 }
791  
792 # Set and return the value.
793 8 my $value = $options{value};
794 8 $cache->set($key, $value, $expires_in);
795 8 return $value;
796  
797 } elsif ($action eq 'expire') {
798 1 ▶ my @keys = (ref($key) eq 'ARRAY') ? @$key : ($key);
Branch: 1 branch
BranchTrueFalseTotal
ref $key eq 'ARRAY' ? : 1 0 1
799 1 foreach my $key (@keys) {
800 2 $cache->expire($key);
801 }
802  
803 } elsif ($action eq 'keys') {
804 1 return $cache->get_keys;
805 }
806 }
807  
808 98 ▶sub cache_self {
Subroutine cache_self: called CC=16 SCAR=27.7
Pod documented
809 98 my ($self, %options) = @_;
810  
811 98 ▶ return if $self->{top_stack}->[STACK_IN_CALL_SELF]->{'CACHE_SELF'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"top_stack"}[6]{"CACHE_SELF"} 35 63 98
812  
813 63 my (%store_options, %retrieve_options);
814 63 my ($expires_in, $key, $cache);
815 63 ▶ if ($self->data_cache_api eq '1.0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_cache_api eq '1.0') { } 3 60 63
816 3 foreach (qw(key expire_if busy_lock)) {
817 9 ▶ $retrieve_options{$_} = $options{$_} if (exists($options{$_}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$_} 3 6 9
818 }
819 3 foreach (qw(key expire_at expire_next expire_in)) {
820 12 ▶ $store_options{$_} = $options{$_} if (exists($options{$_}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$_} 3 9 12
821 }
822 } else {
823 #
824 # key, expires_in/expire_in, expire_if and busy_lock go into
825 # the set and get methods as appropriate. All other options
826 # are passed into $self->cache.
827 #
828 60 foreach (qw(expire_if busy_lock)) {
829 120 ▶ $retrieve_options{$_} = delete($options{$_}) if (exists($options{$_}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$_} 8 112 120
830 }
831 60 ▶ $expires_in = delete $options{expires_in} || delete $options{expire_in} || 'never';
Condition100%5/5
delete $options{'expires_in'} || delete $options{'expire_in'} || 'never'
l!l
1248
delete $options{"expires_in"} or delete $options{"expire_in"}
l!l&&r!l&&!r
6648
MC/DC100%2/2
delete $options{'expires_in'} || delete $options{'expire_in'} || 'never'
delete $options{"expires_in"} delete $options{"expire_in"}
Truth table100%3/3
A or B
A= delete $options{"expires_in"}B= delete $options{"expire_in"}
ABresult
1X1
011
000
832 60 ▶ $key = delete $options{key} || '__mason_cache_self__';
Condition100%2/2
delete $options{'key'} || '__mason_cache_self__'
l!l
1446
MC/DC100%1/1
delete $options{'key'} || '__mason_cache_self__'
delete $options{'key'}
Truth table100%2/2
A
A= delete $options{'key'}
Aresult
11
00
833 60 $cache = $self->cache(%options);
834 }
835  
836 63 my ($output, @retval, $error);
837  
838 63 ▶ my $cached =
Branch: 1 branch
BranchTrueFalseTotal
$self->data_cache_api eq '1.0' ? : 3 60 63
839 ( $self->data_cache_api eq '1.0' ?
840 $self->cache(%retrieve_options) :
841 $cache->get($key, %retrieve_options)
842 );
843  
844 63 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) { } 28 35 63
845 28 ($output, my $retval) = @$cached;
846 28 @retval = @$retval;
847 } else {
848 35 $self->call_self( \$output, \@retval, \$error, 'CACHE_SELF' );
849  
850 # If user aborted or declined, store in cache and print output
851 # before repropagating.
852 #
853 35 ▶ rethrow_exception $error
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_aborted_or_declined($error) 31 4 35
854 unless ($self->_aborted_or_declined($error));
855  
856 31 my $value = [$output, \@retval];
857 31 ▶ if ($self->data_cache_api eq '1.0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_cache_api eq '1.0') { } 1 30 31
858 1 $self->cache(action=>'store', key=>$key, value=>$value, %store_options);
859 } else {
860 30 $cache->set($key, $value, $expires_in);
861 }
862 }
863  
864 #
865 # Print the component output.
866 #
867 59 $self->print($output);
868  
869 #
870 # Rethrow abort/decline exception if any.
871 #
872 59 rethrow_exception $error;
873  
874 #
875 # Return the component return value in case the caller is interested,
876 # followed by 1 indicating the cache retrieval success.
877 #
878 55 return (@retval, 1);
879 }
880  
881 sub call_self
882 43 ▶{
Subroutine call_self: called CC=12 SCAR=25.0
Pod documented
883 43 my ($self, $output, $retval, $error, $tag) = @_;
884  
885 # Keep track of each individual invocation of call_self in the
886 # component, via $tag. $tag is 'CACHE_SELF' or 'FILTER' when used
887 # by $m->cache_self and <%filter> sections respectively.
888 #
889 43 ▶ $tag ||= 'DEFAULT';
Condition100%2/2
$tag ||= "DEFAULT"
l!l
358
MC/DC100%1/1
$tag ||= "DEFAULT"
$tag
Truth table100%2/2
A
A= $tag
Aresult
11
00
890 43 my $top_stack = $self->{top_stack};
891 43 ▶ $top_stack->[STACK_IN_CALL_SELF] ||= {};
Condition100%2/2
$top_stack->[6] ||= {}
l!l
394
MC/DC100%1/1
$top_stack->[6] ||= {}
$top_stack->[6]
Truth table100%2/2
A
A= $top_stack->[6]
Aresult
11
00
892 43 ▶ return if $top_stack->[STACK_IN_CALL_SELF]->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $top_stack->[6]{$tag} 4 39 43
893 39 local $top_stack->[STACK_IN_CALL_SELF]->{$tag} = 1;
894  
895 # Determine wantarray based on retval reference
896 39 ▶ my $wantarray =
Branch: 2 branches
BranchTrueFalseTotal
defined $retval ? : 37 2 39
UNIVERSAL::isa($retval, 'ARRAY') ? : 37 0 37
897 ( defined $retval ?
898 ( UNIVERSAL::isa( $retval, 'ARRAY' ) ? 1 : 0 ) :
899 undef
900 );
901  
902 # If output or retval references were left undefined, just point
903 # them to a dummy variable.
904 #
905 39 my $dummy;
906 39 ▶ $output ||= \$dummy;
Condition100%2/2
$output ||= \$dummy
l!l
381
MC/DC100%1/1
$output ||= \$dummy
$output
Truth table100%2/2
A
A= $output
Aresult
11
00
907 39 ▶ $retval ||= \$dummy;
Condition100%2/2
$retval ||= \$dummy
l!l
372
MC/DC100%1/1
$retval ||= \$dummy
$retval
Truth table100%2/2
A
A= $retval
Aresult
11
00
908  
909 # Temporarily put $output in place of the current top buffer.
910 39 local $top_stack->[STACK_BUFFER] = $output;
911  
912 # Call the component again, capturing output, return value and
913 # error. Don't catch errors unless the error reference was specified.
914 #
915 39 my $comp = $top_stack->[STACK_COMP];
916 39 my $args = $top_stack->[STACK_ARGS];
917 39 my @result;
918 39 eval {
919 39 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 37 2 39
elsif (defined $wantarray) { } 0 2 2
920 37 @$retval = $comp->run(@$args);
921 } elsif (defined $wantarray) {
922 0 $$retval = $comp->run(@$args);
923 } else {
924 2 $comp->run(@$args);
925 }
926 };
927 39 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 8 31 39
928 8 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 8 0 8
929 8 $$error = $@;
930 } else {
931 0 die $@;
932 }
933 }
934  
935 # Return 1, indicating that this invocation of call_self is done.
936 #
937 39 return 1;
938 }
939  
940 22 ▶sub call_dynamic {
Subroutine call_dynamic: called CC=4 SCAR=13.9
Pod undocumented
941 22 my ($m, $key, @args) = @_;
942 22 ▶ my $comp = ($m->current_comp->is_subcomp) ? $m->current_comp->owner : $m->current_comp;
Branch: 1 branch
BranchTrueFalseTotal
$m->current_comp->is_subcomp ? : 11 11 22
943 22 ▶ if (!defined($comp->dynamic_subs_request) or $comp->dynamic_subs_request ne $m) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $comp->dynamic_subs_request or $comp->dynamic_subs_request ne $m) 13 9 22
Condition66%2/3
not defined $comp->dynamic_subs_request or $comp->dynamic_subs_request ne $m
l!l&&r!l&&!r
1309
MC/DC50%1/2
not defined $comp->dynamic_subs_request or $comp->dynamic_subs_request ne $m
not defined $comp->dynamic_subs_request $comp->dynamic_subs_request ne $m
Truth table66%2/3
not(A) or B
A= not defined $comp->dynamic_subs_requestB= $comp->dynamic_subs_request ne $m
ABresult
1X1
011
000
944 13 $comp->dynamic_subs_init;
945 8 $comp->dynamic_subs_request($m);
946 }
947  
948 17 return $comp->run_dynamic_sub($key, @args);
949 }
950  
951 23 ▶sub call_next {
Subroutine call_next: called CC=2 SCAR=7.0
Pod documented
952 23 my ($self,@extra_args) = @_;
953 23 ▶ my $comp = $self->fetch_next
Branch: 1 branch
BranchTrueFalseTotal
my $comp = $self->fetch_next or error("call_next: no next component to invoke") 23 0 23
954 or error "call_next: no next component to invoke";
955 23 return $self->comp({base_comp=>$self->request_comp}, $comp, @{$self->current_args}, @extra_args);
956 }
957  
958 sub caller
959 3 ▶{
Subroutine caller: called CC=1 SCAR=0.0
Pod documented
960 3 my ($self) = @_;
961 3 return $self->callers(1);
962 }
963  
964 #
965 # Return a specified component from the stack, or the whole stack as a list.
966 #
967 sub callers
968 48 ▶{
Subroutine callers: called CC=3 SCAR=11.0
Pod documented
969 48 my ($self, $levels_back) = @_;
970 48 ▶ if (defined($levels_back)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $levels_back) { } 39 9 48
971 39 my $frame = $self->_stack_frame($levels_back);
972 39 ▶ return unless defined $frame;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame 8 31 39
973 31 return $frame->[STACK_COMP];
974 } else {
975 9 my $depth = $self->depth;
976 9 return map($_->[STACK_COMP], $self->_stack_frames);
977 }
978 }
979  
980 #
981 # Return a specified argument list from the stack.
982 #
983 sub caller_args
984 3 ▶{
Subroutine caller_args: called CC=4 SCAR=14.5
Pod documented
985 3 my ($self, $levels_back) = @_;
986 3 ▶ param_error "caller_args expects stack level as argument" unless defined $levels_back;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $levels_back 0 3 3
987  
988 3 my $frame = $self->_stack_frame($levels_back);
989 3 ▶ return unless $frame;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame 0 3 3
990 3 my $args = $frame->[STACK_ARGS];
991 3 ▶ return wantarray ? @$args : { @$args };
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
992 }
993  
994 sub comp_exists
995 17 ▶{
Subroutine comp_exists: called CC=2 SCAR=6.9
Pod documented
996 17 my ($self, $path) = @_;
997  
998 # In order to support SELF, PARENT, REQUEST, subcomponents and
999 # methods, it is easiest just to defer to fetch_comp.
1000 #
1001 17 ▶ return $self->fetch_comp($path) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->fetch_comp($path) ? : 7 10 17
1002 }
1003  
1004 sub decline
1005 10 ▶{
Subroutine decline: called CC=1 SCAR=0.0
Pod documented
1006 10 my ($self) = @_;
1007  
1008 10 $self->clear_buffer;
1009 my $subreq = $self->make_subrequest
1010 (comp => $self->{top_path},
1011 args => $self->{request_args},
1012 10 declined_comps => {$self->request_comp->comp_id, 1, %{$self->{declined_comps}}});
1013 9 my $retval = $subreq->exec;
1014 8 HTML::Mason::Exception::Decline->throw( error => 'Request->decline was called', declined_value => $retval );
1015 }
1016  
1017 #
1018 # Return the current number of stack levels. 1 means top level, 0
1019 # means that no component has been called yet.
1020 #
1021 sub depth
1022 1358 ▶{
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
1023 1358 return scalar @{ $_[0]->{stack} };
1024 }
1025  
1026 #
1027 # Given a component path (absolute or relative), returns a component.
1028 # Handles SELF, PARENT, REQUEST, comp:method, relative->absolute
1029 # conversion, and local subcomponents.
1030 #
1031 # fetch_comp handles caching if use_internal_component_caches is on.
1032 # _fetch_comp does the real work.
1033 #
1034 sub fetch_comp
1035 840 ▶{
Subroutine fetch_comp: called CC=7 SCAR=19.8
Pod documented
1036 840 my ($self, $path, $current_comp, $error, $exists_only) = @_;
1037  
1038 840 ▶ return undef unless defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 838 840
1039 838 ▶ $current_comp ||= $self->{top_stack}->[STACK_COMP];
Condition66%2/3
$current_comp ||= $self->{"top_stack"}[0]
l!l&&r!l&&!r
767620
MC/DC0%0/2
$current_comp ||= $self->{"top_stack"}[0]
$current_comp $self->{"top_stack"}[0]
Truth table66%2/3
A ||= B
A= $current_compB= $self->{"top_stack"}[0]
ABresult
1X1
011
000
1040  
1041 return $self->_fetch_comp($path, $current_comp, $error)
1042 838 ▶ unless $self->{use_internal_component_caches};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"use_internal_component_caches"} 803 35 838
1043  
1044 35 my $fetch_comp_cache = $current_comp->{fetch_comp_cache};
1045 35 ▶ unless (defined($fetch_comp_cache->{$path})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fetch_comp_cache->{$path}) 11 24 35
1046  
1047 # Cache the component objects associated with
1048 # uncanonicalized paths like ../foo/bar.html. SELF and
1049 # REQUEST are dynamic and cannot be cached. Weaken the
1050 # references in this cache so that we don't hang on to the
1051 # coponent if it disappears from the main code cache.
1052 #
1053 # See Interp::_initialize for the definition of
1054 # use_internal_component_caches and the conditions under
1055 # which we can create this cache safely.
1056 #
1057 11 ▶ if ($path =~ /^(?:SELF|REQUEST)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /^(?:SELF|REQUEST)/) { } 0 11 11
1058 0 return $self->_fetch_comp($path, $current_comp, $error);
1059 } else {
1060 11 $fetch_comp_cache->{$path} =
1061 $self->_fetch_comp($path, $current_comp, $error);
1062 11 ▶ Scalar::Util::weaken($fetch_comp_cache->{$path}) if can_weaken;
Branch: 1 branch
BranchTrueFalseTotal
if &can_weaken() 11 0 11
1063 }
1064 }
1065  
1066 35 return $fetch_comp_cache->{$path};
1067 }
1068  
1069 sub _fetch_comp
1070 814 ▶{
Subroutine _fetch_comp: called CC=16 SCAR=27.7
1071 814 my ($self, $path, $current_comp, $error) = @_;
1072  
1073 #
1074 # Handle paths SELF, PARENT, and REQUEST
1075 #
1076 814 ▶ if ($path eq 'SELF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "SELF") 63 751 814
1077 63 return $self->base_comp;
1078 }
1079 751 ▶ if ($path eq 'PARENT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "PARENT") 8 743 751
1080 8 my $c = $current_comp->parent;
1081 8 ▶ $$error = "PARENT designator used from component with no parent" if !$c && defined($error);
Branch: 1 branch
BranchTrueFalseTotal
if not $c and defined $error 1 7 8
Condition100%3/3
not $c and defined $error
!ll&&!rl&&r
431
MC/DC100%2/2
not $c and defined $error
not $c defined $error
Truth table100%3/3
not(A) and B
A= not $cB= defined $error
ABresult
0X0
100
111
1082 8 return $c;
1083 }
1084 743 ▶ if ($path eq 'REQUEST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "REQUEST") 4 739 743
1085 4 return $self->request_comp;
1086 }
1087  
1088 #
1089 # Handle paths of the form comp_path:method_name
1090 #
1091 739 ▶ if (index($path,':') != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($path, ":") != -1) 75 664 739
1092 75 my $method_comp;
1093 75 my ($owner_path,$method_name) = split(':',$path,2);
1094 75 ▶ if (my $owner_comp = $self->fetch_comp($owner_path, $current_comp, $error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $owner_comp = $self->fetch_comp($owner_path, $current_comp, $error)) { } 73 2 75
1095 73 ▶ if ($owner_comp->_locate_inherited('methods',$method_name,\$method_comp)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($owner_comp->_locate_inherited('methods', $method_name, \$method_comp)) { } 68 5 73
1096 68 return $method_comp;
1097 } else {
1098 5 ▶ $$error = "no such method '$method_name' for component " . $owner_comp->title if defined($error);
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 4 5
1099 }
1100 } else {
1101 2 ▶ $$error ||= "could not find component for path '$owner_path'\n" if defined($error);
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 1 1 2
Condition50%1/2
$$error ||= "could not find component for path '${owner_path}'\n"
l!l
10
MC/DC0%0/1
$$error ||= "could not find component for path '${owner_path}'\n"
$$error
Truth table50%1/2
A
A= $$error
Aresult
11
00
1102 }
1103  
1104 7 return $method_comp;
1105 }
1106  
1107 #
1108 # If path does not contain a slash, check for a subcomponent in the
1109 # current component first.
1110 #
1111 664 ▶ if ($path !~ /\//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ m[/]) 464 200 664
1112 # Check my subcomponents.
1113 464 ▶ if (my $subcomp = $current_comp->subcomps($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subcomp = $current_comp->subcomps($path)) 58 406 464
1114 58 return $subcomp;
1115 }
1116 # If I am a subcomponent, also check my owner's subcomponents.
1117 # This won't work when we go to multiply embedded subcomponents...
1118 406 ▶ if ($current_comp->is_subcomp and my $subcomp = $current_comp->owner->subcomps($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_comp->is_subcomp and my $subcomp = $current_comp->owner->subcomps($path)) 5 401 406
Condition66%2/3
$current_comp->is_subcomp and my $subcomp = $current_comp->owner->subcomps($path)
!ll&&!rl&&r
40105
MC/DC50%1/2
$current_comp->is_subcomp and my $subcomp = $current_comp->owner->subcomps($path)
$current_comp->is_subcomp my $subcomp = $current_comp->owner->subcomps($path)
Truth table66%2/3
A and B
A= $current_comp->is_subcompB= my $subcomp = $current_comp->owner->subcomps($path)
ABresult
0X0
100
111
1119 5 return $subcomp;
1120 }
1121 }
1122  
1123 #
1124 # Otherwise pass the canonicalized absolute path to interp->load.
1125 #
1126 601 $path = absolute_comp_path($path, $current_comp->dir_path);
1127 601 my $comp = $self->interp->load($path);
1128  
1129 601 return $comp;
1130 }
1131  
1132 #
1133 # Fetch the index of the next component in wrapper chain. If current
1134 # component is not in chain, search the component stack for the most
1135 # recent one that was.
1136 #
1137 29 ▶sub _fetch_next_helper {
Subroutine _fetch_next_helper: called CC=4 SCAR=13.9
1138 29 my ($self) = @_;
1139 29 my $index = $self->{wrapper_index}->{$self->current_comp->comp_id};
1140 29 ▶ unless (defined($index)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 2 27 29
1141 2 my @callers = $self->callers;
1142 2 shift(@callers);
1143 2 ▶ while (my $comp = shift(@callers) and !defined($index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comp = shift @callers and not defined $index) 2 2 4
Condition100%3/3
my $comp = shift @callers and not defined $index
!ll&&!rl&&r
112
MC/DC100%2/2
my $comp = shift @callers and not defined $index
my $comp = shift @callers not defined $index
Truth table100%3/3
A and not(B)
A= my $comp = shift @callersB= not defined $index
ABresult
0X0
100
111
1144 2 $index = $self->{wrapper_index}->{$comp->comp_id};
1145 }
1146 }
1147 29 return $index;
1148 }
1149  
1150 #
1151 # Fetch next component in wrapper chain.
1152 #
1153 26 ▶sub fetch_next {
Subroutine fetch_next: called CC=2 SCAR=7.0
Pod documented
1154 26 my ($self) = @_;
1155 26 my $index = $self->_fetch_next_helper;
1156 26 ▶ error "fetch_next: cannot find next component in chain"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 26 26
1157 unless defined($index);
1158 26 return $self->{wrapper_chain}->[$index+1];
1159 }
1160  
1161 #
1162 # Fetch remaining components in wrapper chain.
1163 #
1164 3 ▶sub fetch_next_all {
Subroutine fetch_next_all: called CC=2 SCAR=7.0
Pod documented
1165 3 my ($self) = @_;
1166 3 my $index = $self->_fetch_next_helper;
1167 3 ▶ error "fetch_next_all: cannot find next component in chain"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 3 3
1168 unless defined($index);
1169 3 my @wc = @{$self->{wrapper_chain}};
1170 3 return @wc[($index+1)..$#wc];
1171 }
1172  
1173 sub file
1174 3 ▶{
Subroutine file: called CC=4 SCAR=14.1
Pod documented
1175 3 my ($self,$file) = @_;
1176 3 my $interp = $self->interp;
1177 3 ▶ unless ( File::Spec->file_name_is_absolute($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($file)) 3 0 3
1178 # use owner if current comp is a subcomp
1179 3 ▶ my $context_comp =
Branch: 1 branch
BranchTrueFalseTotal
$self->current_comp->is_subcomp ? : 1 2 3
1180 ( $self->current_comp->is_subcomp ?
1181 $self->current_comp->owner :
1182 $self->current_comp );
1183  
1184 3 ▶ if ($context_comp->is_file_based) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_comp->is_file_based) { } 3 0 3
1185 3 my $source_dir = $context_comp->source_dir;
1186 3 $file = File::Spec->catfile( $source_dir, $file );
1187 } else {
1188 0 $file = File::Spec->catfile( File::Spec->rootdir, $file );
1189 }
1190 }
1191 3 my $content = read_file($file,1);
1192 3 return $content;
1193 }
1194  
1195 sub print
1196 3587 ▶{
Subroutine print: called CC=5 SCAR=16.1
Pod documented
1197 3587 my $self = shift;
1198  
1199 # $self->{top_stack} is always defined _except_ in the case of a
1200 # call to print inside a start-/end-request plugin.
1201 my $bufref =
1202 ( defined $self->{top_stack}
1203 ? $self->{top_stack}->[STACK_BUFFER]
1204 : \$self->{request_buffer}
1205 3587 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'top_stack'} ? : 3566 21 3587
1206  
1207 # use 'if defined' for maximum efficiency; grep creates a list.
1208 3587 for ( @_ ) {
1209 3591 ▶ $$bufref .= $_ if defined;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 3587 4 3591
1210 }
1211  
1212 3587 ▶ $self->flush_buffer if $self->{autoflush};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autoflush"} 134 3453 3587
1213 }
1214  
1215 32 *out = \&print;
1216  
1217 #
1218 # Execute the given component
1219 #
1220 1250 ▶sub comp {
Subroutine comp: called CC=23 SCAR=31.4
Pod documented
1221 1250 my $self = shift;
1222 1250 my $log_is_debug = $log->is_debug;
1223  
1224 # Get modifiers: optional hash reference passed in as first argument.
1225 # Merge multiple hash references to simplify user and internal usage.
1226 #
1227 1250 my %mods;
1228 1250 ▶ %mods = (%{shift()}, %mods) while ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "HASH" and do { %mods = (%{shift();}, %mods) } 565 1250 1815
1229  
1230 # Get component path or object. If a path, load into object.
1231 #
1232 1250 my $path;
1233 1250 my $comp = shift;
1234 1250 ▶ if (!ref($comp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $comp) 726 524 1250
1235 726 ▶ die "comp called without component - must pass a path or component object"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $comp 0 726 726
1236 unless defined($comp);
1237 726 $path = $comp;
1238 726 my $error;
1239 726 ▶ $comp = $self->fetch_comp($path, undef, \$error)
Branch: 1 branch
BranchTrueFalseTotal
$comp = $self->fetch_comp($path, undef, \$error) or error($error || "could not find component for path '${path}'\n") 717 9 726
Condition100%2/2
$error || "could not find component for path '${path}'\n"
l!l
27
MC/DC100%1/1
$error || "could not find component for path '${path}'\n"
$error
Truth table100%2/2
A
A= $error
Aresult
11
00
1240 or error($error || "could not find component for path '$path'\n");
1241 }
1242  
1243 # Increment depth and check for maximum recursion. Depth starts at 1.
1244 #
1245 1241 my $depth = $self->depth;
1246 error "$depth levels deep in component stack (infinite recursive call?)\n"
1247 1241 ▶ if $depth >= $self->{max_recurse};
Branch: 1 branch
BranchTrueFalseTotal
if $depth >= $self->{"max_recurse"} 1 1240 1241
1248  
1249 # Log start of component call.
1250 #
1251 1240 ▶ $log->debugf("entering component '%s' [depth %d]", $comp->title(), $depth)
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 2 1238 1240
1252 if $log_is_debug;
1253  
1254 # Keep the same output buffer unless store modifier was passed. If we have
1255 # a filter, put the filter buffer on the stack instead of the regular buffer.
1256 #
1257 1240 my $filter_buffer = '';
1258 1240 ▶ my $top_buffer = defined($mods{store}) ? $mods{store} : $self->{top_stack}->[STACK_BUFFER];
Branch: 1 branch
BranchTrueFalseTotal
defined $mods{'store'} ? : 494 746 1240
1259 1240 ▶ my $stack_buffer = $comp->{has_filter} ? \$filter_buffer : $top_buffer;
Branch: 1 branch
BranchTrueFalseTotal
$comp->{'has_filter'} ? : 39 1201 1240
1260 1240 ▶ my $flushable = exists $mods{flushable} ? $mods{flushable} : ($self->{top_stack}->[STACK_BUFFER_IS_FLUSHABLE] && ! defined($mods{store})) ;
Branch: 1 branch
BranchTrueFalseTotal
exists $mods{'flushable'} ? : 466 774 1240
Condition100%3/3
$self->{"top_stack"}[7] and not defined $mods{"store"}
!ll&&!rl&&r
24028506
MC/DC100%2/2
$self->{"top_stack"}[7] and not defined $mods{"store"}
$self->{"top_stack"}[7] not defined $mods{"store"}
Truth table100%3/3
A and not(B)
A= $self->{"top_stack"}[7]B= not defined $mods{"store"}
ABresult
0X0
100
111
1261  
1262 # Add new stack frame and point dynamically scoped $self->{top_stack} at it.
1263 1240 push @{ $self->{stack} },
1264 [ $comp, # STACK_COMP
1265 \@_, # STACK_ARGS
1266 $stack_buffer, # STACK_BUFFER
1267 \%mods, # STACK_MODS
1268 $path, # STACK_PATH
1269 undef, # STACK_BASE_COMP
1270 undef, # STACK_IN_CALL_SELF
1271 $flushable, # STACK_BUFFER_IS_FLUSHABLE
1272 ];
1273 1240 local $self->{top_stack} = $self->{stack}->[-1];
1274  
1275 # Run start_component hooks for each plugin.
1276 #
1277 1240 ▶ if ($self->{has_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_plugins"}) 54 1186 1240
1278 54 my $context = bless
1279 [$self, $comp, \@_],
1280 'HTML::Mason::Plugin::Context::StartComponent';
1281  
1282 54 foreach my $plugin_instance (@{$self->{plugin_instances}}) {
1283 74 $plugin_instance->start_component_hook( $context );
1284 }
1285 }
1286  
1287 # Finally, call the component.
1288 #
1289 1239 my $wantarray = wantarray;
1290 1239 my @result;
1291
1292 1239 eval {
1293 # By putting an empty block here, we protect against stack
1294 # corruption when a component calls next or last outside of a
1295 # loop. See 05-request.t #28 for a test.
1296 {
1297 1239 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 14 1225 1239
elsif (defined $wantarray) { } 23 1202 1225
1298 14 @result = $comp->run(@_);
1299 } elsif (defined $wantarray) {
1300 23 $result[0] = $comp->run(@_);
1301 } else {
1302 1202 $comp->run(@_);
1303 }
1304 }
1305 };
1306 1239 my $error = $@;
1307  
1308 # Run component's filter if there is one, and restore true top buffer
1309 # (e.g. in case a plugin prints something).
1310 #
1311 1239 ▶ if ($comp->{has_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->{"has_filter"}) 39 1200 1239
1312 # We have to check $comp->filter because abort or error may
1313 # occur before filter gets defined in component. In such cases
1314 # there should be no output, but should look into this more.
1315 #
1316 39 ▶ if (defined($comp->filter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp->filter) 37 2 39
1317 37 $$top_buffer .= $comp->filter->($filter_buffer);
1318 }
1319 37 $self->{top_stack}->[STACK_BUFFER] = $top_buffer;
1320 }
1321  
1322 # Run end_component hooks for each plugin, in reverse order.
1323 #
1324 1237 ▶ if ($self->{has_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"has_plugins"}) 53 1184 1237
1325 53 my $context = bless
1326 [$self, $comp, \@_, $wantarray, \@result, \$error],
1327 'HTML::Mason::Plugin::Context::EndComponent';
1328
1329 53 foreach my $plugin_instance (@{$self->{plugin_instances_reverse}}) {
1330 73 $plugin_instance->end_component_hook( $context );
1331 }
1332 }
1333  
1334 # This is very important in order to avoid memory leaks, since we
1335 # stick the arguments on the stack. If we don't pop the stack,
1336 # they don't get cleaned up until the component exits.
1337 1234 pop @{ $self->{stack} };
1338  
1339 # Log end of component call.
1340 #
1341 1234 ▶ $log->debug(sprintf("exiting component '%s' [depth %d]", $comp->title(), $depth))
Branch: 1 branch
BranchTrueFalseTotal
if $log_is_debug 2 1232 1234
1342 if $log_is_debug;
1343  
1344 # Repropagate error if one occurred, otherwise return result.
1345 1234 ▶ rethrow_exception $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 156 1078 1234
1346 1078 ▶ return $wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 9 1069 1078
1347 }
1348  
1349 #
1350 # Like comp, but return component output.
1351 #
1352 25 ▶sub scomp {
Subroutine scomp: called CC=1 SCAR=0.0
Pod documented
1353 25 my $self = shift;
1354 25 my $buf;
1355 25 $self->comp({store => \$buf},@_);
1356 25 return $buf;
1357 }
1358  
1359 2 ▶sub has_content {
Subroutine has_content: called CC=1 SCAR=0.0
Pod documented
1360 2 my $self = shift;
1361 2 return defined($self->{top_stack}->[STACK_MODS]->{content});
1362 }
1363  
1364 47 ▶sub content {
Subroutine content: called CC=2 SCAR=6.9
Pod documented
1365 47 my $self = shift;
1366 47 my $content = $self->{top_stack}->[STACK_MODS]->{content};
1367 47 ▶ return undef unless defined($content);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 2 45 47
1368  
1369 # Run the content routine with the previous stack frame active and
1370 # with output going to a new buffer.
1371 #
1372 45 my $err;
1373 my $buffer;
1374 45 my $save_frame = pop @{ $self->{stack} };
1375 {
1376 45 local $self->{top_stack} = $self->{stack}[-1];
1377 45 local $self->{top_stack}->[STACK_BUFFER] = \$buffer;
1378 45 local $self->{top_stack}->[STACK_BUFFER_IS_FLUSHABLE] = 0;
1379 45 local $self->{top_stack}->[STACK_HIDDEN_BUFFER] = $save_frame->[STACK_BUFFER];
1380 45 eval { $content->(); };
1381 45 $err = $@;
1382 }
1383  
1384 45 push @{ $self->{stack} }, $save_frame;
1385  
1386 45 rethrow_exception $err;
1387  
1388 # Return the output from the content routine.
1389 #
1390 45 return $buffer;
1391 }
1392  
1393 3 ▶sub notes {
Subroutine notes: called CC=3 SCAR=11.0
Pod documented
1394 3 my $self = shift;
1395 3 ▶ return $self->{notes} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
1396
1397 2 my $key = shift;
1398 2 ▶ return $self->{notes}{$key} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 1 2
1399
1400 1 return $self->{notes}{$key} = shift;
1401 }
1402  
1403 sub clear_buffer
1404 29 ▶{
Subroutine clear_buffer: called CC=3 SCAR=11.0
Pod documented
1405 29 my $self = shift;
1406  
1407 29 foreach my $frame (@{$self->{stack}}) {
1408 41 my $bufref = $frame->[STACK_BUFFER];
1409 41 $$bufref = '';
1410 41 $bufref = $frame->[STACK_HIDDEN_BUFFER];
1411 41 ▶ $$bufref = '' if $bufref;
Branch: 1 branch
BranchTrueFalseTotal
if $bufref 2 39 41
1412 }
1413 }
1414  
1415 sub flush_buffer
1416 147 ▶{
Subroutine flush_buffer: called CC=6 SCAR=18.0
Pod documented
1417 147 my $self = shift;
1418  
1419 $self->out_method->($self->{request_buffer})
1420 147 ▶ if length $self->{request_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"request_buffer"} 136 11 147
1421 147 $self->{request_buffer} = '';
1422  
1423 147 ▶ if ( $self->{top_stack}->[STACK_BUFFER_IS_FLUSHABLE]
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"top_stack"}[7] and $self->{"top_stack"}[2]) 140 7 147
Condition66%2/3
$self->{"top_stack"}[7] and $self->{"top_stack"}[2]
!ll&&!rl&&r
70140
MC/DC50%1/2
$self->{"top_stack"}[7] and $self->{"top_stack"}[2]
$self->{"top_stack"}[7] $self->{"top_stack"}[2]
Truth table66%2/3
A and B
A= $self->{"top_stack"}[7]B= $self->{"top_stack"}[2]
ABresult
0X0
100
111
1424 && $self->{top_stack}->[STACK_BUFFER] )
1425 {
1426 140 my $comp = $self->{top_stack}->[STACK_COMP];
1427 140 ▶ if ( $comp->has_filter()
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->has_filter and defined $comp->filter) { } 2 138 140
Condition66%2/3
$comp->has_filter and defined $comp->filter
!ll&&!rl&&r
13802
MC/DC50%1/2
$comp->has_filter and defined $comp->filter
$comp->has_filter defined $comp->filter
Truth table66%2/3
A and B
A= $comp->has_filterB= defined $comp->filter
ABresult
0X0
100
111
1428 && defined $comp->filter() )
1429 {
1430 $self->out_method->
1431 2 ( $comp->filter->( ${ $self->{top_stack}->[STACK_BUFFER] } ) );
1432 }
1433 else
1434 {
1435 138 $self->out_method->( ${ $self->{top_stack}->[STACK_BUFFER] } );
1436 }
1437 140 ${$self->{top_stack}->[STACK_BUFFER]} = '';
1438 }
1439 }
1440  
1441 sub request_args
1442 3 ▶{
Subroutine request_args: called CC=2 SCAR=6.9
Pod documented
1443 3 my ($self) = @_;
1444 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 2 3
1445 1 return @{$self->{request_args}};
1446 } else {
1447 2 return { @{$self->{request_args}} };
1448 }
1449 }
1450  
1451 # For backward compatibility:
1452 32 *top_args = \&request_args;
1453 32 *top_comp = \&request_comp;
1454  
1455 #
1456 # Subroutine called by every component while in debug mode, convenient
1457 # for breakpointing.
1458 #
1459 sub debug_hook
1460 0 ▶{
Subroutine debug_hook: not called CC=1 SCAR=6.9
Pod undocumented
1461 0 1;
1462 }
1463  
1464  
1465 #
1466 # stack handling
1467 #
1468  
1469 # Return the stack frame $levels down from the top of the stack.
1470 # If $levels is negative, count from the bottom of the stack.
1471 #
1472 42 ▶sub _stack_frame {
Subroutine _stack_frame: called CC=4 SCAR=13.9
1473 42 my ($self, $levels) = @_;
1474 42 my $depth = $self->depth();
1475 42 my $index;
1476 42 ▶ if ($levels < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($levels < 0) { } 9 33 42
1477 9 $index = (-1 * $levels) - 1;
1478 } else {
1479 33 $index = $depth-1 - $levels;
1480 }
1481 42 ▶ return if $index < 0 or $index >= $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 or $index >= $depth 8 34 42
Condition100%3/3
$index < 0 or $index >= $depth
l!l&&r!l&&!r
6234
MC/DC100%2/2
$index < 0 or $index >= $depth
$index < 0 $index >= $depth
Truth table100%3/3
A or B
A= $index < 0B= $index >= $depth
ABresult
1X1
011
000
1482 34 return $self->{stack}->[$index];
1483 }
1484  
1485 # Return all stack frames, in order from the top of the stack to the
1486 # initial frame.
1487 9 ▶sub _stack_frames {
Subroutine _stack_frames: called CC=1 SCAR=0.0
1488 9 my ($self) = @_;
1489  
1490 9 my $depth = $self->depth;
1491 9 return reverse map { $self->{stack}->[$_] } (0..$depth-1);
1492 }
1493  
1494 #
1495 # Accessor methods for top of stack elements.
1496 #
1497 385 ▶sub current_comp { return $_[0]->{top_stack}->[STACK_COMP] }
Subroutine current_comp: called CC=1 SCAR=0.0
Pod documented
1498 23 ▶sub current_args { return $_[0]->{top_stack}->[STACK_ARGS] }
Subroutine current_args: called CC=1 SCAR=0.0
Pod documented
1499  
1500 89 ▶sub base_comp {
Subroutine base_comp: called CC=3 SCAR=11.0
Pod documented
1501 89 my ($self) = @_;
1502  
1503 89 ▶ return unless $self->{top_stack};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"top_stack"} 0 89 89
1504  
1505 89 ▶ unless ( defined $self->{top_stack}->[STACK_BASE_COMP] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"top_stack"}[5]) 40 49 89
1506 40 $self->_compute_base_comp_for_frame( $self->depth - 1 );
1507 }
1508 89 return $self->{top_stack}->[STACK_BASE_COMP];
1509 }
1510  
1511 #
1512 # Determine the base_comp for a stack frame. See the user
1513 # documentation for base_comp for a description of these rules.
1514 #
1515 48 ▶sub _compute_base_comp_for_frame {
Subroutine _compute_base_comp_for_frame: called CC=9 SCAR=22.0
1516 48 my ($self, $frame_num) = @_;
1517 48 ▶ die "Invalid frame number: $frame_num" if $frame_num < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $frame_num < 0 0 48 48
1518  
1519 48 my $frame = $self->{stack}->[$frame_num];
1520  
1521 48 ▶ unless (defined($frame->[STACK_BASE_COMP])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $frame->[5]) 44 4 48
1522 44 my $mods = $frame->[STACK_MODS];
1523 44 my $path = $frame->[STACK_PATH];
1524 44 my $comp = $frame->[STACK_COMP];
1525
1526 44 my $base_comp;
1527 44 ▶ if (exists($mods->{base_comp})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $mods->{'base_comp'}) { } 32 12 44
elsif (not $path or $path =~ /^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/ or $comp->is_subcomp and not $comp->is_method) { } 8 4 12
elsif ($path =~ /(.*):/) { } 1 3 4
1528 32 $base_comp = $mods->{base_comp};
1529 ▶ } elsif (!$path ||
Condition100%9/9
not $path or $path =~ /^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/ or $comp->is_subcomp and not $comp->is_method
l!l&&r!l&&!r
534
not $path or $path =~ /^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/
l!l&&r!l&&!r
147
$comp->is_subcomp and not $comp->is_method
!ll&&!rl&&r
313
MC/DC100%4/4
not $path or $path =~ /^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/ or $comp->is_subcomp and not $comp->is_method
not $path $path =~ /^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/ $comp->is_subcomp not $comp->is_method
Truth table100%5/5
not(A) or B or C and not(D)
A= not $pathB= $path =~ /^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/C= $comp->is_subcompD= not $comp->is_method
ABCDresult
1XXX1
01XX1
00111
000X0
00100
1530 $path =~ m/^(?:SELF|PARENT|REQUEST)(?:\:..*)?$/ ||
1531 ($comp->is_subcomp && !$comp->is_method)) {
1532 8 $base_comp = $self->_compute_base_comp_for_frame($frame_num-1);
1533 } elsif ($path =~ m/(.*):/) {
1534 1 my $calling_comp = $self->{stack}->[$frame_num-1]->[STACK_COMP];
1535 1 $base_comp = $self->fetch_comp($1, $calling_comp);
1536 } else {
1537 3 $base_comp = $comp;
1538 }
1539 44 $frame->[STACK_BASE_COMP] = $base_comp;
1540 }
1541 48 return $frame->[STACK_BASE_COMP];
1542 }
1543  
1544 sub log
1545 2 ▶{
Subroutine log: called CC=1 SCAR=0.0
Pod documented
1546 2 my ($self) = @_;
1547 2 return $self->current_comp->logger();
1548 }
1549  
1550 package Tie::Handle::Mason;
1551 32 $Tie::Handle::Mason::VERSION = '1.60';
1552 sub TIEHANDLE
1553 467 ▶{
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
1554 467 my $class = shift;
1555  
1556  
1557 467 return bless {}, $class;
1558 }
1559  
1560 sub PRINT
1561 116 ▶{
Subroutine PRINT: called CC=1 SCAR=0.0
1562 116 my $self = shift;
1563  
1564 116 my $old = select STDOUT;
1565 # Use direct $m access instead of Request->instance() to optimize common case
1566 116 $HTML::Mason::Commands::m->print(@_);
1567  
1568 116 select $old;
1569 }
1570  
1571 sub PRINTF
1572 2 ▶{
Subroutine PRINTF: called CC=1 SCAR=0.0
1573 2 my $self = shift;
1574  
1575 # apparently sprintf(@_) won't work, it needs to be a scalar
1576 # followed by a list
1577 2 $self->PRINT(sprintf(shift, @_));
1578 }
1579  
1580 32 1;
1581  
1582 __END__
« lib/HTML/Mason/CGIHandler.pm ↑ Summary lib/HTML/Mason/Lexer.pm »