home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Compiler.pm < prev    next >
Encoding:
Perl POD Document  |  2003-11-12  |  24.3 KB  |  883 lines

  1. # Copyright (c) 1998-2003 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.  
  7. use strict;
  8.  
  9. use HTML::Mason::Component::FileBased;
  10. use HTML::Mason::Component::Subcomponent;
  11. use HTML::Mason::Lexer;
  12.  
  13. use HTML::Mason::Exceptions( abbr => [qw(param_error compiler_error syntax_error)] );
  14. use Params::Validate qw(:all);
  15. Params::Validate::validation_options( on_fail => sub { param_error join '', @_ } );
  16.  
  17. use Class::Container;
  18. use base qw(Class::Container);
  19.  
  20. BEGIN
  21. {
  22.     __PACKAGE__->valid_params
  23.     (
  24.      allow_globals =>
  25.          { parse => 'list', type => ARRAYREF, default => [],
  26.            descr => "An array of names of Perl variables that are allowed globally within components" },
  27.  
  28.      default_escape_flags =>
  29.          { parse => 'string', type => SCALAR|ARRAYREF, default => [],
  30.            descr => "Escape flags that will apply by default to all Mason tag output" },
  31.  
  32.      lexer =>
  33.          { isa => 'HTML::Mason::Lexer',
  34.            descr => "A Lexer object that will scan component text during compilation" },
  35.  
  36.      preprocess =>
  37.          { parse => 'code', type => CODEREF, optional => 1,
  38.            descr => "A subroutine through which all component text will be sent during compilation" },
  39.  
  40.      postprocess_perl =>
  41.          { parse => 'code', type => CODEREF, optional => 1,
  42.            descr => "A subroutine through which all Perl code will be sent during compilation" },
  43.  
  44.      postprocess_text =>
  45.          { parse => 'code', type => CODEREF, optional => 1,
  46.            descr => "A subroutine through which all plain text will be sent during compilation" },
  47.  
  48.      use_source_line_numbers =>
  49.      { parse => 'boolean', type => SCALAR, default => 1,
  50.        descr => "Whether to use source line numbers in errors and debugger" },
  51.     );
  52.  
  53.     __PACKAGE__->contained_objects
  54.         ( lexer => { class => 'HTML::Mason::Lexer',
  55.                      descr => "This class generates compiler events based on the components source" },
  56.         );
  57. }
  58.  
  59. use HTML::Mason::MethodMaker
  60.     ( read_write => [ map { [ $_ => __PACKAGE__->validation_spec->{$_} ] }
  61.                       qw( lexer
  62.                           preprocess
  63.                           postprocess_perl
  64.                           postprocess_text
  65.               use_source_line_numbers
  66.                         )
  67.             ],
  68.     );
  69.  
  70. my $old_escape_re = qr/^[hnu]+$/;
  71.  
  72. sub new
  73. {
  74.     my $class = shift;
  75.     my $self = $class->SUPER::new(@_);
  76.  
  77.     $self->default_escape_flags( $self->{default_escape_flags} )
  78.         if defined $self->{default_escape_flags};
  79.  
  80.     # Verify the validity of the global names
  81.     $self->allow_globals( @{$self->{allow_globals}} );
  82.  
  83.     return $self;
  84. }
  85.  
  86. sub object_id
  87. {
  88.     my $self = shift;
  89.  
  90.     # Can't use object keys because they stringify differently every
  91.     # time the program is loaded, whether they are a reference to the
  92.     # same object or not.
  93.     my $spec = $self->validation_spec;
  94.  
  95.     my @id_keys =
  96.     ( grep { ! exists $spec->{$_}{isa} && ! exists $spec->{$_}{can} }
  97.       grep { $_ ne 'container' } keys %$spec );
  98.  
  99.     my @vals;
  100.     foreach my $k ( @id_keys )
  101.     {
  102.     push @vals, $k;
  103.  
  104.     # For coderef params we simply indicate whether or not it is
  105.     # present.  This is better than simply ignoring them but not
  106.     # by much.  We _could_ use B::Deparse's coderef2text method to
  107.     # do this properly but I'm not sure if that's a good idea or
  108.     # if it works for Perl 5.005.
  109.     push @vals,
  110.             $HTML::Mason::VERSION,
  111.             ( $spec->{$k}{parse} eq 'code'  ? ( $self->{$k} ? 1 : 0 ) :
  112.               UNIVERSAL::isa( $self->{$k}, 'HASH' )  ?
  113.               map { $_ => $self->{$k}{$_} } sort keys %{ $self->{$k} } :
  114.               UNIVERSAL::isa( $self->{$k}, 'ARRAY' ) ? sort @{ $self->{$k} } :
  115.               $self->{$k} );
  116.     }
  117.  
  118.     local $^W; # ignore undef warnings
  119.     # unpack('%32C*', $x) computes the 32-bit checksum of $x
  120.     return join '!', $self->lexer->object_id, unpack('%32C*', join "\0", @vals);
  121. }
  122.  
  123. my %top_level_only_block = map { $_ => 1 } qw( cleanup once shared );
  124. my %valid_comp_flag = map { $_ => 1 } qw( inherit );
  125.  
  126. sub add_allowed_globals
  127. {
  128.     my $self = shift;
  129.     my @globals = @_;
  130.  
  131.     if ( my @bad = grep { ! /^[\$@%]/ } @globals )
  132.     {
  133.     param_error "add_allowed_globals: bad parameters '@bad', must begin with one of \$, \@, %\n";
  134.     }
  135.  
  136.     $self->{allow_globals} = [ keys %{ { map { $_ => 1 } @globals, @{ $self->{allow_globals} } } } ];
  137.     return @{ $self->{allow_globals} };
  138. }
  139.  
  140. sub allow_globals
  141. {
  142.     my $self = shift;
  143.  
  144.     if (@_)
  145.     {
  146.     $self->{allow_globals} = [];
  147.     return if @_ == 1 and not defined $_[0]; # @_ is (undef)
  148.     $self->add_allowed_globals(@_);
  149.     }
  150.  
  151.     return @{ $self->{allow_globals} };
  152. }
  153.  
  154. sub default_escape_flags
  155. {
  156.     my $self = shift;
  157.  
  158.     return $self->{default_escape_flags} unless @_;
  159.  
  160.     my $flags = shift;
  161.  
  162.     unless ( defined $flags )
  163.     {
  164.         $self->{default_escape_flags} = [];
  165.         return;
  166.     }
  167.  
  168.     # make sure this is always an arrayref
  169.     unless ( ref $flags )
  170.     {
  171.         if ( $flags =~ /^[hu]+$/ )
  172.         {
  173.             $self->{default_escape_flags} = [ split //, $flags ];
  174.         }
  175.         else
  176.         {
  177.             $self->{default_escape_flags} = [ $flags ];
  178.         }
  179.     }
  180.  
  181.     return $self->{default_escape_flags};
  182. }
  183.  
  184. sub compile
  185. {
  186.     my $self = shift;
  187.     my %p = validate( @_, { comp_source => { type => SCALAR|SCALARREF },
  188.                 name => { type => SCALAR },
  189.                 fh => { type => HANDLE, optional => 1 },
  190.               } );
  191.     my $src = ref($p{comp_source}) ? $p{comp_source} : \$p{comp_source};
  192.  
  193.     # The current compile - initially the main component, then each subcomponent/method
  194.     local $self->{current_compile} = {};
  195.     
  196.     # Useful for implementing features that affect both main body and methods/subcomps
  197.     local $self->{main_compile} = $self->{current_compile};
  198.  
  199.     # So we're re-entrant in subcomps
  200.     local $self->{paused_compiles} = [];
  201.  
  202.     # Preprocess the source.  The preprocessor routine is handed a
  203.     # reference to the entire source.
  204.     if ($self->preprocess)
  205.     {
  206.     eval { $self->preprocess->( $src ) };
  207.     compiler_error "Error during custom preprocess step: $@" if $@;
  208.     }
  209.  
  210.     $self->lexer->lex( comp_source => $src, name => $p{name}, compiler => $self );
  211.  
  212.     return $self->compiled_component( exists($p{fh}) ? (fh => $p{fh}) : () );
  213. }
  214.  
  215. sub start_component
  216. {
  217.     my $self = shift;
  218.     my $c = $self->{current_compile};
  219.  
  220.     $c->{in_main} = 1;
  221.     $c->{comp_with_content_stack} = [];
  222.  
  223.     $c->{in_block} = undef;
  224.  
  225.     $self->_init_comp_data($c);
  226. }
  227.  
  228. sub _init_comp_data
  229. {
  230.     my $self = shift;
  231.     my $data = shift;
  232.  
  233.     $data->{body} = '';
  234.     $data->{last_body_code_type} = '';
  235.  
  236.     foreach ( qw( def method ) )
  237.     {
  238.     $data->{$_} = {};
  239.     }
  240.  
  241.     $data->{args} = [];
  242.     $data->{flags} = {};
  243.     $data->{attr} = {};
  244.  
  245.     foreach ( qw( cleanup filter init once shared ) )
  246.     {
  247.     $data->{blocks}{$_} = [];
  248.     }
  249. }
  250.  
  251. sub end_component
  252. {
  253.     my $self = shift;
  254.     my $c = $self->{current_compile};
  255.  
  256.     $self->lexer->throw_syntax_error("Not enough component-with-content ending tags found")
  257.     if $c->{comp_with_content_stack} && @{ $c->{comp_with_content_stack} };
  258. }
  259.  
  260. sub start_block
  261. {
  262.     my $self = shift;
  263.     my $c = $self->{current_compile};
  264.     my %p = @_;
  265.  
  266.     $self->lexer->throw_syntax_error("Cannot define a $p{block_type} section inside a method or subcomponent")
  267.      if $top_level_only_block{ $p{block_type} } && ! $c->{in_main};
  268.  
  269.     $self->lexer->throw_syntax_error("Cannot nest a $p{block_type} inside a $c->{in_block} block")
  270.      if $c->{in_block};
  271.  
  272.     $c->{in_block} = $p{block_type};
  273. }
  274.  
  275. sub raw_block
  276. {
  277.     # These blocks contain Perl code - so don't include <%text> and so on.
  278.  
  279.     my $self = shift;
  280.     my $c = $self->{current_compile};
  281.     my %p = @_;
  282.  
  283.     eval { $self->postprocess_perl->( \$p{block} ) if $self->postprocess_perl };
  284.     compiler_error $@ if $@;
  285.  
  286.     my $method = "$p{block_type}_block";
  287.     return $self->$method(%p) if $self->can($method);
  288.  
  289.     my $comment = '';
  290.     if ( $self->lexer->line_number )
  291.     {
  292.     my $line = $self->lexer->line_number;
  293.     my $file = $self->lexer->name;
  294.     $comment = "#line $line $file\n" if $self->use_source_line_numbers;
  295.     }
  296.  
  297.     push @{ $self->{current_compile}{blocks}{ $p{block_type} } }, "$comment$p{block}";
  298. }
  299.  
  300. sub doc_block
  301. {
  302.     # Don't do anything - just discard the comment.
  303. }
  304.  
  305. sub perl_block
  306. {
  307.     my $self = shift;
  308.     my %p = @_;
  309.  
  310.     $self->_add_body_code( $p{block} );
  311.  
  312.     $self->{current_compile}{last_body_code_type} = 'perl_block';
  313. }
  314.  
  315. sub text
  316. {
  317.     my ($self, %p) = @_;
  318.     my $tref = ref($p{text}) ? $p{text} : \$p{text};  # Allow a reference
  319.  
  320.     eval { $self->postprocess_text->($tref) } if $self->postprocess_text;
  321.     compiler_error $@ if $@;
  322.  
  323.     $$tref =~ s,(['\\]),\\$1,g;
  324.  
  325.     $self->_add_body_code("\$m->print( '", $$tref, "' );\n");
  326.  
  327.     $self->{current_compile}{last_body_code_type} = 'text';
  328. }
  329.  
  330. sub text_block
  331. {
  332.     my $self = shift;
  333.     my %p = @_;
  334.     $self->text(text => \$p{block});
  335. }
  336.  
  337. sub end_block
  338. {
  339.     my $self = shift;
  340.     my $c = $self->{current_compile};
  341.     my %p = @_;
  342.  
  343.     $self->lexer->throw_syntax_error("End of $p{block_type} encountered while in $c->{in_block} block")
  344.     unless $c->{in_block} eq $p{block_type};
  345.  
  346.     $c->{in_block} = undef;
  347. }
  348.  
  349. sub variable_declaration
  350. {
  351.     my $self = shift;
  352.     my %p = @_;
  353.  
  354.     $self->lexer->throw_syntax_error("variable_declaration called inside a $p{block_type} block")
  355.     unless $p{block_type} eq 'args';
  356.  
  357.     my $arg = "$p{type}$p{name}";
  358.  
  359.     $self->lexer->throw_syntax_error("$arg already defined")
  360.         if grep { "$_->{type}$_->{name}" eq $arg } @{ $self->{current_compile}{args} };
  361.  
  362.     push @{ $self->{current_compile}{args} }, { type => $p{type},
  363.                          name => $p{name},
  364.                          default => $p{default},
  365.                          line => $self->lexer->line_number,
  366.                          file => $self->lexer->name,
  367.                        };
  368. }
  369.  
  370. sub key_value_pair
  371. {
  372.     my $self = shift;
  373.     my %p = @_;
  374.  
  375.     compiler_error "key_value_pair called inside a $p{block_type} block"
  376.     unless $p{block_type} eq 'flags' || $p{block_type} eq 'attr';
  377.  
  378.     my $type = $p{block_type} eq 'flags' ? 'flag' : 'attribute';
  379.     $self->lexer->throw_syntax_error("$p{key} $type already defined")
  380.     if exists $self->{current_compile}{ $p{block_type} }{ $p{key} };
  381.  
  382.     $self->{current_compile}{ $p{block_type} }{ $p{key} } = $p{value}
  383. }
  384.  
  385. sub start_named_block
  386. {
  387.     my $self = shift;
  388.     my $c = $self->{current_compile};
  389.     my %p = @_;
  390.  
  391.     # Error if defining one def or method inside another
  392.     $self->lexer->throw_syntax_error
  393.     ("Cannot define a $p{block_type} block inside a method or subcomponent")
  394.         unless $c->{in_main};
  395.  
  396.     # Error for invalid character in name
  397.     $self->lexer->throw_syntax_error("Invalid $p{block_type} name: $p{name}")
  398.     if $p{name} =~ /[^.\w-]/;
  399.  
  400.     # Error if two defs or two methods defined with same name
  401.     $self->lexer->throw_syntax_error
  402.         (sprintf("Duplicate definition of %s '%s'",
  403.          $p{block_type} eq 'def' ? 'subcomponent' : 'method', $p{name}))
  404.             if exists $c->{$p{block_type}}{ $p{name} };
  405.     
  406.     # Error if def and method defined with same name
  407.     my $other_type = $p{block_type} eq 'def' ? 'method' : 'def';
  408.     $self->lexer->throw_syntax_error
  409.         ("Cannot define a method and subcomponent with the same name ($p{name}")
  410.             if exists $c->{$other_type}{ $p{name} };
  411.  
  412.     $c->{in_main}--;
  413.  
  414.     $c->{ $p{block_type} }{ $p{name} } = {};
  415.     $self->_init_comp_data( $c->{ $p{block_type} }{ $p{name} } );
  416.     push @{$self->{paused_compiles}}, $c;
  417.     $self->{current_compile} = $c->{ $p{block_type} }{ $p{name} };
  418.     $self->{current_compile}->{in_named_block} = {block_type => $p{block_type}, name => $p{name}};
  419. }
  420.  
  421. sub end_named_block
  422. {
  423.     my $self = shift;
  424.  
  425.     delete $self->{current_compile}->{in_named_block};
  426.     $self->{current_compile} = pop @{$self->{paused_compiles}};
  427.     $self->{current_compile}{in_main}++;
  428. }
  429.  
  430. sub substitution
  431. {
  432.     my $self = shift;
  433.     my %p = @_;
  434.  
  435.     my $text = $p{substitution};
  436.  
  437.     if ( ( exists $p{escape} && defined $p{escape} ) ||
  438.          @{ $self->{default_escape_flags} }
  439.        )
  440.     {
  441.         my @flags;
  442.         if ( defined $p{escape} )
  443.         {
  444.             $p{escape} =~ s/\s+$//;
  445.  
  446.             if ( $p{escape} =~ /$old_escape_re/ )
  447.             {
  448.                 @flags = split //, $p{escape};
  449.             }
  450.             else
  451.             {
  452.                 @flags = split /\s*,\s*/, $p{escape};
  453.             }
  454.         }
  455.  
  456.         # is there any way to check the flags for validity and still
  457.         # allow them to be dynamically set from components?
  458.  
  459.         unshift @flags, @{ $self->default_escape_flags }
  460.             unless grep { $_ eq 'n' } @flags;
  461.  
  462.         my %seen;
  463.     my $flags =
  464.             ( join ', ',
  465.               map { $seen{$_}++ ? () : "'$_'" }
  466.               grep { $_ ne 'n' } @flags
  467.             );
  468.  
  469.         $text = "\$m->interp->apply_escapes( (join '', ($text)), $flags )" if $flags;
  470.     }
  471.  
  472.     my $code = "\$m->print( $text );\n";
  473.  
  474.     eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
  475.     compiler_error $@ if $@;
  476.  
  477.     $self->_add_body_code($code);
  478.  
  479.     $self->{current_compile}{last_body_code_type} = 'substitution';
  480. }
  481.  
  482. sub component_call
  483. {
  484.     my $self = shift;
  485.     my %p = @_;
  486.  
  487.     my $call = $p{call};
  488.     for ($call) { s/^\s+//; s/\s+$//; }
  489.     if ( $call =~ m,^[\w/.],)
  490.     {
  491.     my $comma = index($call, ',');
  492.     $comma = length $call if $comma == -1;
  493.     (my $comp = substr($call, 0, $comma)) =~ s/\s+$//;
  494.     $call = "'$comp'" . substr($call, $comma);
  495.     }
  496.  
  497.     my $code = "\$m->comp( $call );\n";
  498.     eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
  499.     compiler_error $@ if $@;
  500.  
  501.     $self->_add_body_code($code);
  502.  
  503.     $self->{current_compile}{last_body_code_type} = 'component_call';
  504. }
  505.  
  506. sub component_content_call
  507. {
  508.     my $self = shift;
  509.     my $c = $self->{current_compile};
  510.     my %p = @_;
  511.  
  512.     my $call = $p{call};
  513.     for ($call) { s/^\s+//; s/\s+$//; }
  514.     push @{ $c->{comp_with_content_stack} }, $call;
  515.  
  516.     my $code = "\$m->comp( { content => sub {\n";
  517.  
  518.     eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
  519.     compiler_error $@ if $@;
  520.  
  521.     $self->_add_body_code($code);
  522.  
  523.     $c->{last_body_code_type} = 'component_content_call';
  524. }
  525.  
  526. sub component_content_call_end
  527. {
  528.     my $self = shift;
  529.     my $c = $self->{current_compile};
  530.  
  531.     $self->lexer->throw_syntax_error("found component with content ending tag but no beginning tag")
  532.     unless @{ $c->{comp_with_content_stack} };
  533.  
  534.     my $call = pop @{ $c->{comp_with_content_stack} };
  535.  
  536.     if ( $call =~ m,^[\w/.],)
  537.     {
  538.     my $comma = index($call, ',');
  539.     $comma = length $call if $comma == -1;
  540.     (my $comp = substr($call, 0, $comma)) =~ s/\s+$//;
  541.     $call = "'$comp'" . substr($call, $comma);
  542.     }
  543.  
  544.     my $code = "} }, $call );\n";
  545.  
  546.     eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
  547.     compiler_error $@ if $@;
  548.  
  549.     $self->_add_body_code($code);
  550.  
  551.     $c->{last_body_code_type} = 'component_content_call_end';
  552. }
  553.  
  554. sub perl_line
  555. {
  556.     my $self = shift;
  557.     my %p = @_;
  558.  
  559.     my $code = "$p{line}\n";
  560.  
  561.     eval { $self->postprocess_perl->(\$code) } if $self->postprocess_perl;
  562.     compiler_error $@ if $@;
  563.  
  564.     $self->_add_body_code($code);
  565.  
  566.     $self->{current_compile}{last_body_code_type} = 'perl_line';
  567. }
  568.  
  569. sub _add_body_code
  570. {
  571.     my $self = shift;
  572.  
  573.     # We know a perl-line is always _one_ line, so we know that the
  574.     # line numbers are going to match up as long as the first line in
  575.     # a series has a line number comment before it.  Adding a comment
  576.     # can break certain constructs like qw() list that spans multiple
  577.     # perl-lines.
  578.     if ( $self->lexer->line_number &&
  579.          $self->{current_compile}{last_body_code_type} ne 'perl_line' )
  580.     {
  581.     my $line = $self->lexer->line_number;
  582.     my $file = $self->lexer->name;
  583.     $self->{current_compile}{body} .= "#line $line $file\n" if $self->use_source_line_numbers;
  584.     }
  585.  
  586.     $self->{current_compile}{body} .= $_ foreach @_;
  587. }
  588.  
  589. sub dump
  590. {
  591.     my $self = shift;
  592.     my $c = $self->{current_compile};
  593.  
  594.     warn "Main component\n";
  595.  
  596.     $self->_dump_data( $c );
  597.  
  598.     foreach ( keys %{ $c->{def} } )
  599.     {
  600.     warn "  Subcomponent $_\n";
  601.     $self->_dump_data( $c->{def}{$_}, '  ' );
  602.     }
  603.  
  604.     foreach ( keys %{ $c->{method} } )
  605.     {
  606.     warn "  Methods $_\n";
  607.     $self->_dump_data( $c->{method}{$_}, '  ');
  608.     }
  609. }
  610.  
  611. sub _dump_data
  612. {
  613.     my $self = shift;
  614.     my $data = shift;
  615.     my $indent = shift || '';
  616.  
  617.     if ( @{ $data->{args} } )
  618.     {
  619.     warn "$indent  args\n";
  620.     foreach ( @{ $data->{args} } )
  621.     {
  622.         warn "$indent    $_->{type}$_->{name}";
  623.         warn " => $_->{default}" if defined $_->{default};
  624.         warn "\n";
  625.     }
  626.     }
  627.  
  628.     warn "\n$indent  body\n";
  629.     warn $data->{body}, "\n";
  630. }
  631.  
  632. sub _blocks
  633. {
  634.     my $self = shift;
  635.  
  636.     return @{ $self->{current_compile}{blocks}{ shift() } };
  637. }
  638.  
  639. sub HTML::Mason::Parser::new
  640. {
  641.     die "The Parser module is no longer a part of HTML::Mason.  Please see ".
  642.         "the Lexer and Compiler modules, its replacements.\n";
  643. }
  644.  
  645. 1;
  646.  
  647. __END__
  648.  
  649. =head1 NAME
  650.  
  651. HTML::Mason::Compiler - Compile Mason component source
  652.  
  653. =head1 SYNOPSIS
  654.  
  655.   package My::Funky::Compiler;
  656.  
  657.   use base qw(HTML::Mason::Compiler);
  658.  
  659. =head1 DESCRIPTION
  660.  
  661. The compiler starts the compilation process by calling its lexer's
  662. C<lex> method and passing itself as the C<compiler> parameter.  The
  663. lexer then calls various methods in the compiler as it parses the
  664. component source.
  665.  
  666. =head1 PARAMETERS TO THE new() CONSTRUCTOR
  667.  
  668. =over 4
  669.  
  670. =item allow_globals
  671.  
  672. List of variable names, complete with prefix (C<$@%>), that you intend
  673. to use as globals in components.  Normally global variables are
  674. forbidden by C<strict>, but any variable mentioned in this list is
  675. granted a reprieve via a "use vars" statement. For example:
  676.  
  677.     allow_globals => [qw($DBH %session)]
  678.  
  679. In a mod_perl environment, C<$r> (the request object) is automatically
  680. added to this list.
  681.  
  682. =item default_escape_flags
  683.  
  684. Escape flags to apply to all <% %> expressions by default. The current
  685. valid flags are
  686.  
  687.     h - escape for HTML ('<' => '<', etc.)
  688.     u - escape for URL (':' => '%3A', etc.)
  689.  
  690. The developer can override default escape flags on a per-expression
  691. basis; see the L<escaping expressions|HTML::Mason::Devel/escaping expressions> section of the developer's manual.
  692.  
  693. If you want to set I<multiple> flags as the default, this should be
  694. given as a reference to an array of flags.
  695.  
  696. =item lexer
  697.  
  698. The Lexer object to associate with this Compiler. By default a new
  699. object of class L<lexer_class|HTML::Mason::Params/lexer_class> will be created.
  700.  
  701. =item lexer_class
  702.  
  703. The class to use when creating a lexer. Defaults to L<HTML::Mason::Lexer|HTML::Mason::Lexer>.
  704.  
  705. =item preprocess
  706.  
  707. Sub reference that is called to preprocess each component before the compiler does
  708. it's magic.  The sub is called with a single parameter, a scalar reference
  709. to the script.  The sub is expected to process the script in-place.   This is
  710. one way to extend the HTML::Mason syntax with new tags, etc., although a much
  711. more flexible way is to subclass the Lexer or Compiler class. See also
  712. L<postprocess_text|HTML::Mason::Params/postprocess_text> and L<postprocess_perl|HTML::Mason::Params/postprocess_perl>.
  713.  
  714. =item postprocess_text
  715.  
  716. Sub reference that is called to postprocess the text portion of a
  717. compiled component, just before it is assembled into its final
  718. subroutine form.  The sub is called with a single parameter, a scalar
  719. reference to the text portion of the component.  The sub is expected
  720. to process the string in-place. See also
  721. L<preprocess|HTML::Mason::Params/preprocess> and L<postprocess_perl|HTML::Mason::Params/postprocess_perl>.
  722.  
  723. =item postprocess_perl
  724.  
  725. Sub reference that is called to postprocess the Perl portion of a
  726. compiled component, just before it is assembled into its final
  727. subroutine form.  The sub is called with a single parameter, a scalar
  728. reference to the Perl portion of the component.  The sub is expected
  729. to process the string in-place. See also
  730. L<preprocess|HTML::Mason::Params/preprocess> and L<postprocess_text|HTML::Mason::Params/postprocess_text>.
  731.  
  732. =item use_source_line_numbers
  733.  
  734. True or false, default is true. Indicates whether component line
  735. numbers that appear in error messages, stack traces, etc. are in terms
  736. of the source file instead of the object file. Mason does this by
  737. inserting '#line' directives into compiled components.  While source
  738. line numbers are more immediately helpful, object file line numbers
  739. may be more appropriate for in-depth debugging sessions.
  740.  
  741. =back
  742.  
  743. =head1 METHODS
  744.  
  745. There are several methods besides the compilation callbacks below that
  746. a Compiler subclass needs to implement.
  747.  
  748. =over 4
  749.  
  750. =item compile(comp_source => <string>, name => <string>, comp_class => <string>)
  751.  
  752. The "comp_class" parameter may be ignored by the compiler.
  753.  
  754. =item object_id
  755.  
  756. This method should return a unique id for the given compiler object.
  757. This is used by the interpreter when loading previously compiled
  758. objects in order to determine whether or not the object should be
  759. re-compiled.
  760.  
  761. =back
  762.  
  763. =head2 Compilation Callbacks
  764.  
  765. These are methods called by the Lexer while processing a component
  766. source.  You may wish to override some of these methods if you're
  767. implementing your own custom Compiler class.
  768.  
  769. =over 4
  770.  
  771. =item start_component()
  772.  
  773. This method is called by the Lexer when it starts processing a
  774. component.
  775.  
  776. =item end_component()
  777.  
  778. This method is called by the Lexer when it finishes processing a
  779. component.
  780.  
  781. =item start_block(block_type => <string>)
  782.  
  783. This method is called by the Lexer when it encounters an opening Mason
  784. block tag like C<< <%perl> >> or C<< <%args> >>.  Its main purpose is
  785. to keep track of the nesting of different kinds of blocks within each
  786. other.  The type of block ("init", "once", etc.) is passed via the
  787. "block_type" parameter.
  788.  
  789. =item end_block(block_type => <string>)
  790.  
  791. This method is called by the Lexer when it encounters a closing Mason
  792. block tag like C<< </%perl> >> or C<< </%args> >>.  Like
  793. C<start_block()>, its main purpose is to help maintain syntactic
  794. integrity.
  795.  
  796. =item *_block(block => <string>, [ block_type => <string> ])
  797.  
  798. Several compiler methods like C<doc_block()>, C<text_block()>, and
  799. C<raw_block()> are called by the Lexer after C<start_block()> when it
  800. encounters blocks of certain types.  These methods actually do the
  801. work of putting the body of a block into the compiled data structure.
  802.  
  803. The methods that follow this pattern are C<init_block()>,
  804. C<perl_block()>, C<doc_block()>, C<text_block()>, and C<raw_block()>.
  805. The last method is called for all C<< <%once> >>, C<< <%cleanup> >>,
  806. C<< <%filter> >>, C<< <%init> >>, C<< <%perl> >>, and C<< <%shared> >>
  807. blocks.
  808.  
  809. =item text(text => <string>)
  810.  
  811. Inserts the text contained in a C<text> parameter into the component
  812. for verbatim output.
  813.  
  814. This is called when the lexer finds plain text in a component.
  815.  
  816. =item variable_declaration( type => <string>, name => <string>, default => <string> )
  817.  
  818. Inserts a variable declaration from the C<< <%args> >> section into
  819. the component.
  820.  
  821. The type will be either "$", "@", or "%", indicating a scalar, array,
  822. or hash.  The name is the variable name without the leading sigil.
  823. The default is everything found after the first "=>" on an C<< <%args> >>
  824. block line, and may include a comment.
  825.  
  826. =item key_value_pair(block_type => <string>, key => <string>, value => <string>)
  827.  
  828. Inserts a key-value pair from a C<< <%flags> >> or C<< <%attr> >>
  829. section into the component.
  830.  
  831. The "block_type" parameter will be either "flags" or "attr".
  832.  
  833. =item start_named_block(block_type => <string>, name => <name>)
  834.  
  835. Analogous to L<item_start_block|start_block>, but starts a "named" block 
  836. (C<< <%method> >> or C<< <%def> >>).
  837.  
  838. =item end_named_block()
  839.  
  840. Called by the Lexer to end a "named" block.
  841.  
  842. =item substitution(substitution => <string>, escape => <string>)
  843.  
  844. Called by the Lexer when it encounters a substitution tag 
  845. (C<< <% ... %> >>).
  846.  
  847. The value of the "escape" parameter will be everything found after the
  848. pipe (|) in the substitution tag, and may be more than one character
  849. such as "nh".
  850.  
  851. =item component_call(call => <string>)
  852.  
  853. Called by the Lexer when it encounters a component call tag without
  854. embedded content (C<< <& ... &> >>).
  855.  
  856. The "call" parameter contains the entire contents of the tag.
  857.  
  858. =item component_content_call(call => <string>)
  859.  
  860. Called by the Lexer when it encounters a component call tag with
  861. embedded content (C<< <&| ... &> >>).
  862.  
  863. =item component_content_call_end()
  864.  
  865. Called by the Lexer when it encounters an ending tag for a component
  866. call with content (C<< </&> >>).  Note that there is no corresponding
  867. C<component_call_end()> method for component calls without content,
  868. because these calls don't have ending tags.
  869.  
  870. =item perl_line(line => <string>)
  871.  
  872. Called by the Lexer when it encounters a C<%>-line.
  873.  
  874. =back
  875.  
  876. =head1 SEE ALSO
  877.  
  878. L<HTML::Mason|HTML::Mason>,
  879. L<HTML::Mason::Admin|HTML::Mason::Admin>,
  880. L<HTML::Mason::Interp|HTML::Mason::Interp>
  881.  
  882. =cut
  883.