Poet::Mason(3pm)
π Poet::Mason π
okay so bestie, welcome to Poet::Mason β that's the section 3pm page, Perl-contributed-docs girlies know the vibe π
. It's the Mason settings + enhancements situation for Poet, and honestly? It's giving backstage pass to your whole app. Let's go π¦
β¨ SYNOPSIS β¨
# In a conf file...
mason:
plugins:
- Cache
- TidyObjectFiles
- +My::Mason::Plugin
static_source: 1
static_source_touch_file: ${root}/data/purge.dat
# Get the main Mason instance
my $mason = Poet::Mason->instance();
# Create a new Mason object
my $mason = Poet::Mason->new(...);
πΈ DESCRIPTION πΈ
So this is a Poet-specific Mason subclass, no cap. It does three iconic things: it sets up sane default settings β¨, it maintains a main Mason instance for handling web requests π, and it adds Poet-specific methods to $m (which is the Mason request object, fyi, she's the main character here π
).
π CLASS METHODS π
get_optionsβ returns a hash of Mason options, made by combining the default settings with your configuration. Lowkey the blueprint of everything π§instanceβ returns the main Mason instance used for web requests, and it's created with the options fromget_options. This is the one you actually want, bestie π«Άnewβ returns a brand new main Mason object, also using options fromget_options. But fr fr, unless you specifically need a new object, you probably want to callinstanceinstead. Don't be extra for no reason π
π DEFAULT SETTINGS π
comp_rootis set to$poet->comps_dirβ which by default is thecompssubdirectory under the environment root πdata_diris set to$poet->data_dirβ which by default is thedatasubdirectory under the environment root πpluginsis set to includeCache,HTMLFiltersandRouterSimpleπ₯cache_root_class(a parameter of theCacheplugin, iykyk) is set toMyApp::Cacheif it exists β replacingMyAppwith your app name, obviously β otherwise it falls back toPoet::Cache. She's flexible like that π
π CONFIGURATION π
The Poet configuration entry 'mason', if there is one, gets treated as a hash of options that supplements and/or overrides the defaults up there β¬οΈ. And if that hash contains 'extra_plugins', those get added to the default plugins. Obsessed. e.g.:
mason:
static_source: 1
static_source_touch_file: ${root}/data/purge.dat
extra_plugins:
- AnotherFavoritePlugin
π¦ QUICK VARS AND UTILITIES π¦
Poet inserts this line at the top of every compiled Mason component (yes, every single one, she's thorough):
use Poet qw($conf $poet :web);
which means $conf, $poet, and the web utilities are available from every component. Periodt. π
π NEW REQUEST METHODS π
Under Poet, these extra web-related methods are available in the Mason request object β you can reach them in components via $m, or anywhere else via "Mason::Request->current_request" π
req ()β a reference to thePlack::Requestobject. e.g.
my $user_agent = $m->req->headers->header('User-Agent');
res ()β a reference to thePlack::Responseobject. e.g.
$m->res->content_type('text/plain');
abort (status)clear_and_abort (status)
These methods are overridden to set the response status before aborting, if a status is provided. e.g. to send back a FORBIDDEN result:
$m->clear_and_abort(403);
which is equivalent to:
$m->res->status(403);
$m->clear_and_abort();
And if a status is not provided? The methods work just as before, no drama π
redirect (url[, status])β sets the headers and status for a redirect, then clears the Mason buffer and aborts the request. e.g.
$m->redirect("http://somesite.com", 302);
is equivalent to:
$m->res->redirect("http://somesite.com", 302);
$m->clear_and_abort();
not_found ()β sets the status to404, then clears the Mason buffer and aborts the request. e.g.
$m->not_found();
is equivalent to:
$m->clear_and_abort(404);
sessionβ a shortcut for"$m->req->session", aka the Plack session π. This is simply a persistent hash that you can read from and write to. It's tied to the user's browser session via cookies πͺ and stored in a file cache in the data directory (by default).
my $value = $m->session->{key};
$m->session->{key} = { some_complex => ['value'] };
send_json ($data)β outputs the JSON-encoded$data, sets the content type to"application/json", and aborts. e.g.
method handle {
my $data;
# compute data somehow
$m->send_json($data);
}
"send_json" is a shortcut for:
$m->clear_buffer;
$m->print(JSON::XS::encode_json($data));
$m->res->content_type("application/json");
$m->abort();
π SEE ALSO π
Poet β go read her too, she's the whole vibe π«Ά
π AUTHOR π
Jonathan Swartz <swartz@pobox.com> β thank you king π
π 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. Slay, legally speaking βοΈβ¨
and that's a wrap bestie!! this lil doc moment was brought to you by perl v5.34.0, dated 2022-06-18, and it's the Poet::Mason(3pm) page β go forth and build something iconic π
πΈπ¦
π¦ where this page comes from
- debian bookworm: libpoet-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libpoet-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libpoet-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libpoet-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libpoet-perl 0.16-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms