| 1 | # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package HTML::Mason::Compiler::ToObject; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 31 | $HTML::Mason::Compiler::ToObject::VERSION = '1.60'; | |||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 31 | ▶ | use Params::Validate qw(BOOLEAN SCALAR validate); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 31 | ▶ | use HTML::Mason::Tools qw(taint_is_on); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 31 | ▶ | use HTML::Mason::Compiler; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 31 | ▶ | use base qw( HTML::Mason::Compiler ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 31 | ▶ | use HTML::Mason::Exceptions( abbr => [qw(wrong_compiler_error system_error)] ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 31 | ▶ | use File::Path qw(mkpath rmtree); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 31 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | BEGIN | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 31 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 31 | __PACKAGE__->valid_params | |||||||||||||||||||||||||||||||||||||||||
| 24 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 25 | comp_class => | ||||||||||||||||||||||||||||||||||||||||||
| 26 | { parse => 'string', type => SCALAR, default => 'HTML::Mason::Component', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | descr => "The class into which component objects will be blessed" }, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | subcomp_class => | ||||||||||||||||||||||||||||||||||||||||||
| 30 | { parse => 'string', type => SCALAR, default => 'HTML::Mason::Component::Subcomponent', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | descr => "The class into which subcomponent objects will be blessed" }, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | in_package => | ||||||||||||||||||||||||||||||||||||||||||
| 34 | { parse => 'string', type => SCALAR, default => 'HTML::Mason::Commands', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | descr => "The package in which component execution will take place" }, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | preamble => | ||||||||||||||||||||||||||||||||||||||||||
| 38 | { parse => 'string', type => SCALAR, default => '', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | descr => "A chunk of Perl code to add to the beginning of each compiled component" }, | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | postamble => | ||||||||||||||||||||||||||||||||||||||||||
| 42 | { parse => 'string', type => SCALAR, default => '', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | descr => "A chunk of Perl code to add to the end of each compiled component" }, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | use_strict => | ||||||||||||||||||||||||||||||||||||||||||
| 46 | { parse => 'boolean', type => SCALAR, default => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | descr => "Whether to turn on Perl's 'strict' pragma in components" }, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | use_warnings => | ||||||||||||||||||||||||||||||||||||||||||
| 50 | { parse => 'boolean', type => SCALAR, default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | descr => "Whether to turn on Perl's 'warnings' pragma in components" }, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | define_args_hash => | ||||||||||||||||||||||||||||||||||||||||||
| 54 | { parse => 'string', type => SCALAR, default => 'auto', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | regex => qr/^(?:always|auto|never)$/, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | descr => "Whether or not to create the %ARGS hash" }, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | named_component_subs => | ||||||||||||||||||||||||||||||||||||||||||
| 59 | { parse => 'boolean', type => BOOLEAN, default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | descr => "Whether to use named subroutines for component code" }, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | use HTML::Mason::MethodMaker | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 31 | ▶ | ( read_only => [ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | qw( comp_class | ||||||||||||||||||||||||||||||||||||||||||
| 67 | define_args_hash | ||||||||||||||||||||||||||||||||||||||||||
| 68 | in_package | ||||||||||||||||||||||||||||||||||||||||||
| 69 | named_component_subs | ||||||||||||||||||||||||||||||||||||||||||
| 70 | postamble | ||||||||||||||||||||||||||||||||||||||||||
| 71 | preamble | ||||||||||||||||||||||||||||||||||||||||||
| 72 | subcomp_class | ||||||||||||||||||||||||||||||||||||||||||
| 73 | use_strict | ||||||||||||||||||||||||||||||||||||||||||
| 74 | use_warnings | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 31 | ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | sub compile | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 588 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 588 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 588 | my %p = @_; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 588 | ▶ | local $self->{comp_class} = delete $p{comp_class} if exists $p{comp_class}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 588 | return $self->SUPER::compile( %p ); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # compile_to_file( source => ..., file => ... ) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # Save object text in an object file. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # We attempt to handle several cases in which a file already exists | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # and we wish to create a directory, or vice versa. However, not | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # every case is handled; to be complete, mkpath would have to unlink | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # any existing file in its way. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||||||||||
| 97 | sub compile_to_file | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 553 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile_to_file: called CC=10 SCAR=23.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 553 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 553 | my %p = validate( @_, { file => { type => SCALAR }, | |||||||||||||||||||||||||||||||||||||||||
| 102 | source => { isa => 'HTML::Mason::ComponentSource' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 553 | my ($file, $source) = @p{qw(file source)}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 553 | my @newfiles = ($file); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 553 | ▶ | if (defined $file && !-f $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $file and not -f $file
MC/DC50%1/2
defined $file and not -f $file
defined $file
not -f $file
Truth table66%2/3
A and not(B)
A= defined $fileB= not -f $file
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 539 | my ($dirname) = dirname($file); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 539 | ▶ | if (!-d $dirname) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 102 | ▶ | unlink($dirname) if (-e _); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 102 | push @newfiles, mkpath($dirname, 0, 0775); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 102 | ▶ | system_error "Couldn't create directory $dirname: $!" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | unless -d $dirname; | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 539 | ▶ | rmtree($file) if (-d $file); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 553 | ▶ | ($file) = $file =~ /^(.*)/s if taint_is_on; # Untaint blindly | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 553 | ▶ | open my $fh, "> $file" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | or system_error "Couldn't create object file $file: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 553 | $self->compile( comp_source => $source->comp_source_ref, | |||||||||||||||||||||||||||||||||||||||||
| 125 | name => $source->friendly_name, | ||||||||||||||||||||||||||||||||||||||||||
| 126 | comp_class => $source->comp_class, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | comp_path => $source->comp_path, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | fh => $fh ); | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 537 | ▶ | close $fh | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | or system_error "Couldn't close object file $file: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 537 | return \@newfiles; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | sub _output_chunk | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 2289 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_output_chunk: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 2289 | my ($self, $fh, $string) = (shift, shift, shift); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 2289 | ▶ | if ($fh) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2153 | ▶ | print $fh (ref $_ ? $$_ : $_) foreach grep defined, @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | else | ||||||||||||||||||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 136 | ▶ | $$string .= (ref $_ ? $$_ : $_) foreach @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | # There are some really spooky relationships between the variables & | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # data members in the compiled_component() routine. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | sub compiled_component | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 571 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compiled_component: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 571 | my ($self, %p) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 571 | my $c = $self->{current_compile}; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 571 | my $obj_text = ''; | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 571 | ▶ | local $c->{compiled_def} = $self->_compile_subcomponents if %{ $c->{def} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 571 | ▶ | local $c->{compiled_method} = $self->_compile_methods if %{ $c->{method} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | # Some preamble stuff, including 'use strict', 'use vars', and <%once> block | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 571 | my $header = $self->_make_main_header; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 571 | $self->_output_chunk($p{fh}, \$obj_text, $header); | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 571 | my $params = $self->_component_params; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 571 | $params->{load_time} = time; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 571 | ▶ | $params->{subcomps} = '\%_def' if %{ $c->{def} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 571 | ▶ | $params->{methods} = '\%_method' if %{ $c->{method} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 571 | ▶ | if ( $self->_blocks('shared') ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 11 | my %subs; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 11 | ▶ | while ( my ($name, $pref) = each %{ $c->{compiled_def} } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 3 | my $key = "subcomponent_$name"; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 3 | $subs{$key} = $pref->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 3 | $pref->{code} = "sub {\nHTML::Mason::Request->instance->call_dynamic('$key',\@_)\n}"; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 11 | ▶ | while (my ($name, $pref) = each %{ $c->{compiled_method} } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | my $key = "method_$name"; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | $subs{$key} = $pref->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 4 | $pref->{code} = "sub {\nHTML::Mason::Request->instance->call_dynamic( '$key', \@_ )\n}"; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 11 | $subs{main} = $params->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 11 | $params->{code} = "sub {\nHTML::Mason::Request->instance->call_dynamic( 'main', \@_ )\n}"; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 11 | my $named_subs = ''; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 11 | my %named_subs = $self->_named_subs_hash; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 11 | ▶ | while ( my ( $name, $body ) = each %named_subs ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | { | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | $named_subs .= '*' . $name . " = sub {\n" . $body . "\n};\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | $params->{dynamic_subs_init} = | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 11 | join '', ( "sub {\n", | |||||||||||||||||||||||||||||||||||||||||
| 199 | $self->_set_request, | ||||||||||||||||||||||||||||||||||||||||||
| 200 | $self->_blocks('shared'), | ||||||||||||||||||||||||||||||||||||||||||
| 201 | $named_subs, | ||||||||||||||||||||||||||||||||||||||||||
| 202 | "return {\n", | ||||||||||||||||||||||||||||||||||||||||||
| 203 | map( "'$_' => $subs{$_},\n", sort keys %subs ), | ||||||||||||||||||||||||||||||||||||||||||
| 204 | "\n}\n}" | ||||||||||||||||||||||||||||||||||||||||||
| 205 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | else | ||||||||||||||||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 560 | my %named_subs = $self->_named_subs_hash; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 560 | ▶ | while ( my ( $name, $body ) = each %named_subs ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 5 | $self->_output_chunk( $p{fh}, \$obj_text, | |||||||||||||||||||||||||||||||||||||||||
| 213 | "sub $name {\n" . $body . "\n}\n" | ||||||||||||||||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 571 | $self->_output_chunk($p{fh}, \$obj_text, $self->_subcomponents_footer); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 571 | $self->_output_chunk($p{fh}, \$obj_text, $self->_methods_footer); | |||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 571 | $self->_output_chunk($p{fh}, \$obj_text, | |||||||||||||||||||||||||||||||||||||||||
| 222 | $self->_constructor( $self->comp_class, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | $params ), | ||||||||||||||||||||||||||||||||||||||||||
| 224 | ';', | ||||||||||||||||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 571 | return \$obj_text; | |||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | sub _named_subs_hash | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 571 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_named_subs_hash: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 571 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 571 | ▶ | return unless $self->named_component_subs; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | 4 | my %subs; | |||||||||||||||||||||||||||||||||||||||||
| 237 | 4 | $subs{ $self->_sub_name } = $self->_body; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 4 | ▶ | while ( my ( $name, $params ) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 5 | each %{ $self->{current_compile}{compiled_def} } ) | |||||||||||||||||||||||||||||||||||||||||
| 241 | { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 1 | $subs{ $self->_sub_name( 'def', $name ) } = $params->{body}; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 4 | ▶ | while ( my ( $name, $params ) = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 6 | each %{ $self->{current_compile}{compiled_method} } ) | |||||||||||||||||||||||||||||||||||||||||
| 247 | { | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | $subs{ $self->_sub_name( 'method', $name ) } = $params->{body}; | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 4 | return %subs; | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | sub _sub_name | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 14 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sub_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 14 | return join '_', $self->_escape_sub_name_part( $self->{comp_path}, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | sub _escape_sub_name_part | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 14 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_sub_name_part: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | 14 | return map { my $part = $_; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 26 | $part =~ s/([^\w_])/'_' . sprintf( '%x', ord $1 )/ge; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 26 | $part; } @_; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | sub _compile_subcomponents | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 44 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_subcomponents: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 44 | return $self->_compile_subcomponents_or_methods('def'); | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | sub _compile_methods | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 36 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_methods: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 36 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 36 | return $self->_compile_subcomponents_or_methods('method'); | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | sub _compile_subcomponents_or_methods | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 80 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compile_subcomponents_or_methods: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 80 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 80 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | 80 | my %compiled; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 80 | foreach ( keys %{ $self->{current_compile}{$type} } ) | |||||||||||||||||||||||||||||||||||||||||
| 291 | { | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 111 | local $self->{current_compile} = $self->{current_compile}{$type}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 111 | local $self->{current_compile}->{in_named_block} = {type => $type, name => $_}; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 111 | $compiled{$_} = $self->_component_params; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 80 | return \%compiled; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | sub _make_main_header | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 571 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_main_header: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 571 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | 571 | my $pkg = $self->in_package; | |||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 571 | ▶ | return join '', ( "package $pkg;\n", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | $self->use_strict ? "use strict;\n" : "no strict;\n", | ||||||||||||||||||||||||||||||||||||||||||
| 308 | $self->use_warnings ? "use warnings;\n" : "", | ||||||||||||||||||||||||||||||||||||||||||
| 309 | sprintf( "use vars qw(\%s);\n", | ||||||||||||||||||||||||||||||||||||||||||
| 310 | join ' ', '$m', $self->allow_globals ), | ||||||||||||||||||||||||||||||||||||||||||
| 311 | $self->_blocks('once'), | ||||||||||||||||||||||||||||||||||||||||||
| 312 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | sub _subcomponents_footer | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 571 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_subcomponents_footer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 571 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 571 | return $self->_subcomponent_or_method_footer('def'); | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | sub _methods_footer | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 571 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_methods_footer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | 571 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | 571 | return $self->_subcomponent_or_method_footer('method'); | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | sub _subcomponent_or_method_footer | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 1142 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_subcomponent_or_method_footer: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 1142 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 1142 | my $c = $self->{current_compile}; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 1142 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 1142 | ▶ | return '' unless %{ $c->{$type} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | return join('', | ||||||||||||||||||||||||||||||||||||||||||
| 338 | "my %_$type =\n(\n", | ||||||||||||||||||||||||||||||||||||||||||
| 339 | map( {("'$_' => " , | ||||||||||||||||||||||||||||||||||||||||||
| 340 | $self->_constructor( $self->{subcomp_class}, | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 111 | $c->{"compiled_$type"}{$_} ) , | |||||||||||||||||||||||||||||||||||||||||
| 342 | 80 | ",\n")} keys %{ $c->{"compiled_$type"} } ) , | |||||||||||||||||||||||||||||||||||||||||
| 343 | "\n);\n" | ||||||||||||||||||||||||||||||||||||||||||
| 344 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | sub _constructor | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 682 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_constructor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 682 | my ($self, $class, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | return ("${class}->new(\n", | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 1470 | map( {("'$_' => ", $params->{$_}, ",\n")} | |||||||||||||||||||||||||||||||||||||||||
| 353 | 682 | sort grep { $_ ne 'body' } keys %$params ), | |||||||||||||||||||||||||||||||||||||||||
| 354 | "\n)\n", | ||||||||||||||||||||||||||||||||||||||||||
| 355 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | sub _component_params | ||||||||||||||||||||||||||||||||||||||||||
| 359 | 682 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_component_params: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 682 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 682 | my %params; | |||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 682 | ▶ | if ( $self->named_component_subs ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | $params{code} = | ||||||||||||||||||||||||||||||||||||||||||
| 367 | '\\&' . | ||||||||||||||||||||||||||||||||||||||||||
| 368 | $self->_sub_name | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 14 | ( grep { defined } | |||||||||||||||||||||||||||||||||||||||||
| 370 | 7 | @{ $self->{current_compile}{in_named_block} } | |||||||||||||||||||||||||||||||||||||||||
| 371 | 7 | { 'type', 'name' } ); | |||||||||||||||||||||||||||||||||||||||||
| 372 | 7 | $params{body} = $self->_body; | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | else | ||||||||||||||||||||||||||||||||||||||||||
| 375 | { | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 675 | $params{code} = join '', "sub {\n", $self->_body, "}"; | |||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | $params{flags} = join '', "{\n", $self->_flags, "\n}" | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 682 | ▶ | if keys %{ $self->{current_compile}{flags} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | $params{attr} = join '', "{\n", $self->_attr, "\n}" | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 682 | ▶ | if keys %{ $self->{current_compile}{attr} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | $params{declared_args} = join '', "{\n", $self->_declared_args, "\n}" | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 682 | ▶ | if @{ $self->{current_compile}{args} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | 682 | ▶ | $params{has_filter} = 1 if $self->_blocks('filter'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 682 | return \%params; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | sub _body | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 686 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 686 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | return join '', ( $self->preamble, | ||||||||||||||||||||||||||||||||||||||||||
| 398 | $self->_set_request, | ||||||||||||||||||||||||||||||||||||||||||
| 399 | $self->_set_buffer, | ||||||||||||||||||||||||||||||||||||||||||
| 400 | $self->_arg_declarations, | ||||||||||||||||||||||||||||||||||||||||||
| 401 | $self->_filter, | ||||||||||||||||||||||||||||||||||||||||||
| 402 | "\$m->debug_hook( \$m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );\n\n", | ||||||||||||||||||||||||||||||||||||||||||
| 403 | $self->_blocks('init'), | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | # do not add a block around this, it introduces | ||||||||||||||||||||||||||||||||||||||||||
| 406 | # a separate scope and might break cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 407 | # blocks (or all sort of other things!) | ||||||||||||||||||||||||||||||||||||||||||
| 408 | $self->{current_compile}{body}, | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 686 | $self->_blocks('cleanup'), | |||||||||||||||||||||||||||||||||||||||||
| 411 | $self->postamble, | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # semi before return will help catch syntax | ||||||||||||||||||||||||||||||||||||||||||
| 414 | # errors in component body - don't return values | ||||||||||||||||||||||||||||||||||||||||||
| 415 | # explicitly | ||||||||||||||||||||||||||||||||||||||||||
| 416 | ";return;\n", | ||||||||||||||||||||||||||||||||||||||||||
| 417 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | sub _set_request | ||||||||||||||||||||||||||||||||||||||||||
| 421 | 697 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_request: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 422 | 697 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | 697 | ▶ | return if $self->in_package eq 'HTML::Mason::Commands'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 7 | return 'local $' . $self->in_package . '::m = $HTML::Mason::Commands::m;' . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | sub _set_buffer | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 728 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_buffer: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 431 | 728 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | 728 | ▶ | if ($self->enable_autoflush) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 434 | 719 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 435 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 9 | return 'my $_outbuf = $m->{top_stack}->[HTML::Mason::Request::STACK_BUFFER];' . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 31 | my %coercion_funcs = ( '@' => 'HTML::Mason::Tools::coerce_to_array', | |||||||||||||||||||||||||||||||||||||||||
| 441 | '%' => 'HTML::Mason::Tools::coerce_to_hash', | ||||||||||||||||||||||||||||||||||||||||||
| 442 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 443 | sub _arg_declarations | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 686 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_arg_declarations: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 686 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 686 | my $init; | |||||||||||||||||||||||||||||||||||||||||
| 448 | my @args_hash; | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 686 | my $pos; | |||||||||||||||||||||||||||||||||||||||||
| 450 | 686 | my @req_check; | |||||||||||||||||||||||||||||||||||||||||
| 451 | 686 | my @decl; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 686 | my @assign; | |||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 686 | my $define_args_hash = $self->_define_args_hash; | |||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 686 | ▶ | unless ( @{ $self->{current_compile}{args} } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | { | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 630 | ▶ | return unless $define_args_hash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | 21 | return ( "my \%ARGS;\n", | |||||||||||||||||||||||||||||||||||||||||
| 461 | "{ local \$^W; \%ARGS = \@_ unless (\@_ % 2); }\n" | ||||||||||||||||||||||||||||||||||||||||||
| 462 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | 56 | $init = <<'EOF'; | |||||||||||||||||||||||||||||||||||||||||
| 466 | HTML::Mason::Exception::Params->throw | ||||||||||||||||||||||||||||||||||||||||||
| 467 | ( error => | ||||||||||||||||||||||||||||||||||||||||||
| 468 | "Odd number of parameters passed to component expecting name/value pairs" | ||||||||||||||||||||||||||||||||||||||||||
| 469 | ) if @_ % 2; | ||||||||||||||||||||||||||||||||||||||||||
| 470 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 56 | ▶ | if ( $define_args_hash ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | { | ||||||||||||||||||||||||||||||||||||||||||
| 474 | 2 | @args_hash = "my \%ARGS = \@_;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | # opening brace will be closed later. we want this in a separate | ||||||||||||||||||||||||||||||||||||||||||
| 478 | # block so that the rest of the component can't see %pos | ||||||||||||||||||||||||||||||||||||||||||
| 479 | 56 | $pos = <<'EOF'; | |||||||||||||||||||||||||||||||||||||||||
| 480 | { | ||||||||||||||||||||||||||||||||||||||||||
| 481 | my %pos; | ||||||||||||||||||||||||||||||||||||||||||
| 482 | for ( my $x = 0; $x < @_; $x += 2 ) | ||||||||||||||||||||||||||||||||||||||||||
| 483 | { | ||||||||||||||||||||||||||||||||||||||||||
| 484 | $pos{ $_[$x] } = $x + 1; | ||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | my @required = | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 51 | ( map { $_->{name} } | |||||||||||||||||||||||||||||||||||||||||
| 490 | 94 | grep { ! defined $_->{default} } | |||||||||||||||||||||||||||||||||||||||||
| 491 | 56 | @{ $self->{current_compile}{args} } | |||||||||||||||||||||||||||||||||||||||||
| 492 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | 56 | ▶ | if (@required) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | { | ||||||||||||||||||||||||||||||||||||||||||
| 496 | # just to be sure | ||||||||||||||||||||||||||||||||||||||||||
| 497 | 39 | local $" = ' '; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 39 | @req_check = <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | foreach my \$arg ( qw( @required ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 501 | { | ||||||||||||||||||||||||||||||||||||||||||
| 502 | HTML::Mason::Exception::Params->throw | ||||||||||||||||||||||||||||||||||||||||||
| 503 | ( error => "no value sent for required parameter '\$arg'" ) | ||||||||||||||||||||||||||||||||||||||||||
| 504 | unless exists \$pos{\$arg}; | ||||||||||||||||||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||||||||||||||||
| 506 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 56 | foreach ( @{ $self->{current_compile}{args} } ) | |||||||||||||||||||||||||||||||||||||||||
| 510 | { | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 94 | my $var_name = "$_->{type}$_->{name}"; | |||||||||||||||||||||||||||||||||||||||||
| 512 | 94 | push @decl, $var_name; | |||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | 94 | my $arg_in_array = "\$_[ \$pos{'$_->{name}'} ]"; | |||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | 94 | my $coerce; | |||||||||||||||||||||||||||||||||||||||||
| 517 | 94 | ▶ | if ( $coercion_funcs{ $_->{type} } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 518 | { | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 9 | $coerce = $coercion_funcs{ $_->{type} } . "( $arg_in_array, '$var_name')"; | |||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||
| 521 | else | ||||||||||||||||||||||||||||||||||||||||||
| 522 | { | ||||||||||||||||||||||||||||||||||||||||||
| 523 | 85 | $coerce = $arg_in_array; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | 94 | ▶ | if ( defined $_->{line} && defined $_->{file} && $self->use_source_line_numbers ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $_->{"line"} and defined $_->{"file"} and $self->use_source_line_numbers
defined $_->{"line"} and defined $_->{"file"}
MC/DC33%1/3
defined $_->{"line"} and defined $_->{"file"} and $self->use_source_line_numbers
defined $_->{"line"}
defined $_->{"file"}
$self->use_source_line_numbers
Truth table50%2/4
A and B and C
A= defined $_->{"line"}B= defined $_->{"file"}C= $self->use_source_line_numbers
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | { | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 88 | my $file = $self->_escape_filename( $_->{file} ); | |||||||||||||||||||||||||||||||||||||||||
| 529 | 88 | push @assign, qq{#line $_->{line} "$file"\n}; | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||
| 532 | 94 | ▶ | if ( defined $_->{default} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 533 | { | ||||||||||||||||||||||||||||||||||||||||||
| 534 | 43 | my $default_val = $_->{default}; | |||||||||||||||||||||||||||||||||||||||||
| 535 | # allow for comments after default declaration | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 43 | ▶ | $default_val .= "\n" if defined $_->{default} && $_->{default} =~ /\#/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_->{"default"} and $_->{"default"} =~ /\#/
MC/DC50%1/2
defined $_->{"default"} and $_->{"default"} =~ /\#/
defined $_->{"default"}
$_->{"default"} =~ /\#/
Truth table66%2/3
A and B
A= defined $_->{"default"}B= $_->{"default"} =~ /\#/
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | 43 | push @assign, <<"EOF"; | |||||||||||||||||||||||||||||||||||||||||
| 539 | $var_name = exists \$pos{'$_->{name}'} ? $coerce : $default_val; | ||||||||||||||||||||||||||||||||||||||||||
| 540 | EOF | ||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | else | ||||||||||||||||||||||||||||||||||||||||||
| 543 | { | ||||||||||||||||||||||||||||||||||||||||||
| 544 | 51 | push @assign, | |||||||||||||||||||||||||||||||||||||||||
| 545 | " $var_name = $coerce;\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | } | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | 56 | my $decl = 'my ( '; | |||||||||||||||||||||||||||||||||||||||||
| 550 | 56 | $decl .= join ', ', @decl; | |||||||||||||||||||||||||||||||||||||||||
| 551 | 56 | $decl .= " );\n"; | |||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | # closing brace closes opening of @pos | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 56 | return $init, @args_hash, $decl, $pos, @req_check, @assign, "}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | sub _define_args_hash | ||||||||||||||||||||||||||||||||||||||||||
| 558 | 686 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_define_args_hash: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | 686 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | 686 | ▶ | return 1 if $self->define_args_hash eq 'always'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | 685 | ▶ | return 0 if $self->define_args_hash eq 'never'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | 683 | foreach ( $self->preamble, | |||||||||||||||||||||||||||||||||||||||||
| 565 | $self->_blocks('filter'), | ||||||||||||||||||||||||||||||||||||||||||
| 566 | $self->_blocks('init'), | ||||||||||||||||||||||||||||||||||||||||||
| 567 | $self->{current_compile}{body}, | ||||||||||||||||||||||||||||||||||||||||||
| 568 | $self->_blocks('cleanup'), | ||||||||||||||||||||||||||||||||||||||||||
| 569 | $self->postamble, | ||||||||||||||||||||||||||||||||||||||||||
| 570 | 94 | grep { defined } map { $_->{default} } @{ $self->{current_compile}{args} } | |||||||||||||||||||||||||||||||||||||||||
| 571 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 572 | { | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 2192 | ▶ | return 1 if /ARGS/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | sub _filter | ||||||||||||||||||||||||||||||||||||||||||
| 578 | 686 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_filter: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 686 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 686 | my @filter; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 686 | ▶ | @filter = $self->_blocks('filter') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 583 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | 31 | return ( join '', | |||||||||||||||||||||||||||||||||||||||||
| 586 | "\$m->current_comp->filter( sub { local \$_ = shift;\n", | ||||||||||||||||||||||||||||||||||||||||||
| 587 | ( join ";\n", @filter ), | ||||||||||||||||||||||||||||||||||||||||||
| 588 | ";\n", | ||||||||||||||||||||||||||||||||||||||||||
| 589 | "return \$_;\n", | ||||||||||||||||||||||||||||||||||||||||||
| 590 | "} );\n", | ||||||||||||||||||||||||||||||||||||||||||
| 591 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | sub _flags | ||||||||||||||||||||||||||||||||||||||||||
| 596 | 19 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flags: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | 19 | return $self->_flags_or_attr('flags'); | |||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||
| 602 | sub _attr | ||||||||||||||||||||||||||||||||||||||||||
| 603 | 20 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_attr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 604 | 20 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | 20 | return $self->_flags_or_attr('attr'); | |||||||||||||||||||||||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||
| 609 | sub _flags_or_attr | ||||||||||||||||||||||||||||||||||||||||||
| 610 | 39 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flags_or_attr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 611 | 39 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 612 | 39 | my $type = shift; | |||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | 62 | return join "\n,", ( map { "$_ => $self->{current_compile}{$type}{$_}" } | |||||||||||||||||||||||||||||||||||||||||
| 615 | 39 | keys %{ $self->{current_compile}{$type} } ); | |||||||||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||
| 618 | sub _declared_args | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 56 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_declared_args: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 620 | 56 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | 56 | my @args; | |||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | 56 | foreach my $arg ( sort {"$a->{type}$a->{name}" cmp "$b->{type}$b->{name}" } | |||||||||||||||||||||||||||||||||||||||||
| 625 | 56 | @{ $self->{current_compile}{args} } ) | |||||||||||||||||||||||||||||||||||||||||
| 626 | { | ||||||||||||||||||||||||||||||||||||||||||
| 627 | 94 | ▶ | my $def = defined $arg->{default} ? "$arg->{default}" : 'undef'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 628 | 94 | $def =~ s,([\\']),\\$1,g; | |||||||||||||||||||||||||||||||||||||||||
| 629 | 94 | ▶ | $def = "'$def'" unless $def eq 'undef'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||
| 631 | 94 | push @args, " '$arg->{type}$arg->{name}' => { default => $def }"; | |||||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||
| 634 | 56 | return join ",\n", @args; | |||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | 31 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | __END__ | ||||||||||||||||||||||||||||||||||||||||||