lib/HTML/Mason/Compiler.pm

filter: Statementstmt 89.9% 313 / 348 Branchbran 77.2% 102 / 132 Conditioncond 62.0% 18 / 29 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 91.1% 41 / 45 Podpod 72.0% 18 / 25 totaltotal 83.3% 497 / 596 SCAR 47.1
CC
86
Cov
85%
_dump_data
30.0
substitution
13.0
dump
12.0
CRAP 110.6
« lib/HTML/Mason/Interp.pm ↑ Summary lib/HTML/Mason/Lexer.pm »
1 # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved.
2 # This program is free software; you can redistribute it and/or modify it
3 # under the same terms as Perl itself.
4  
5 package HTML::Mason::Compiler;
6 31 $HTML::Mason::Compiler::VERSION = '1.58';
7 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use HTML::Mason::Component::FileBased;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use HTML::Mason::Component::Subcomponent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 ▶use HTML::Mason::Exceptions( abbr => [qw(param_error compiler_error syntax_error)] );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use HTML::Mason::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 ▶use HTML::Mason::Tools qw(checksum);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶Params::Validate::validation_options( on_fail => sub { param_error join '', @_ } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18 31 ▶use Class::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 31 ▶use base qw(Class::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 BEGIN
22 {
23 31 ▶ __PACKAGE__->valid_params
Subroutine BEGIN: called CC=2 SCAR=7.4
24 (
25 allow_globals =>
26 { parse => 'list', type => ARRAYREF, default => [],
27 descr => "An array of names of Perl variables that are allowed globally within components" },
28  
29 default_escape_flags =>
30 { parse => 'string', type => SCALAR|ARRAYREF, default => [],
31 descr => "Escape flags that will apply by default to all Mason tag output" },
32  
33 enable_autoflush =>
34 { parse => 'boolean', type => SCALAR, default => 1,
35 descr => "Whether to include support for autoflush when compiling components" },
36  
37 lexer =>
38 { isa => 'HTML::Mason::Lexer',
39 descr => "A Lexer object that will scan component text during compilation" },
40  
41 preprocess =>
42 { parse => 'code', type => CODEREF, optional => 1,
43 descr => "A subroutine through which all component text will be sent during compilation" },
44  
45 postprocess_perl =>
46 { parse => 'code', type => CODEREF, optional => 1,
47 descr => "A subroutine through which all Perl code will be sent during compilation" },
48  
49 postprocess_text =>
50 { parse => 'code', type => CODEREF, optional => 1,
51 descr => "A subroutine through which all plain text will be sent during compilation" },
52  
53 use_source_line_numbers =>
54 { parse => 'boolean', type => SCALAR, default => 1,
55 descr => "Whether to use source line numbers in errors and debugger" },
56 );
57  
58 31 __PACKAGE__->contained_objects
59 ( lexer => { class => 'HTML::Mason::Lexer',
60 descr => "This class generates compiler events based on the components source" },
61 );
62  
63 # Define an IN_PERL_DB compile-time constant indicating whether we are
64 # in the Perl debugger. This is used in the object file to
65 # determine whether to call $m->debug_hook.
66 #
67 31 ▶ if (defined($DB::sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DB::sub) { } 0 31 31
68 0 *IN_PERL_DB = sub () { 1 };
69 } else {
70 31 *IN_PERL_DB = sub () { 0 };
71 }
72 }
73  
74 use HTML::Mason::MethodMaker
75 31 ( read_only => [qw(
76 enable_autoflush
77 lexer
78 object_id
79 preprocess
80 postprocess_perl
81 postprocess_text
82 use_source_line_numbers
83 )
84 ],
85 31 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 31 my $old_escape_re = qr/^[hnu]+$/;
88  
89 sub new
90 {
91 415 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
92 415 my $self = $class->SUPER::new(@_);
93  
94 $self->default_escape_flags( $self->{default_escape_flags} )
95 415 ▶ if defined $self->{default_escape_flags};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"default_escape_flags"} 415 0 415
96  
97 # Verify the validity of the global names
98 415 $self->allow_globals( @{$self->{allow_globals}} );
99  
100 # Compute object_id once, on the assumption that all of compiler's
101 # and lexer's parameters are read-only.
102 415 $self->compute_object_id;
103
104 415 return $self;
105 }
106  
107 sub compute_object_id
108 {
109 415 ▶ my $self = shift;
Subroutine compute_object_id: called CC=3 SCAR=11.0
Pod undocumented
110  
111 # Can't use object keys because they stringify differently every
112 # time the program is loaded, whether they are a reference to the
113 # same object or not.
114 415 my $spec = $self->validation_spec;
115 my @id_keys =
116 7056 ▶ ( grep { ! exists $spec->{$_}{isa} && ! exists $spec->{$_}{can} }
Condition66%2/3
exists $spec->{$_}{'isa'} || exists $spec->{$_}{'can'}
l!l&&r!l&&!r
41506641
MC/DC50%1/2
exists $spec->{$_}{'isa'} || exists $spec->{$_}{'can'}
exists $spec->{$_}{'isa'} exists $spec->{$_}{'can'}
Truth table66%2/3
A || B
A= exists $spec->{$_}{'isa'}B= exists $spec->{$_}{'can'}
ABresult
1X1
011
000
117 415 grep { $_ ne 'container' } keys %$spec );
118  
119 415 my @vals = ('HTML::Mason::VERSION', $HTML::Mason::VERSION);
120 415 foreach my $k ( sort @id_keys ) {
121 6641 push @vals, $k, $self->{$k};
122 }
123 415 my $dumped_vals = Data::Dumper->new(\@vals)->Indent(0)->Sortkeys(1)->Dump;
124 415 $self->{object_id} = checksum($dumped_vals);
125 }
126  
127 31 my %top_level_only_block = map { $_ => 1 } qw( cleanup once shared );
128 31 my %valid_comp_flag = map { $_ => 1 } qw( inherit );
129  
130 sub add_allowed_globals
131 {
132 12 ▶ my $self = shift;
Subroutine add_allowed_globals: called CC=2 SCAR=7.0
Pod undocumented
133 12 my @globals = @_;
134  
135 12 ▶ if ( my @bad = grep { ! /^[\$@%]/ } @globals )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad) = grep({not /^[\$\@%]/;} @globals)) 0 12 12
136 {
137 0 param_error "add_allowed_globals: bad parameters '@bad', must begin with one of \$, \@, %\n";
138 }
139  
140 12 $self->{allow_globals} = [ sort keys %{ { map { $_ => 1 } @globals, @{ $self->{allow_globals} } } } ];
141 12 return @{ $self->{allow_globals} };
142 }
143  
144 sub allow_globals
145 {
146 987 ▶ my $self = shift;
Subroutine allow_globals: called CC=4 SCAR=14.2
Pod documented
147  
148 987 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 982 987
149 {
150 5 $self->{allow_globals} = [];
151 5 ▶ return if @_ == 1 and not defined $_[0]; # @_ is (undef)
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not defined $_[0] 0 5 5
Condition33%1/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
050
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
152 5 $self->add_allowed_globals(@_);
153 }
154  
155 987 return @{ $self->{allow_globals} };
156 }
157  
158 sub default_escape_flags
159 {
160 437 ▶ my $self = shift;
Subroutine default_escape_flags: called CC=5 SCAR=17.0
Pod documented
161  
162 437 ▶ return $self->{default_escape_flags} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 22 415 437
163  
164 415 my $flags = shift;
165  
166 415 ▶ unless ( defined $flags )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $flags) 0 415 415
167 {
168 0 $self->{default_escape_flags} = [];
169 0 return;
170 }
171  
172 # make sure this is always an arrayref
173 415 ▶ unless ( ref $flags )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $flags) 4 411 415
174 {
175 4 ▶ if ( $flags =~ /^[hu]+$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /^[hu]+$/) { } 4 0 4
176 {
177 4 $self->{default_escape_flags} = [ split //, $flags ];
178 }
179 else
180 {
181 0 $self->{default_escape_flags} = [ $flags ];
182 }
183 }
184  
185 415 return $self->{default_escape_flags};
186 }
187  
188 sub compile
189 {
190 588 ▶ my $self = shift;
Subroutine compile: called CC=5 SCAR=16.1
Pod documented
191 588 my %p = validate( @_, { comp_source => { type => SCALAR|SCALARREF },
192 name => { type => SCALAR },
193 comp_path => { type => SCALAR },
194 fh => { type => HANDLE, optional => 1 },
195 } );
196 588 ▶ my $src = ref($p{comp_source}) ? $p{comp_source} : \$p{comp_source};
Branch: 1 branch
BranchTrueFalseTotal
ref $p{'comp_source'} ? : 552 36 588
197  
198 # The current compile - initially the main component, then each subcomponent/method
199 588 local $self->{current_compile} = {};
200
201 # Useful for implementing features that affect both main body and methods/subcomps
202 588 local $self->{main_compile} = $self->{current_compile};
203  
204 # So we're re-entrant in subcomps
205 588 local $self->{paused_compiles} = [];
206  
207 588 local $self->{comp_path} = $p{comp_path};
208  
209 # Preprocess the source. The preprocessor routine is handed a
210 # reference to the entire source.
211 588 ▶ if ($self->preprocess)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preprocess) 1 587 588
212 {
213 1 eval { $self->preprocess->( $src ) };
214 1 ▶ compiler_error "Error during custom preprocess step: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
215 }
216  
217 588 $self->lexer->lex( comp_source => $src, name => $p{name}, compiler => $self );
218  
219 571 ▶ return $self->compiled_component( exists($p{fh}) ? (fh => $p{fh}) : () );
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'fh'} ? : 537 34 571
220 }
221  
222 sub start_component
223 {
224 588 ▶ my $self = shift;
Subroutine start_component: called CC=1 SCAR=0.0
Pod documented
225 588 my $c = $self->{current_compile};
226  
227 588 $c->{in_main} = 1;
228  
229 588 $c->{in_block} = undef;
230  
231 588 $self->_init_comp_data($c);
232 }
233  
234 sub _init_comp_data
235 {
236 703 ▶ my $self = shift;
Subroutine _init_comp_data: called CC=3 SCAR=11.0
237 703 my $data = shift;
238  
239 703 $data->{body} = '';
240 703 $data->{last_body_code_type} = '';
241  
242 703 foreach ( qw( def method ) )
243 {
244 1406 $data->{$_} = {};
245 }
246  
247 703 $data->{args} = [];
248 703 $data->{flags} = {};
249 703 $data->{attr} = {};
250  
251 703 $data->{comp_with_content_stack} = [];
252  
253 703 foreach ( qw( cleanup filter init once shared ) )
254 {
255 3515 $data->{blocks}{$_} = [];
256 }
257 }
258  
259 sub end_component
260 {
261 588 ▶ my $self = shift;
Subroutine end_component: called CC=2 SCAR=6.9
Pod documented
262 588 my $c = $self->{current_compile};
263  
264 $self->lexer->throw_syntax_error("Not enough component-with-content ending tags found")
265 588 ▶ if @{ $c->{comp_with_content_stack} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$c->{"comp_with_content_stack"};} 2 586 588
266 }
267  
268 sub start_block
269 {
270 293 ▶ my $self = shift;
Subroutine start_block: called CC=4 SCAR=14.3
Pod documented
271 293 my $c = $self->{current_compile};
272 293 my %p = @_;
273  
274 $self->lexer->throw_syntax_error("Cannot define a $p{block_type} section inside a method or subcomponent")
275 293 ▶ if $top_level_only_block{ $p{block_type} } && ! $c->{in_main};
Branch: 1 branch
BranchTrueFalseTotal
if $top_level_only_block{$p{"block_type"}} and not $c->{"in_main"} 0 293 293
Condition66%2/3
$top_level_only_block{$p{"block_type"}} and not $c->{"in_main"}
!ll&&!rl&&r
267260
MC/DC0%0/2
$top_level_only_block{$p{"block_type"}} and not $c->{"in_main"}
$top_level_only_block{$p{"block_type"}} not $c->{"in_main"}
Truth table66%2/3
A and not(B)
A= $top_level_only_block{$p{"block_type"}}B= not $c->{"in_main"}
ABresult
0X0
100
111
276  
277 $self->lexer->throw_syntax_error("Cannot nest a $p{block_type} inside a $c->{in_block} block")
278 293 ▶ if $c->{in_block};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"in_block"} 0 293 293
279  
280 293 $c->{in_block} = $p{block_type};
281 }
282  
283 sub raw_block
284 {
285 # These blocks contain Perl code - so don't include <%text> and so on.
286  
287 183 ▶ my $self = shift;
Subroutine raw_block: called CC=6 SCAR=18.1
Pod undocumented
288 183 my $c = $self->{current_compile};
289 183 my %p = @_;
290  
291 183 ▶ eval { $self->postprocess_perl->( \$p{block} ) if $self->postprocess_perl };
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 1 182 183
292 183 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 183 183
293  
294 183 my $method = "$p{block_type}_block";
295 183 ▶ return $self->$method(%p) if $self->can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($method) 34 149 183
296  
297 149 my $comment = '';
298 149 ▶ if ( $self->lexer->line_number && $self->use_source_line_numbers )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lexer->line_number and $self->use_source_line_numbers) 149 0 149
Condition33%1/3
$self->lexer->line_number and $self->use_source_line_numbers
!ll&&!rl&&r
00149
MC/DC0%0/2
$self->lexer->line_number and $self->use_source_line_numbers
$self->lexer->line_number $self->use_source_line_numbers
Truth table33%1/3
A and B
A= $self->lexer->line_numberB= $self->use_source_line_numbers
ABresult
0X0
100
111
299 {
300 149 my $line = $self->lexer->line_number;
301 149 my $file = $self->_escape_filename( $self->lexer->name );
302 149 $comment = qq{#line $line "$file"\n};
303 }
304  
305 149 push @{ $self->{current_compile}{blocks}{ $p{block_type} } }, "$comment$p{block}";
306 }
307  
308 sub doc_block
309 ▶{
Subroutine doc_block: called CC=1 SCAR=0.0
Pod undocumented
310 # Don't do anything - just discard the comment.
311 }
312  
313 sub perl_block
314 {
315 34 ▶ my $self = shift;
Subroutine perl_block: called CC=1 SCAR=0.0
Pod undocumented
316 34 my %p = @_;
317  
318 34 $self->_add_body_code( $p{block} );
319  
320 34 $self->{current_compile}{last_body_code_type} = 'perl_block';
321 }
322  
323 sub text
324 {
325 1204 ▶ my ($self, %p) = @_;
Subroutine text: called CC=5 SCAR=16.1
Pod documented
326 1204 ▶ my $tref = ref($p{text}) ? $p{text} : \$p{text}; # Allow a reference
Branch: 1 branch
BranchTrueFalseTotal
ref $p{'text'} ? : 2 1202 1204
327  
328 1204 ▶ eval { $self->postprocess_text->($tref) } if $self->postprocess_text;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_text 5 1199 1204
329 1204 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1204 1204
330  
331 1204 $$tref =~ s,([\'\\]),\\$1,g;
332  
333 1204 ▶ if ($self->enable_autoflush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_autoflush) { } 1186 18 1204
334 1186 $self->_add_body_code("\$m->print( '", $$tref, "' );\n");
335 } else {
336 18 $self->_add_body_code("\$\$_outbuf .= '", $$tref, "';\n");
337 }
338  
339 1204 $self->{current_compile}{last_body_code_type} = 'text';
340 }
341  
342 sub text_block
343 {
344 2 ▶ my $self = shift;
Subroutine text_block: called CC=1 SCAR=0.0
Pod undocumented
345 2 my %p = @_;
346 2 $self->text(text => \$p{block});
347 }
348  
349 sub end_block
350 {
351 291 ▶ my $self = shift;
Subroutine end_block: called CC=2 SCAR=7.0
Pod documented
352 291 my $c = $self->{current_compile};
353 291 my %p = @_;
354  
355 $self->lexer->throw_syntax_error("End of $p{block_type} encountered while in $c->{in_block} block")
356 291 ▶ unless $c->{in_block} eq $p{block_type};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"in_block"} eq $p{"block_type"} 0 291 291
357  
358 291 $c->{in_block} = undef;
359 }
360  
361 sub variable_declaration
362 {
363 95 ▶ my $self = shift;
Subroutine variable_declaration: called CC=3 SCAR=11.1
Pod documented
364 95 my %p = @_;
365  
366 $self->lexer->throw_syntax_error("variable_declaration called inside a $p{block_type} block")
367 95 ▶ unless $p{block_type} eq 'args';
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"block_type"} eq "args" 0 95 95
368  
369 95 my $arg = "$p{type}$p{name}";
370  
371 $self->lexer->throw_syntax_error("$arg already defined")
372 95 ▶ if grep { "$_->{type}$_->{name}" eq $arg } @{ $self->{current_compile}{args} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {qq[$_->{"type"}$_->{"name"}] eq $arg;} @{$$self{"current_compile"}{"args"};} 0 95 95
373  
374 95 push @{ $self->{current_compile}{args} }, { type => $p{type},
375 name => $p{name},
376 default => $p{default},
377 95 line => $self->lexer->line_number,
378 file => $self->lexer->name,
379 };
380 }
381  
382 sub key_value_pair
383 {
384 62 ▶ my $self = shift;
Subroutine key_value_pair: called CC=5 SCAR=16.5
Pod documented
385 62 my %p = @_;
386  
387 compiler_error "key_value_pair called inside a $p{block_type} block"
388 62 ▶ unless $p{block_type} eq 'flags' || $p{block_type} eq 'attr';
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"block_type"} eq "flags" or $p{"block_type"} eq "attr" 0 62 62
Condition66%2/3
$p{"block_type"} eq "flags" or $p{"block_type"} eq "attr"
l!l&&r!l&&!r
19430
MC/DC0%0/2
$p{"block_type"} eq "flags" or $p{"block_type"} eq "attr"
$p{"block_type"} eq "flags" $p{"block_type"} eq "attr"
Truth table66%2/3
A or B
A= $p{"block_type"} eq "flags"B= $p{"block_type"} eq "attr"
ABresult
1X1
011
000
389  
390 62 ▶ my $type = $p{block_type} eq 'flags' ? 'flag' : 'attribute';
Branch: 1 branch
BranchTrueFalseTotal
$p{'block_type'} eq 'flags' ? : 19 43 62
391 $self->lexer->throw_syntax_error("$p{key} $type already defined")
392 62 ▶ if exists $self->{current_compile}{ $p{block_type} }{ $p{key} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current_compile"}{$p{"block_type"}}{$p{"key"}} 0 62 62
393  
394 $self->{current_compile}{ $p{block_type} }{ $p{key} } = $p{value}
395 62 }
396  
397 sub start_named_block
398 {
399 120 ▶ my $self = shift;
Subroutine start_named_block: called CC=7 SCAR=19.5
Pod documented
400 120 my $c = $self->{current_compile};
401 120 my %p = @_;
402  
403 # Error if defining one def or method inside another
404 $self->lexer->throw_syntax_error
405 ("Cannot define a $p{block_type} block inside a method or subcomponent")
406 120 ▶ unless $c->{in_main};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"in_main"} 0 120 120
407  
408 # Error for invalid character in name
409 $self->lexer->throw_syntax_error("Invalid $p{block_type} name: $p{name}")
410 120 ▶ if $p{name} =~ /[^.\w-]/;
Branch: 1 branch
BranchTrueFalseTotal
if $p{"name"} =~ /[^.\w-]/ 2 118 120
411  
412 # Error if two defs or two methods defined with same name
413 $self->lexer->throw_syntax_error
414 (sprintf("Duplicate definition of %s '%s'",
415 $p{block_type} eq 'def' ? 'subcomponent' : 'method', $p{name}))
416 118 ▶ if exists $c->{$p{block_type}}{ $p{name} };
Branch: 2 branches
BranchTrueFalseTotal
if exists $$c{$p{"block_type"}}{$p{"name"}} 2 116 118
$p{'block_type'} eq 'def' ? : 1 1 2
417
418 # Error if def and method defined with same name
419 116 ▶ my $other_type = $p{block_type} eq 'def' ? 'method' : 'def';
Branch: 1 branch
BranchTrueFalseTotal
$p{'block_type'} eq 'def' ? : 58 58 116
420 $self->lexer->throw_syntax_error
421 ("Cannot define a method and subcomponent with the same name ($p{name})")
422 116 ▶ if exists $c->{$other_type}{ $p{name} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{$other_type}{$p{"name"}} 1 115 116
423  
424 115 $c->{in_main}--;
425  
426 115 $c->{ $p{block_type} }{ $p{name} } = {};
427 115 $self->_init_comp_data( $c->{ $p{block_type} }{ $p{name} } );
428 115 push @{$self->{paused_compiles}}, $c;
429 115 $self->{current_compile} = $c->{ $p{block_type} }{ $p{name} };
430 115 $self->{current_compile}->{in_named_block} = {block_type => $p{block_type}, name => $p{name}};
431 }
432  
433 sub end_named_block
434 {
435 114 ▶ my $self = shift;
Subroutine end_named_block: called CC=1 SCAR=0.0
Pod documented
436  
437 114 delete $self->{current_compile}->{in_named_block};
438 114 $self->{current_compile} = pop @{$self->{paused_compiles}};
439 114 $self->{current_compile}{in_main}++;
440 }
441  
442 sub substitution
443 {
444 409 ▶ my $self = shift;
Subroutine substitution: called CC=13 SCAR=25.7
Pod documented
445 409 my %p = @_;
446  
447 409 my $text = $p{substitution};
448  
449 # This is a comment tag if all lines of text contain only whitespace
450 # or start with whitespace and a comment marker, e.g.
451 #
452 # <%
453 # #
454 # # foo
455 # %>
456 #
457 409 my @lines = split(/\n/, $text);
458 409 ▶ unless (grep { /^\s*[^\s\#]/ } @lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/^\s*[^\s\#]/;} @lines) 4 405 409
459 4 $self->{current_compile}{last_body_code_type} = 'substitution';
460 4 return;
461 }
462  
463 405 ▶ if ( ( exists $p{escape} && defined $p{escape} ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{"escape"} and defined $p{"escape"} or @{$self->{"default_escape_flags"};}) 29 376 405
Condition83%5/6
exists $p{"escape"} and defined $p{"escape"} or @{$self->{"default_escape_flags"};}
l!l&&r!l&&!r
227376
exists $p{"escape"} and defined $p{"escape"}
!ll&&!rl&&r
038322
MC/DC66%2/3
exists $p{"escape"} and defined $p{"escape"} or @{$self->{"default_escape_flags"};}
exists $p{"escape"} defined $p{"escape"} @{$self->{"default_escape_flags"};}
Truth table60%3/5
A and B or C
A= exists $p{"escape"}B= defined $p{"escape"}C= @{$self->{"default_escape_flags"};}
ABCresult
11X1
0X11
1011
0X00
1000
464 383 @{ $self->{default_escape_flags} }
465 )
466 {
467 29 my @flags;
468 29 ▶ if ( defined $p{escape} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p{"escape"}) 22 7 29
469 {
470 22 $p{escape} =~ s/\s+$//;
471  
472 22 ▶ if ( $p{escape} =~ /$old_escape_re/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'escape'} =~ /$old_escape_re/) { } 14 8 22
473 {
474 14 @flags = split //, $p{escape};
475 }
476 else
477 {
478 8 @flags = split /\s*,\s*/, $p{escape};
479 }
480 }
481  
482 # is there any way to check the flags for validity and still
483 # allow them to be dynamically set from components?
484  
485 22 unshift @flags, @{ $self->default_escape_flags }
486 29 ▶ unless grep { $_ eq 'n' } @flags;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "n";} @flags 22 7 29
487  
488 29 my %seen;
489 my $flags =
490 ( join ', ',
491 32 ▶ map { $seen{$_}++ ? () : "'$_'" }
Branch: 1 branch
BranchTrueFalseTotal
$seen{$_}++ ? : 8 24 32
492 29 grep { $_ ne 'n' } @flags
493 );
494  
495 29 ▶ $text = "(map {; \$m->interp->apply_escapes(\$_, $flags) } ($text))"
Branch: 1 branch
BranchTrueFalseTotal
if $flags 24 5 29
496 if $flags;
497 }
498  
499 405 my $code;
500  
501 # Make sure to allow lists within <% %> tags.
502 #
503 405 ▶ if ($self->enable_autoflush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enable_autoflush) { } 397 8 405
504 397 $code = "\$m->print( $text );\n";
505 } else {
506 # more efficient output form when autoflush is disabled. only
507 # output defined bits, which is what $m->print does internally
508 # as well. use 'if defined' for maximum efficiency; grep
509 # creates a list.
510 8 $code = "for ( $text ) { \$\$_outbuf .= \$_ if defined }\n";
511 }
512  
513 405 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 3 402 405
514 405 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 405 405
515  
516 405 $self->_add_body_code($code);
517  
518 405 $self->{current_compile}{last_body_code_type} = 'substitution';
519 }
520  
521 sub component_call
522 {
523 241 ▶ my $self = shift;
Subroutine component_call: called CC=5 SCAR=16.2
Pod documented
524 241 my %p = @_;
525  
526 241 my ($prespace, $call, $postspace) = ($p{call} =~ /(\s*)(.*)(\s*)/s);
527 241 ▶ if ( $call =~ m,^[\w/.],)
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ m[^[\w/.]]) 228 13 241
528 {
529 228 my $comma = index($call, ',');
530 228 ▶ $comma = length $call if $comma == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $comma == -1 156 72 228
531 228 (my $comp = substr($call, 0, $comma)) =~ s/\s+$//;
532 228 $call = "'$comp'" . substr($call, $comma);
533 }
534 241 my $code = "\$m->comp( $prespace $call $postspace \n); ";
535 241 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 0 241 241
536 241 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 241 241
537  
538 241 $self->_add_body_code($code);
539  
540 241 $self->{current_compile}{last_body_code_type} = 'component_call';
541 }
542  
543 sub component_content_call
544 {
545 42 ▶ my $self = shift;
Subroutine component_content_call: called CC=4 SCAR=14.0
Pod documented
546 42 my $c = $self->{current_compile};
547 42 my %p = @_;
548  
549 42 my $call = $p{call};
550 42 for ($call) { s/^\s+//; s/\s+$//; }
551 42 push @{ $c->{comp_with_content_stack} }, $call;
552  
553 42 my $code = "\$m->comp( { content => sub {\n";
554 42 $code .= $self->_set_buffer();
555  
556 42 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 0 42 42
557 42 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 42 42
558  
559 42 $self->_add_body_code($code);
560  
561 42 $c->{last_body_code_type} = 'component_content_call';
562 }
563  
564 sub component_content_call_end
565 {
566 42 ▶ my $self = shift;
Subroutine component_content_call_end: called CC=11 SCAR=24.0
Pod documented
567 42 my $c = $self->{current_compile};
568 42 my %p = @_;
569  
570 $self->lexer->throw_syntax_error("Found component with content ending tag but no beginning tag")
571 42 ▶ unless @{ $c->{comp_with_content_stack} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$c->{"comp_with_content_stack"};} 2 40 42
572  
573 40 my $call = pop @{ $c->{comp_with_content_stack} };
574 40 my $call_end = $p{call_end};
575 40 for ($call_end) { s/^\s+//; s/\s+$//; }
576  
577 40 my $comp = undef;
578 40 ▶ if ( $call =~ m,^[\w/.],)
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ m[^[\w/.]]) 36 4 40
579 {
580 36 my $comma = index($call, ',');
581 36 ▶ $comma = length $call if $comma == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $comma == -1 28 8 36
582 36 ($comp = substr($call, 0, $comma)) =~ s/\s+$//;
583 36 $call = "'$comp'" . substr($call, $comma);
584 }
585 40 ▶ if ($call_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_end) 7 33 40
586 7 ▶ if ($call_end !~ m,^[\w/.],) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($call_end =~ m[^[\w/.]]) 2 5 7
587 2 $self->lexer->throw_syntax_error("Cannot use an expression inside component with content ending tag; use a bare component name or </&> instead");
588 }
589 5 ▶ if (!defined($comp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $comp) 1 4 5
590 1 $self->lexer->throw_syntax_error("Cannot match an expression as a component name; use </&> instead");
591 }
592 4 ▶ if ($call_end ne $comp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_end ne $comp) 1 3 4
593 1 $self->lexer->throw_syntax_error("Component name in ending tag ($call_end) does not match component name in beginning tag ($comp)");
594 }
595 }
596  
597 36 my $code = "} }, $call\n );\n";
598  
599 36 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 0 36 36
600 36 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 36 36
601  
602 36 $self->_add_body_code($code);
603  
604 36 $c->{last_body_code_type} = 'component_content_call_end';
605 }
606  
607 sub perl_line
608 {
609 407 ▶ my $self = shift;
Subroutine perl_line: called CC=3 SCAR=11.0
Pod documented
610 407 my %p = @_;
611  
612 407 my $code = "$p{line}\n";
613  
614 407 ▶ eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
Branch: 1 branch
BranchTrueFalseTotal
if $self->postprocess_perl 1 406 407
615 407 ▶ compiler_error $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 407 407
616  
617 407 $self->_add_body_code($code);
618  
619 407 $self->{current_compile}{last_body_code_type} = 'perl_line';
620 }
621  
622 sub _add_body_code
623 {
624 2369 ▶ my $self = shift;
Subroutine _add_body_code: called CC=5 SCAR=16.1
625  
626 # We know a perl-line is always _one_ line, so we know that the
627 # line numbers are going to match up as long as the first line in
628 # a series has a line number comment before it. Adding a comment
629 # can break certain constructs like qw() list that spans multiple
630 # perl-lines.
631 2369 ▶ if ( $self->lexer->line_number &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lexer->line_number and $self->{"current_compile"}{"last_body_code_type"} ne "perl_line" and $self->use_source_line_numbers) 2055 314 2369
Condition83%5/6
$self->lexer->line_number and $self->{"current_compile"}{"last_body_code_type"} ne "perl_line" and $self->use_source_line_numbers
!ll&&!rl&&r
303112055
$self->lexer->line_number and $self->{"current_compile"}{"last_body_code_type"} ne "perl_line"
!ll&&!rl&&r
03032066
MC/DC66%2/3
$self->lexer->line_number and $self->{"current_compile"}{"last_body_code_type"} ne "perl_line" and $self->use_source_line_numbers
$self->lexer->line_number $self->{"current_compile"}{"last_body_code_type"} ne "perl_line" $self->use_source_line_numbers
Truth table75%3/4
A and B and C
A= $self->lexer->line_numberB= $self->{"current_compile"}{"last_body_code_type"} ne "perl_line"C= $self->use_source_line_numbers
ABCresult
0XX0
10X0
1100
1111
632 $self->{current_compile}{last_body_code_type} ne 'perl_line' &&
633 $self->use_source_line_numbers )
634 {
635 2055 my $line = $self->lexer->line_number;
636 2055 my $file = $self->_escape_filename( $self->lexer->name );
637 2055 $self->{current_compile}{body} .= qq{#line $line "$file"\n};
638 }
639  
640 2369 $self->{current_compile}{body} .= $_ foreach @_;
641 }
642  
643 sub _escape_filename
644 {
645 2292 ▶ my $self = shift;
Subroutine _escape_filename: called CC=1 SCAR=0.0
646 2292 my $file = shift;
647  
648 2292 $file =~ s/\"//g;
649  
650 2292 return $file;
651 }
652  
653 sub dump
654 {
655 0 ▶ my $self = shift;
Subroutine dump: not called CC=3 SCAR=24.8
Pod undocumented
656 0 my $c = $self->{current_compile};
657  
658 0 warn "Main component\n";
659  
660 0 $self->_dump_data( $c );
661  
662 0 foreach ( keys %{ $c->{def} } )
663 {
664 0 warn " Subcomponent $_\n";
665 0 $self->_dump_data( $c->{def}{$_}, ' ' );
666 }
667  
668 0 foreach ( keys %{ $c->{method} } )
669 {
670 0 warn " Methods $_\n";
671 0 $self->_dump_data( $c->{method}{$_}, ' ');
672 }
673 }
674  
675 sub _dump_data
676 {
677 0 ▶ my $self = shift;
Subroutine _dump_data: not called CC=5 SCAR=34.0
678 0 my $data = shift;
679 0 ▶ my $indent = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
680  
681 0 ▶ if ( @{ $data->{args} } )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$data->{"args"};}) 0 0 0
682 {
683 0 warn "$indent args\n";
684 0 foreach ( @{ $data->{args} } )
685 {
686 0 warn "$indent $_->{type}$_->{name}";
687 0 ▶ warn " => $_->{default}" if defined $_->{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"default"} 0 0 0
688 0 warn "\n";
689 }
690 }
691  
692 0 warn "\n$indent body\n";
693 0 warn $data->{body}, "\n";
694 }
695  
696 sub _blocks
697 {
698 5942 ▶ my $self = shift;
Subroutine _blocks: called CC=1 SCAR=0.0
699  
700 5942 return @{ $self->{current_compile}{blocks}{ shift() } };
701 }
702  
703 sub HTML::Mason::Parser::new
704 {
705 0 ▶ die "The Parser module is no longer a part of HTML::Mason. Please see ".
Subroutine new: not called CC=1 SCAR=6.9
706 "the Lexer and Compiler modules, its replacements.\n";
707 }
708  
709 31 1;
710  
711 __END__
« lib/HTML/Mason/Interp.pm ↑ Summary lib/HTML/Mason/Lexer.pm »