/usr/share/perl5/HTML/Mason/Compiler/ToObject.pm is in libhtml-mason-perl 1:1.54-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | # Copyright (c) 1998-2005 by Jonathan Swartz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
package HTML::Mason::Compiler::ToObject;
{
$HTML::Mason::Compiler::ToObject::VERSION = '1.54';
}
use strict;
use warnings;
use Params::Validate qw(BOOLEAN SCALAR validate);
use HTML::Mason::Tools qw(taint_is_on);
use HTML::Mason::Compiler;
use base qw( HTML::Mason::Compiler );
use HTML::Mason::Exceptions( abbr => [qw(wrong_compiler_error system_error)] );
use File::Path qw(mkpath rmtree);
use File::Basename qw(dirname);
BEGIN
{
__PACKAGE__->valid_params
(
comp_class =>
{ parse => 'string', type => SCALAR, default => 'HTML::Mason::Component',
descr => "The class into which component objects will be blessed" },
subcomp_class =>
{ parse => 'string', type => SCALAR, default => 'HTML::Mason::Component::Subcomponent',
descr => "The class into which subcomponent objects will be blessed" },
in_package =>
{ parse => 'string', type => SCALAR, default => 'HTML::Mason::Commands',
descr => "The package in which component execution will take place" },
preamble =>
{ parse => 'string', type => SCALAR, default => '',
descr => "A chunk of Perl code to add to the beginning of each compiled component" },
postamble =>
{ parse => 'string', type => SCALAR, default => '',
descr => "A chunk of Perl code to add to the end of each compiled component" },
use_strict =>
{ parse => 'boolean', type => SCALAR, default => 1,
descr => "Whether to turn on Perl's 'strict' pragma in components" },
use_warnings =>
{ parse => 'boolean', type => SCALAR, default => 0,
descr => "Whether to turn on Perl's 'warnings' pragma in components" },
define_args_hash =>
{ parse => 'string', type => SCALAR, default => 'auto',
regex => qr/^(?:always|auto|never)$/,
descr => "Whether or not to create the %ARGS hash" },
named_component_subs =>
{ parse => 'boolean', type => BOOLEAN, default => 0,
descr => "Whether to use named subroutines for component code" },
);
}
use HTML::Mason::MethodMaker
( read_only => [
qw( comp_class
define_args_hash
in_package
named_component_subs
postamble
preamble
subcomp_class
use_strict
use_warnings
)
],
);
sub compile
{
my $self = shift;
my %p = @_;
local $self->{comp_class} = delete $p{comp_class} if exists $p{comp_class};
return $self->SUPER::compile( %p );
}
#
# compile_to_file( source => ..., file => ... )
# Save object text in an object file.
#
# We attempt to handle several cases in which a file already exists
# and we wish to create a directory, or vice versa. However, not
# every case is handled; to be complete, mkpath would have to unlink
# any existing file in its way.
#
sub compile_to_file
{
my $self = shift;
my %p = validate( @_, { file => { type => SCALAR },
source => { isa => 'HTML::Mason::ComponentSource' } },
);
my ($file, $source) = @p{qw(file source)};
my @newfiles = ($file);
if (defined $file && !-f $file) {
my ($dirname) = dirname($file);
if (!-d $dirname) {
unlink($dirname) if (-e _);
push @newfiles, mkpath($dirname, 0, 0775);
system_error "Couldn't create directory $dirname: $!"
unless -d $dirname;
}
rmtree($file) if (-d $file);
}
($file) = $file =~ /^(.*)/s if taint_is_on; # Untaint blindly
open my $fh, "> $file"
or system_error "Couldn't create object file $file: $!";
$self->compile( comp_source => $source->comp_source_ref,
name => $source->friendly_name,
comp_class => $source->comp_class,
comp_path => $source->comp_path,
fh => $fh );
close $fh
or system_error "Couldn't close object file $file: $!";
return \@newfiles;
}
sub _output_chunk
{
my ($self, $fh, $string) = (shift, shift, shift);
if ($fh)
{
print $fh (ref $_ ? $$_ : $_) foreach grep defined, @_;
}
else
{
$$string .= (ref $_ ? $$_ : $_) foreach @_;
}
}
# There are some really spooky relationships between the variables &
# data members in the compiled_component() routine.
sub compiled_component
{
my ($self, %p) = @_;
my $c = $self->{current_compile};
my $obj_text = '';
local $c->{compiled_def} = $self->_compile_subcomponents if %{ $c->{def} };
local $c->{compiled_method} = $self->_compile_methods if %{ $c->{method} };
# Some preamble stuff, including 'use strict', 'use vars', and <%once> block
my $header = $self->_make_main_header;
$self->_output_chunk($p{fh}, \$obj_text, $header);
my $params = $self->_component_params;
$params->{load_time} = time;
$params->{subcomps} = '\%_def' if %{ $c->{def} };
$params->{methods} = '\%_method' if %{ $c->{method} };
if ( $self->_blocks('shared') )
{
my %subs;
while ( my ($name, $pref) = each %{ $c->{compiled_def} } )
{
my $key = "subcomponent_$name";
$subs{$key} = $pref->{code};
$pref->{code} = "sub {\nHTML::Mason::Request->instance->call_dynamic('$key',\@_)\n}";
}
while (my ($name, $pref) = each %{ $c->{compiled_method} } )
{
my $key = "method_$name";
$subs{$key} = $pref->{code};
$pref->{code} = "sub {\nHTML::Mason::Request->instance->call_dynamic( '$key', \@_ )\n}";
}
$subs{main} = $params->{code};
$params->{code} = "sub {\nHTML::Mason::Request->instance->call_dynamic( 'main', \@_ )\n}";
my $named_subs = '';
my %named_subs = $self->_named_subs_hash;
while ( my ( $name, $body ) = each %named_subs )
{
$named_subs .= '*' . $name . " = sub {\n" . $body . "\n};\n\n";
}
$params->{dynamic_subs_init} =
join '', ( "sub {\n",
$self->_set_request,
$self->_blocks('shared'),
$named_subs,
"return {\n",
map( "'$_' => $subs{$_},\n", sort keys %subs ),
"\n}\n}"
);
}
else
{
my %named_subs = $self->_named_subs_hash;
while ( my ( $name, $body ) = each %named_subs )
{
$self->_output_chunk( $p{fh}, \$obj_text,
"sub $name {\n" . $body . "\n}\n"
);
}
}
$self->_output_chunk($p{fh}, \$obj_text, $self->_subcomponents_footer);
$self->_output_chunk($p{fh}, \$obj_text, $self->_methods_footer);
$self->_output_chunk($p{fh}, \$obj_text,
$self->_constructor( $self->comp_class,
$params ),
';',
);
return \$obj_text;
}
sub _named_subs_hash
{
my $self = shift;
return unless $self->named_component_subs;
my %subs;
$subs{ $self->_sub_name } = $self->_body;
while ( my ( $name, $params ) =
each %{ $self->{current_compile}{compiled_def} } )
{
$subs{ $self->_sub_name( 'def', $name ) } = $params->{body};
}
while ( my ( $name, $params ) =
each %{ $self->{current_compile}{compiled_method} } )
{
$subs{ $self->_sub_name( 'method', $name ) } = $params->{body};
}
return %subs;
}
sub _sub_name
{
my $self = shift;
return join '_', $self->_escape_sub_name_part( $self->{comp_path}, @_ );
}
sub _escape_sub_name_part
{
my $self = shift;
return map { my $part = $_;
$part =~ s/([^\w_])/'_' . sprintf( '%x', ord $1 )/ge;
$part; } @_;
}
sub _compile_subcomponents
{
my $self = shift;
return $self->_compile_subcomponents_or_methods('def');
}
sub _compile_methods
{
my $self = shift;
return $self->_compile_subcomponents_or_methods('method');
}
sub _compile_subcomponents_or_methods
{
my $self = shift;
my $type = shift;
my %compiled;
foreach ( keys %{ $self->{current_compile}{$type} } )
{
local $self->{current_compile} = $self->{current_compile}{$type}{$_};
local $self->{current_compile}->{in_named_block} = {type => $type, name => $_};
$compiled{$_} = $self->_component_params;
}
return \%compiled;
}
sub _make_main_header
{
my $self = shift;
my $pkg = $self->in_package;
return join '', ( "package $pkg;\n",
$self->use_strict ? "use strict;\n" : "no strict;\n",
$self->use_warnings ? "use warnings;\n" : "",
sprintf( "use vars qw(\%s);\n",
join ' ', '$m', $self->allow_globals ),
$self->_blocks('once'),
);
}
sub _subcomponents_footer
{
my $self = shift;
return $self->_subcomponent_or_method_footer('def');
}
sub _methods_footer
{
my $self = shift;
return $self->_subcomponent_or_method_footer('method');
}
sub _subcomponent_or_method_footer
{
my $self = shift;
my $c = $self->{current_compile};
my $type = shift;
return '' unless %{ $c->{$type} };
return join('',
"my %_$type =\n(\n",
map( {("'$_' => " ,
$self->_constructor( $self->{subcomp_class},
$c->{"compiled_$type"}{$_} ) ,
",\n")} keys %{ $c->{"compiled_$type"} } ) ,
"\n);\n"
);
}
sub _constructor
{
my ($self, $class, $params) = @_;
return ("${class}->new(\n",
map( {("'$_' => ", $params->{$_}, ",\n")}
sort grep { $_ ne 'body' } keys %$params ),
"\n)\n",
);
}
sub _component_params
{
my $self = shift;
my %params;
if ( $self->named_component_subs )
{
$params{code} =
'\\&' .
$self->_sub_name
( grep { defined }
@{ $self->{current_compile}{in_named_block} }
{ 'type', 'name' } );
$params{body} = $self->_body;
}
else
{
$params{code} = join '', "sub {\n", $self->_body, "}";
}
$params{flags} = join '', "{\n", $self->_flags, "\n}"
if keys %{ $self->{current_compile}{flags} };
$params{attr} = join '', "{\n", $self->_attr, "\n}"
if keys %{ $self->{current_compile}{attr} };
$params{declared_args} = join '', "{\n", $self->_declared_args, "\n}"
if @{ $self->{current_compile}{args} };
$params{has_filter} = 1 if $self->_blocks('filter');
return \%params;
}
sub _body
{
my $self = shift;
return join '', ( $self->preamble,
$self->_set_request,
$self->_set_buffer,
$self->_arg_declarations,
$self->_filter,
"\$m->debug_hook( \$m->current_comp->path ) if ( HTML::Mason::Compiler::IN_PERL_DB() );\n\n",
$self->_blocks('init'),
# do not add a block around this, it introduces
# a separate scope and might break cleanup
# blocks (or all sort of other things!)
$self->{current_compile}{body},
$self->_blocks('cleanup'),
$self->postamble,
# semi before return will help catch syntax
# errors in component body - don't return values
# explicitly
";return;\n",
);
}
sub _set_request
{
my $self = shift;
return if $self->in_package eq 'HTML::Mason::Commands';
return 'local $' . $self->in_package . '::m = $HTML::Mason::Commands::m;' . "\n";
}
sub _set_buffer
{
my $self = shift;
if ($self->enable_autoflush) {
return '';
} else {
return 'my $_outbuf = $m->{top_stack}->[HTML::Mason::Request::STACK_BUFFER];' . "\n";
}
}
my %coercion_funcs = ( '@' => 'HTML::Mason::Tools::coerce_to_array',
'%' => 'HTML::Mason::Tools::coerce_to_hash',
);
sub _arg_declarations
{
my $self = shift;
my $init;
my @args_hash;
my $pos;
my @req_check;
my @decl;
my @assign;
my $define_args_hash = $self->_define_args_hash;
unless ( @{ $self->{current_compile}{args} } )
{
return unless $define_args_hash;
return ( "my \%ARGS;\n",
"{ local \$^W; \%ARGS = \@_ unless (\@_ % 2); }\n"
);
}
$init = <<'EOF';
HTML::Mason::Exception::Params->throw
( error =>
"Odd number of parameters passed to component expecting name/value pairs"
) if @_ % 2;
EOF
if ( $define_args_hash )
{
@args_hash = "my \%ARGS = \@_;\n";
}
# opening brace will be closed later. we want this in a separate
# block so that the rest of the component can't see %pos
$pos = <<'EOF';
{
my %pos;
for ( my $x = 0; $x < @_; $x += 2 )
{
$pos{ $_[$x] } = $x + 1;
}
EOF
my @required =
( map { $_->{name} }
grep { ! defined $_->{default} }
@{ $self->{current_compile}{args} }
);
if (@required)
{
# just to be sure
local $" = ' ';
@req_check = <<"EOF";
foreach my \$arg ( qw( @required ) )
{
HTML::Mason::Exception::Params->throw
( error => "no value sent for required parameter '\$arg'" )
unless exists \$pos{\$arg};
}
EOF
}
foreach ( @{ $self->{current_compile}{args} } )
{
my $var_name = "$_->{type}$_->{name}";
push @decl, $var_name;
my $arg_in_array = "\$_[ \$pos{'$_->{name}'} ]";
my $coerce;
if ( $coercion_funcs{ $_->{type} } )
{
$coerce = $coercion_funcs{ $_->{type} } . "( $arg_in_array, '$var_name')";
}
else
{
$coerce = $arg_in_array;
}
if ( defined $_->{line} && defined $_->{file} && $self->use_source_line_numbers )
{
my $file = $self->_escape_filename( $_->{file} );
push @assign, qq{#line $_->{line} "$file"\n};
}
if ( defined $_->{default} )
{
my $default_val = $_->{default};
# allow for comments after default declaration
$default_val .= "\n" if defined $_->{default} && $_->{default} =~ /\#/;
push @assign, <<"EOF";
$var_name = exists \$pos{'$_->{name}'} ? $coerce : $default_val;
EOF
}
else
{
push @assign,
" $var_name = $coerce;\n";
}
}
my $decl = 'my ( ';
$decl .= join ', ', @decl;
$decl .= " );\n";
# closing brace closes opening of @pos
return $init, @args_hash, $decl, $pos, @req_check, @assign, "}\n";
}
sub _define_args_hash
{
my $self = shift;
return 1 if $self->define_args_hash eq 'always';
return 0 if $self->define_args_hash eq 'never';
foreach ( $self->preamble,
$self->_blocks('filter'),
$self->_blocks('init'),
$self->{current_compile}{body},
$self->_blocks('cleanup'),
$self->postamble,
grep { defined } map { $_->{default} } @{ $self->{current_compile}{args} }
)
{
return 1 if /ARGS/;
}
}
sub _filter
{
my $self = shift;
my @filter;
@filter = $self->_blocks('filter')
or return;
return ( join '',
"\$m->current_comp->filter( sub { local \$_ = shift;\n",
( join ";\n", @filter ),
";\n",
"return \$_;\n",
"} );\n",
);
}
sub _flags
{
my $self = shift;
return $self->_flags_or_attr('flags');
}
sub _attr
{
my $self = shift;
return $self->_flags_or_attr('attr');
}
sub _flags_or_attr
{
my $self = shift;
my $type = shift;
return join "\n,", ( map { "$_ => $self->{current_compile}{$type}{$_}" }
keys %{ $self->{current_compile}{$type} } );
}
sub _declared_args
{
my $self = shift;
my @args;
foreach my $arg ( sort {"$a->{type}$a->{name}" cmp "$b->{type}$b->{name}" }
@{ $self->{current_compile}{args} } )
{
my $def = defined $arg->{default} ? "$arg->{default}" : 'undef';
$def =~ s,([\\']),\\$1,g;
$def = "'$def'" unless $def eq 'undef';
push @args, " '$arg->{type}$arg->{name}' => { default => $def }";
}
return join ",\n", @args;
}
1;
__END__
=pod
=head1 NAME
HTML::Mason::Compiler::ToObject - A Compiler subclass that generates Mason object code
=head1 VERSION
version 1.54
=head1 SYNOPSIS
my $compiler = HTML::Mason::Compiler::ToObject->new;
my $object_code =
$compiler->compile( comp_source => $source,
name => $comp_name,
comp_path => $comp_path,
);
=head1 DESCRIPTION
This Compiler subclass generates Mason object code (Perl code). It is
the default Compiler class used by Mason.
=head1 PARAMETERS TO THE new() CONSTRUCTOR
All of these parameters are optional.
=over
=item comp_class
The class into which component objects are blessed. This defaults to
L<HTML::Mason::Component|HTML::Mason::Component>.
=item subcomp_class
The class into which subcomponent objects are blessed. This defaults
to L<HTML::Mason::Component::Subcomponent|HTML::Mason::Component::Subcomponent>.
=item in_package
This is the package in which a component's code is executed. For
historical reasons, this defaults to C<HTML::Mason::Commands>.
=item preamble
Text given for this parameter is placed at the beginning of each
component, but after the execution of any C<< <%once> >> block. See
also L<postamble|HTML::Mason::Params/postamble>. The request will be available as C<$m> in preamble
code.
=item postamble
Text given for this parameter is placed at the end of each
component. See also L<preamble|HTML::Mason::Params/preamble>. The request will be available as
C<$m> in postamble code.
=item use_strict
True or false, default is true. Indicates whether or not a given
component should C<use strict>.
=item use_warnings
True or false, default is false. Indicates whether or not a given
component should C<use warnings>.
=item named_component_subs
When compiling a component, use uniquely named subroutines for the a
component's body, subcomponents, and methods. Doing this allows you to
effectively profile Mason components. Without this, all components
simply show up as __ANON__ or something similar in the profiler.
=item define_args_hash
One of "always", "auto", or "never". This determines whether or not
an C<%ARGS> hash is created in components. If it is set to "always",
one is always defined. If set to "never", it is never defined.
The default, "auto", will cause the hash to be defined only if some
part of the component contains the string "ARGS". This is somewhat
crude, and may result in some false positives, but this is preferable
to false negatives.
Not defining the args hash means that we can avoid copying component
arguments, which can save memory and slightly improve execution speed.
=back
=head1 ACCESSOR METHODS
All of the above properties have read-only accessor methods of the
same name. You cannot change any property of a compiler after it has
been created (but you can create multiple compilers with different
properties).
=head1 METHODS
This class is primarily meant to be used by the Interpreter object,
and as such has a very limited public API.
=over
=item compile(...)
This method will take component source and return the compiled object
code for that source. See L<HTML::Mason::Compiler/compile(...)> for
details on this method.
This subclass also accepts a C<comp_class> parameter, allowing you to
override the class into which the component is compiled.
=back
=head1 SEE ALSO
L<Mason|Mason>
=head1 AUTHORS
=over 4
=item *
Jonathan Swartz <swartz@pobox.com>
=item *
Dave Rolsky <autarch@urth.org>
=item *
Ken Williams <ken@mathforum.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Jonathan Swartz.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|