Catalyst::Plugin::Hooks - Add hooks to Catalyst engine actions
In MyApp.pm:
use Catalyst qw( -Debug Hooks );
In Some model:
sub new { my $self = shift; my ( $c ) = @_; $self = $self->NEXT::new( @_ ); open my $filehandle, "> foo.log"; $c->add_after_finalize_hook( sub { my $c = shift; $filehandle->flush(); $c->log->info( "Flushed filehandle after finalize" ); } ); return $self; }
Don't use this plugin!
Use Catalyst::Plugin::Observe. All functionality provided in C:P:Hooks will very shortly be available in C:P:Observe. C:P:Hooks is probably not going to work in the next Catalyst release, so rewrite your code to use C:P:Observe.
This Plugin is usefull for when you want to run some code before or after a catalyst engine action. Consider writing a Catalyst plugin if you implement more general functionality. But let's say you want to flush your log's filehandle after the request is done (then the requestor doesn't have to wait for your log's to be flushed). It would be nice to put the code for flushing the filehandle next to the rest of the code that's bothered with the filehandle, so you don't have to pass the filehandle around. Example for this is shown in the "SYNOPSIS".
Hooks are addable everywhere a $c exists, even in Controllers. But remember, adding a hook every request will cause a memory overflow. So don't put these methods in Controller actions.
All of these methods are currently hookable:
handle_request prepare prepare_request prepare_connection prepare_query_parameters prepare_headers prepare_cookies prepare_path prepare_body prepare_body_parameters prepare_parameters prepare_uploads prepare_action dispatch finalize finalize_uploads finalize_error finalize_headers finalize_cookies finalize_body
To add a before hook: $c->add_ <method name> _hook( sub { some code } );
To add an after hook: $c->add_after_ <method name> _hook( sub { some code } );
$c->add_before_ <method name> _hook is an alias to $c->add_ <method name> _hook.
$c->add_before_ <method name> _hook
$c->add_ <method name> _hook
To remove all hooks for an action: $c->remove_all_ <method name> _hooks;
To remove all hooks set by this module: $c->remove_all_hooks;
Catalyst, Catalyst::Manual::Internals for when the different actions are called.
Berik Visschers <berikv@xs4all.nl>
To install Catalyst::Plugin::Hooks, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Plugin::Hooks
CPAN shell
perl -MCPAN -e shell install Catalyst::Plugin::Hooks
For more information on module installation, please visit the detailed CPAN module installation guide.