{
    "mode": "perldoc",
    "parameter": "HTML::Mason::Compiler",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/HTML%3A%3AMason%3A%3ACompiler/json",
    "generated": "2026-06-15T14:10:54Z",
    "synopsis": "package My::Funky::Compiler;\nuse base qw(HTML::Mason::Compiler);",
    "sections": {
        "NAME": {
            "content": "HTML::Mason::Compiler - Compile Mason component source\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package My::Funky::Compiler;\n\nuse base qw(HTML::Mason::Compiler);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The compiler starts the compilation process by calling its lexer's \"lex\" method and passing\nitself as the \"compiler\" parameter. The lexer then calls various methods in the compiler as it\nparses the component source.\n\nPARAMETERS TO THE new() CONSTRUCTOR\nallowglobals\nList of variable names, complete with prefix (\"$@%\"), that you intend to use as globals in\ncomponents. Normally global variables are forbidden by \"strict\", but any variable mentioned\nin this list is granted a reprieve via a \"use vars\" statement. For example:\n\nallowglobals => [qw($DBH %session)]\n\nIn a modperl environment, $r (the request object) is automatically added to this list.\n\ndefaultescapeflags\nEscape flags to apply to all <% %> expressions by default. The current valid flags are\n\nh - escape for HTML ('<' => '&lt;', etc.)\nu - escape for URL (':' => '%3A', etc.)\n\nThe developer can override default escape flags on a per-expression basis; see the escaping\nexpressions section of the developer's manual.\n\nIf you want to set *multiple* flags as the default, this should be given as a reference to\nan array of flags.\n\nenableautoflush\nTrue or false, default is true. Indicates whether components are compiled with support for\nautoflush. The component can be compiled to a more efficient form if it does not have to\ncheck for autoflush mode, so you should set this to 0 if you can.\n\nlexer\nThe Lexer object to associate with this Compiler. By default a new object of class\nlexerclass will be created.\n\nlexerclass\nThe class to use when creating a lexer. Defaults to HTML::Mason::Lexer.\n\npreprocess\nSub reference that is called to preprocess each component before the compiler does it's\nmagic. The sub is called with a single parameter, a scalar reference to the script. The sub\nis expected to process the script in-place. This is one way to extend the HTML::Mason syntax\nwith new tags, etc., although a much more flexible way is to subclass the Lexer or Compiler\nclass. See also postprocesstext and postprocessperl.\n\npostprocesstext\nSub reference that is called to postprocess the text portion of a compiled component, just\nbefore it is assembled into its final subroutine form. The sub is called with a single\nparameter, a scalar reference to the text portion of the component. The sub is expected to\nprocess the string in-place. See also preprocess and postprocessperl.\n\npostprocessperl\nSub reference that is called to postprocess the Perl portion of a compiled component, just\nbefore it is assembled into its final subroutine form. The sub is called with a single\nparameter, a scalar reference to the Perl portion of the component. The sub is expected to\nprocess the string in-place. See also preprocess and postprocesstext.\n\nusesourcelinenumbers\nTrue or false, default is true. Indicates whether component line numbers that appear in\nerror messages, stack traces, etc. are in terms of the source file instead of the object\nfile. Mason does this by inserting '#line' directives into compiled components. While source\nline numbers are more immediately helpful, object file line numbers may be more appropriate\nfor in-depth debugging sessions.\n",
            "subsections": []
        },
        "ACCESSOR METHODS": {
            "content": "All of the above properties have read-only accessor methods of the same name.\n\nYou cannot change any property of a compiler after it has been created - among other things,\nthis would potentially invalidate any existing cached component objects or object files. Your\nbest bet is to create different compiler objects and load them into different interpreters.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "There are several methods besides the compilation callbacks below that a Compiler subclass needs\nto implement.\n",
            "subsections": [
                {
                    "name": "compile",
                    "content": "This method has several parameters:\n\n*       compsource (required)\n\nEither a scalar or reference to a scalar containing the component source.\n\n*       name (required)\n\nThe name of the component. This should be the filename of the component if it is\nfile-based, or some other clear identifier of the component source.\n\n*       comppath (required)\n\nThis should be the component's path.\n\n*       fh (optional)\n\nIf this is given then the output of the compiler will be sent directly to this\nhandle, rather than being buffered in memory. This is an optimization to avoid\nmemory usage.\n\nobjectid\nThis method should return a unique id for the given compiler object. This is used by the\ninterpreter when determining the object directory, for example.\n"
                },
                {
                    "name": "Compilation Callbacks",
                    "content": "These are methods called by the Lexer while processing a component source. You may wish to\noverride some of these methods if you're implementing your own custom Compiler class.\n"
                },
                {
                    "name": "start_component",
                    "content": "This method is called by the Lexer when it starts processing a component.\n"
                },
                {
                    "name": "end_component",
                    "content": "This method is called by the Lexer when it finishes processing a component.\n"
                },
                {
                    "name": "start_block",
                    "content": "This method is called by the Lexer when it encounters an opening Mason block tag like\n\"<%perl>\" or \"<%args>\". Its main purpose is to keep track of the nesting of different kinds\nof blocks within each other. The type of block (\"init\", \"once\", etc.) is passed via the\n\"blocktype\" parameter.\n"
                },
                {
                    "name": "end_block",
                    "content": "This method is called by the Lexer when it encounters a closing Mason block tag like\n\"</%perl>\" or \"</%args>\". Like \"startblock()\", its main purpose is to help maintain\nsyntactic integrity.\n\n*block(block => <string>, [ blocktype => <string> ])\nSeveral compiler methods like \"docblock()\", \"textblock()\", and \"rawblock()\" are called by\nthe Lexer after \"startblock()\" when it encounters blocks of certain types. These methods\nactually do the work of putting the body of a block into the compiled data structure.\n\nThe methods that follow this pattern are \"initblock()\", \"perlblock()\", \"docblock()\",\n\"textblock()\", and \"rawblock()\". The last method is called for all \"<%once>\",\n\"<%cleanup>\", \"<%filter>\", \"<%init>\", \"<%perl>\", and \"<%shared>\" blocks.\n"
                },
                {
                    "name": "text",
                    "content": "Inserts the text contained in a \"text\" parameter into the component for verbatim output.\n\nThis is called when the lexer finds plain text in a component.\n"
                },
                {
                    "name": "variable_declaration",
                    "content": "Inserts a variable declaration from the \"<%args>\" section into the component.\n\nThe type will be either \"$\", \"@\", or \"%\", indicating a scalar, array, or hash. The name is\nthe variable name without the leading sigil. The default is everything found after the first\n\"=>\" on an \"<%args>\" block line, and may include a comment.\n"
                },
                {
                    "name": "key_value_pair",
                    "content": "Inserts a key-value pair from a \"<%flags>\" or \"<%attr>\" section into the component.\n\nThe \"blocktype\" parameter will be either \"flags\" or \"attr\".\n"
                },
                {
                    "name": "start_named_block",
                    "content": "Analogous to itemstartblock, but starts a \"named\" block (\"<%method>\" or \"<%def>\").\n"
                },
                {
                    "name": "end_named_block",
                    "content": "Called by the Lexer to end a \"named\" block.\n"
                },
                {
                    "name": "substitution",
                    "content": "Called by the Lexer when it encounters a substitution tag (\"<% ... %>\").\n\nThe value of the \"escape\" parameter will be everything found after the pipe (|) in the\nsubstitution tag, and may be more than one character such as \"nh\".\n"
                },
                {
                    "name": "component_call",
                    "content": "Called by the Lexer when it encounters a component call tag without embedded content (\"<&\n... &>\").\n\nThe \"call\" parameter contains the entire contents of the tag.\n"
                },
                {
                    "name": "component_content_call",
                    "content": "Called by the Lexer when it encounters a component call tag with embedded content (\"<&| ...\n&>\").\n"
                },
                {
                    "name": "component_content_call_end",
                    "content": "Called by the Lexer when it encounters an ending tag for a component call with content\n(\"</&>\"). Note that there is no corresponding \"componentcallend()\" method for component\ncalls without content, because these calls don't have ending tags.\n"
                },
                {
                    "name": "perl_line",
                    "content": "Called by the Lexer when it encounters a \"%\"-line.\n"
                }
            ]
        },
        "SUBCLASSING": {
            "content": "We recommend that any parameters you add to Compiler be read-only, because the compiler\nobjectid is only computed once on creation and would not reflect any changes to Lexer\nparameters.\n",
            "subsections": []
        }
    },
    "summary": "HTML::Mason::Compiler - Compile Mason component source",
    "flags": [],
    "examples": [],
    "see_also": []
}