-
-
08 Jan 2015 06:48:58 UTC
- Distribution: Poet
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Repository
- Issues (7)
- Testers (796 / 15 / 0)
- Kwalitee
Bus factor: 1- 85.71% Coverage
- License: perl_5
- Activity
24 month- Tools
- Download (74.45KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- CHI
- Capture::Tiny
- Class::Load
- Data::Rmap
- File::ShareDir
- File::ShareDir::Install
- File::Slurp
- File::Spec::Functions
- Guard
- JSON::XS
- Log::Any::Adapter
- Mason
- Mason::Plugin::Cache
- Mason::Plugin::HTMLFilters
- Mason::Plugin::RouterSimple
- MasonX::ProcessDir
- Method::Signatures::Simple
- Moose
- MooseX::App::Cmd
- MooseX::HasDefaults::RO
- MooseX::StrictConstructor
- Plack
- Plack::Middleware::Debug
- Plack::Middleware::Session
- Plack::Session::Store::Cache
- Try::Tiny
- URI
- URI::Escape
- YAML::XS
- Reverse dependencies
- CPAN Testers List
- Dependency graph
NAME
Poet::Manual::Subclassing - Customizing Poet with subclasses
DESCRIPTION
You can subclass the following Poet classes for your application:
Poet::Cache Poet::Conf Poet::Log Poet::Mason Poet::Import Poet::Plack::Request Poet::Plack::Response
and arrange things so that Poet always uses your subclass instead of its default class.
Place Poet subclasses under
lib/MyApp/Class.pm
in your environment, whereMyApp
is your app name andClass
is the class you are subclassing minus thePoet
prefix. A few of these subclasses are generated for you bypoet new
.For example, to subclass
Poet::Cache
:package MyApp::Cache; use Poet::Moose; extends 'Poet::Cache'; # put your modifications here 1;
(Note: Poet::Moose is Moose plus a few Poet standards. You could also use plain
Moose
here.)Poet will automatically detect, load and use any such subclasses. Internally it uses the app_class environment method whenever it needs a classname, e.g.
# Do something with MyApp::Cache or Poet::Cache $poet->app_class('Cache')->...
Subclassing Mason
As long as you have even a bare-bones
Poet::Mason
subclass, e.g.package MyApp::Mason; use Poet::Moose; extends 'Poet::Mason'; 1;
then your Mason subclasses will be autodetected as well, e.g.
package MyApp::Mason::Interp; use Moose; extends 'Mason::Interp'; # put your modifications here 1;
poet new
will create the bare-bones subclass for you; it is otherwise harmless.See Mason::Manual::Subclasses for more information.
EXAMPLES
Use INI instead of YAML for config files
package MyApp::Conf; use Config::INI; use Moose; extends 'Poet::Conf'; override 'read_conf_file' => sub { my ($self, $file) = @_; return Config::INI::Reader->read_file($file); };
Perform tasks before and after each Mason request
package MyApp::Mason::Request; use Moose; extends 'Mason::Request'; override 'run' => sub { my $self = shift; # Perform tasks before request my $result = super(); # Perform tasks after request return $result; };
Add Perl code to the top of every compiled component
package MyApp::Mason::Compilation; use Moose; extends 'Mason::Compilation'; override 'output_class_header' => sub { return join("\n", super(), 'use Foo;', 'use Bar qw(baz);'); };
Use Log::Dispatch instead of Log4perl for logging
package MyApp::Log; use Log::Any::Adapter; use Log::Dispatch; use Moose; extends 'Poet::Log'; override 'initialize_logging' => sub { my $log = Log::Dispatch->new( ... ); Log::Any::Adapter->set('Dispatch', dispatcher => $log); };
Add your own $dbh quick var
package MyApp::Import use DBI; use Poet::Moose; extends 'Poet::Import'; method provide_dbh ($caller, $poet) { $dbh = DBI->connect(...); }
SEE ALSO
AUTHOR
Jonathan Swartz <swartz@pobox.com>
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.
Module Install Instructions
To install Poet, copy and paste the appropriate command in to your terminal.
cpanm Poet
perl -MCPAN -e shell install Poet
For more information on module installation, please visit the detailed CPAN module installation guide.