The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mason::Manual::FAQ - Frequently asked questions about Mason

FREQUENTLY ASKED QUESTIONS

Components

Can I create global variable(s) that can be seen from all components?

Mason components each run in their own packages, so if you set a regular global in one you won't be able to see it in the others.

But you can use allow_globals and set_global to create globals accessible from all components.

Why does my output have extra newlines/whitespace and how can I get rid of it?

See Whitespace And Newlines in the syntax manual. To suppress extra newlines you can use a backslash at the end of each line, or you can use the NoBlankLines filter.

To emit binary data without the risk of inserting extra whitespace, surround your code with $m->clear_buffer and $m->abort:

    <%init>
    $m->clear_buffer;
    open(my $fh, '<', 'binary-file') or die $!;
    my $buffer;
    while (read $fh, $buffer, 8192) {
        $m->print($buffer);
    }
    $m->abort;
    </%init>

I'm trying to generate an image or other binary file, but it seems to be getting corrupted.

This is almost always caused by unwanted whitespace or other output at the beginning or end of your binary data. Use $m->clear_buffer and $m->abort as in previous answer.

How do I put comments in components?

See Comments section in the syntax manual for reference.

  • Put general comments in the <%doc> section.

  • Within code blocks (<%class>, <%init>, <%perl>, etc.), use standard Perl comments ('#').

  • Use <% # %> for single or multi-line comments anywhere outside of Perl sections.

  • If you are producing HTML, you can use standard HTML comments delimited by <!-- -->. The difference is that these comments will appear in the final output.

What's a good way to temporarily comment out code in a component?

For HTML, you might be tempted to surround the section with <!-- -->. But be careful! Any code inside the section will still execute. Here's a example of commenting out a call to an ad server:

    <!-- temporarily comment out
    <& /shared/fetch_ad.mi &>
    -->

The ad will still be fetched and counted, but not displayed!

A better way to block out a section is if (0):

    % if (0) {
      ...
    % }

Code blocked out in this way will neither be executed nor displayed, and multiple if (0) blocks can be nested inside each other (unlike HTML comments).

Another way to block out code is with a <%doc> tag, although this not cannot be nested.

How can I capture the output of a component (and modify it, etc.) instead of having it automatically output?

Use $m->scomp.

How can I capture the output from arbitrary code that calls components, etc.?

Use $m->capture.

How can I get a list of components matching a path pattern?

Use $m->glob_paths, e.g.

    my @paths = $m->glob_paths('/some/comp/path/*');

This will work even with multiple component roots; you'll get a combined list of all matching component paths in all component roots.

How can I access $m (the request object) from outside a component, e.g. inside a regular class?

Use Mason::Request->current_request:

    package Foo;

    sub bar {
        my $m = Mason::Request->current_request;
    }

When using multiple component roots, is there a way to explicitly call a component in a specific root?

Multiple component roots were designed to work just like Perl's @INC. A given component path matches exactly one file, the first file found in an ordered search through the roots. There is no way to explicitly ask for a file in a specific root.

HTTP and HTML

How do I use Mason with mod_perl / FastCGI / (insert backend here)?

With Mason::Plugin::PSGIHandler, Mason becomes a standard PSGI application, so it can be used with any of the Plack handlers. See Plack::Handler::Apache1, Plack::Handler::Apache2, Plack::Handler::FCGI, and Plack::Handler::CGI.

Why is Mason so slow with standard CGI?

Under standard CGI you must load all modules and initialize your environment with every request. Mason's startup cost (mostly due to Moose) will make this particularly sub-optimal. Ask yourself whether you absolutely have to use CGI, and if not, switch to a persistent solution like mod_perl or Fast CGI or Starman.

How do I access GET or POST arguments?

GET and POST arguments are automatically parsed and passed to the page component. So you can get at GET/POST data by declaring the appropriate attributes and/or consulting $.args. See Run parameters in the PSGIHandler documentation.

I'm passing multiple values for a key in a query string, why is only one value showing up?

You need to declare the corresponding attribute as an ArrayRef. See Run parameters in the PSGIHandler documentation.

What happens if I include query args in a POST?

Query string and POST arguments are combined (via Plack::Request's parameters).

How do I do an external redirect?

Use $m->redirect, which is added by PSGIHandler.

How do I return a not found/404 result?

Use $m->not_found, which is added by PSGIHandler.

SEE ALSO

Mason

AUTHOR

Jonathan Swartz <swartz@pobox.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 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.