Class::Hookable - Base class for hook mechanism
package MyApp::Plugins; use base qw( Class::Hookable ); my $hook = MyApp::Plugins->new; $hook->register_hook( $plugin, 'hook.name' => $plugin->can('callback'), ); $hook->run_hook('hook.name', $args);
Class::Hookable is the base class for the hook mechanism. This module supports the hook mechanism like Plagger.
This module was made based on the hook mechanism of Plagger. I thank Tatsuhiko Miyagawa and Plagger contributors.
NOTE:
Class::Hookable is having substantial changes from version 0.05 to version0.06. When using Class::Hookable, please be careful.
Please see Changes file about a change point.
my $hook = Class::Hookalbe->new;
This method is a constructor of Class::Hookable. Nothing but that is being done.
$hook->register_hook( $plugin, 'hook.A' => $plugin->can('callbackA'), 'hook.B' => $plugin->can('callbackB'), );
This method registers a plugin object and callbacks which corresponds to hooks.
The plugin object is specified as the first argument, and one after that is specified by the order of 'hook' => \&callabck.
'hook' => \&callabck
Only when $hook->class_hookable_filter( 'run_hook', $hook, $action ) has returned truth, the callback specified by this method is registered with a hook.
$hook->class_hookable_filter( 'run_hook', $hook, $action )
Please see "class_hookable_filter" about $hook->class_hookable_filter.
$hook->class_hookable_filter
$hook->register_method( $plugin, 'method.A' => $plugin->can('methodA'), 'method.B' => $plugin->can('methodB'), );
This method registers a plugin and functions with the methods.
The specification of arguments is same as "register_hook" method.
The method is different from hook and only a set of plugin and function are kept about one method. When specifying the method name which exists already, the old method is replaced with the new method.
Only when $hook->class_hookable_filter( 'register_method', $method, $action ) has returned truth, this method registers a plugin and function.
$hook->class_hookable_filter( 'register_method', $method, $action )
$hook->run_hook( $hook, $args, $once, $callback ); my @results = $hook->run_hook('hook.name', \%args, undef, \&callback); my $result = $hook->run_hook('hook.name', \%args, 1, \&callback);
This method calls callback of the registered plugin to hook by the registered order. Arguments are specified by the order of $hook, $args, $once and $callback.
$hook
$args
$once
$callback
Arguments to run_hook method:
Designation of the hook with which a plugin was registered. This argument is indispensable.
The argument which passes it to callback. This argument is optional.
When this argument becomes true, this method finishes calling callback when the first return value has been received.
This argument is optional.
my $callback = sub { my ( $result ) = @_; # some code }
This argument specifies code reference.
When having received a return value from callback of the registered, the callback specified by this argument is called.
A return value of callback of registered plugin is passed to an argument of this callback.
Arguments of registered callback:
sub callback { my ( $plugin, $context, $args ) = @_; # some code }
The argument by which it is passed to callback is $plugin, $context, $args.
$plugin
$context
The plugin object which passed a plugin and callback to the register_hook method when registering.
The context object.
When $hook->hookable_context is specified, the specified object is passed, and when it isn't so, object of Class::Hookable (or object of inherited Class::Hookable class) is passed.
$hook->hookable_context
Please see "hookable_context" about context object which can be specified in $hook->hookable_context.
the argument specified by the run_hook method.
Only when $hook->class_hookable_filter( 'run_hook', $hook, $action, $args ) has returned truth, this method calls callback.
$hook->class_hookable_filter( 'run_hook', $hook, $action, $args )
my $result = $hook->run_hook_once( $hook, $args, $callback );
This method is an alias of $hook->run_hook( $hook, $args, 1, \&callback ).
$hook->run_hook( $hook, $args, 1, \&callback )
my $result = $hook->call_method( $method => $args );
This method calls function of the registered plugin to method. Arguments are specified by the order of $method and $args.
$method
When the function was not found, no this methods are returned.
Arguments of call_method method:
Designation of the method name with which a plugin was registered. This argument is indispensable.
The argument which passes it to function. This argument is optional.
Arguments of registered function:
sub function { my ( $plugin, $context, $args ) = @_; # some code }
The plugin object which passed a plugin and function to the register_method method when registering.
Please see "class_hookable_context" about context object which can be specified in $hook->hookable_context.
The argument specified by the run_hook method.
Only when $hook->class_hookable_filter( 'call_method', $method, $action, $args ) has returned truth, this method calls function.
$hook->class_hookable_filter( 'call_method', $method, $action, $args )
$hook->class_hookable_set_filter( register_hook => \&filterA run_hook => \&filterB, );
This method registers the filter of hook and method.
Arguments are specified by the order of 'name' => \&filter. The character which can be used for the filter name is [a-zA-Z_].
'name' => \&filter
[a-zA-Z_]
When registering a homonymous filter of the filter registered already, a old filter is replaced with a new filter.
Please see "class_hookable_filter" about a calling of the filter.
$hook->class_hookable_filter_prefix('myfilter_prefix');
This method is accessor of filter prefix.
When finding filter in $hook->class_hookable_filter method, prefix specified by this method is used.
The character which can be used for the filter prefix is [a-zA-Z_].
When prefix is not specified, this method returns 'class_hookable_filter' by default.
'class_hookable_filter'
my $result = $hook->class_hookable_filter( $filter => @args );
This method calls a specified filter.
A filter name is specified as the first argument and an argument to a filter is specified as an argument after that.
Search of filter:
This method searches for a filter from several places.
First, when $hook->can("${prefix}_${filter_name}") is defined, its method is used as a filter.
$hook->can("${prefix}_${filter_name}")
${prefix} is return value of $hook->class_hookable_filter_prefix, and ${filter_name} is the filter name specified as this method.
${prefix}
$hook->class_hookable_filter_prefix
${filter_name}
Please see "class_hookable_filter_prefix" about $hook->class_hookable_filter_prefix.
Next when a specified filter is specified by $hook->class_hookable_set_filter method, the filter is used.
$hook->class_hookable_set_filter
When a filter was not found, this method uses the filter to which truth is just always returned.
Arguments of filter:
$hook->class_hookable_set_filter( register_hook => sub { my ( $self, $filter, $hook, $action ) = @_; }, register_method => sub { my ( $self, $filter, $method, $action ) = @_; }, run_hook => sub { my ( $self, $filter, $hook, $action, $args ) = @_; }, call_method => sub { my ( $self, $filter, $method, $action, $args ) = @_; }, )
The filters Class::Hookable calls are 'register_hook', 'register_method', 'run_hook' and 'call_method', and the argument to which it's passed is as follows.
'register_hook'
'register_method'
'run_hook'
'call_method'
$self
Instance of Class::Hookable ( or the class inheriting to Class::Hookable ).
This argument is passed to all filters.
$filter
The filter name called in $hook->class_hookable_filter.
@args
Arguments to the filter to which it was passed by $hook->class_hookable_filter.
The argument when Class::Hookable calls a filter, is as follows.
the hook name or the method name.
This argument is passed to all 'register_hook', 'reigster_method', 'run_hook' and 'call_method'.
'reigster_method'
$action
# In case of hook my ( $plugin, $callback ) = @{ $action }{qw( plugin callback )}; # In case of method my ( $plugin, $function ) = @{ $action }{qw( plugin function )};
The hash reference including the plugin and the callback or function.
The argument specified by the run_hook or call_method method.
This argument is passed to 'run_hook' and 'call_method'.
my $all_hook_data = $hook->registered_hooks; # $all_hook_data = { 'hook.name' => [ { plugin => $plugin callback => \&callback }, ... ] ... }; my @all_hook_name = $hook->registered_hooks; # @all_hook_name = qw( hook.A hook.B hook.C ... ); my $registered_data = $hook->registered_hooks( $plugin ); # $registered_data = [ 'hook.A' => \&callabck, 'hook.B' => \&callback, ... ]; my @registered_hooks = $hook->registered_hooks( $plugin ); # @registered_hooks = qw( hook.A hook.B hook.C ... ); my $registered_data = $hook->registered_hooks( \&callback ); # $registered_data = [ 'hook.A' => $plugin, 'hook.B' => $plugin, ... ]; my @registered_hooks = $hook->registered_hooks( \&callback ); # @registered_hooks = qw( hook.A hook.B hook.C ... );
This method gets registered data or a hook name.
This method returns registered data in scalar context and returns registered hooks name in list context.
List of arguments and return values:
This method returns all registered data.
This data is same as $hook->class_hookable_hooks.
$hook->class_hookable_hooks
This method returns a registered all hook name.
This method returns the hook name with which a plugin is registered and the callback when registering.
The returned value is ARRAY reference, and the contents are the following feeling.
[ 'hook.A' => \&callbackA, 'hook.B' => \&callbackB, ... ]
This method returns the hook name with which a plugin is registered.
\&callback
This method returns the hook name with which callback is registered and the plugin when registering.
[ 'hook.A' => $plugin, 'hook.B' => $plugin, ... ]
This method returns the hook name with which callback is registered.
my $all_hook_data = $hook->registered_methods; # $all_hook_data = { 'method.name' => { plugin => $plugin, function => \&function, }, ... }; my @all_hook_name = $hook->registered_methods; # @all_hook_name = qw( method.A method.B method.C ... ); my $registered_data = $hook->registered_methods( $plugin ); # $registered_data = [ 'method.A' => \&function, 'method.B' => \&function, ... ]; my @registered_methods = $hook->registered_methods( $plugin ); # @registered_methods = qw( method.A method.B method.C ... ); my $registered_data = $hook->registered_methods( \&function ); # $registered_data = [ 'method.A' => \&function, 'method.B' => \&function ]; my @registered_methods = $hook->registered_hooks( \&function ); # @registered_methods = qw( method.A method.B method.C ... );
This method gets registered data or a method name.
This method returns registered data in scalar context and returns registered methods name in list context.
This data is same as $hook->class_hookable_methods.
$hook->class_hookable_methods
This method returns a registered all method name.
This method returns the method name with which a plugin is registered and the function when registering.
[ 'method.A' => \&functionA, 'method.B' => \&functionB, ... ]
This method returns the method name with which a plugin is registered.
\&function
This method returns the method name with which function is registered and the plugin when registering.
[ 'method.A' => $plugin, 'method.B' => $plugin, ... ]
$hook->remove_hook( target => $plugin ); $hook->remove_hook( target => $plugin->can('callback') ); $hook->remove_hook( target => { plugin => $plugin, callback => $plugin->can('callback') } ); $hook->remove_hook( target => [ $pluginA \&callbackB, \%expressonC ] ); $hook->remove_hook( target => $plugin, from => 'hook.name' ); $hook->remove_hook( target => $plugin, from => [qw( hook.A hook.B )] ); $hook->remove_hook( target => \@targets, from => \@hooks ); my $removed = $hook->remove_hook( target => $plugin );
This method deleted hook which matches the specified condition.
About Arguments:
'target'
An deleted target is specified.
They are $plugin, \&callback and \%hash_ref that it can be specified as this argument.
\%hash_ref
When specifying plugin and callback in hash reference, hook with designated plugin and calllback is deleted.
It is possible to specify more than one deletion target using Array reference.
This argument is indispensable.
'from'
It is specified in which hook an deletion target is deleted.
More than one hook can be specified by using Arrary reference.
When this argument was not specified, an deletion target is deleted from all hook.
About Return value
This method returns deleted hook to a return value.
A return value is the following feeling:
$removed = { 'hook.A' => [ { plugin => $plugin, callback => \&callback }, { plugin => $plugin, callback => \&callback }, ], 'hook.B' => [ { plugin => $plugin, callback => \&callback }, { plugin => $plugin, callback => \&callback }, ], }
$hook->remove_method( target => $plugin ); $hook->remove_method( target => $plugin->can('function') ); $hook->remove_method( target => { plugin => $plugin, function => $plugin->can('function') } ); $hook->remove_method( target => [ $pluginA \&functionB, \%expressonC ] ); $hook->remove_method( target => $plugin, from => 'method.name' ); $hook->remove_method( target => $plugin, from => [qw( method.A method.B )] ); $hook->remove_method( target => \@targets, from => \@methods, ); my $removed = $hook->remove_method( target => $plugin );
This method deleted method which matches the specified condition.
They are $plugin, \&function and \%hash_ref that it can be specified as this argument.
When specifying plugin and callback in hash reference, hook with designated plugin and function is deleted.
It is specified in which method an deletion target is deleted.
More than one method can be specified by using Arrary reference.
When this argument was not specified, an deletion target is deleted from all method.
This method returns deleted method to a return value.
$removed = { 'method.A' => { plugin => $plugin, function => \&function }, 'method.B' => { plugin => $plugin, function => \&function }, }
# clear 'hook.A' and 'hook.B' $hook->clear_hooks(qw( hook.A hook.B )); # clear all $hook->clear_hooks; my $removed = $hook->clear_hooks;
This method deleted all registered hooks.
An deleted hook name is specified as an argument.
When arguments were specified, all plugin registered with specified hooks are deleted, and when arguments are not specified, all plugins are deleted from all hooks.
A return value of this method is such feeling:
$removed = { 'hook.A' => [ { plugin => $pluginA, callback => $pluginA->can('foo') }, { plugin => $pluginB, callback => $pluginB->can('bar') }, ... ], 'hook.B' => [ { plugin => $pluginA, callback => $pluginA->can('foo') }, { plugin => $pluginB, callback => $pluginB->can('bar') }, ... ], ... };
# clear 'method.A' and 'methodB' $hook->clear_methods(qw( method.A method.B )); # clear all $hook->clear_methods; my $removed = $hook->clear_methods;
This method deletes all registered method.
An deleted method name is specified as an argument.
When arguments were specified, a plugin registered with specified method is deleted, and when arguments are not specified, all plugins are deleted from all methods.
$removed = { 'method.A' => { plugin => $pluginA, function => $pluginA->can('foo') }, 'method.B' => { plugin => $pluginB, function => %pluginB->can('bar') }, ... };
my $data = $hook->class_hookable_stash;
This method is stash in Class::Hookable. All variables Class::Hookable needs are put here.
This method does not get arguments, and return hash reference includes all variables.
# set $hook->class_hookable_context( $context ); # get my $context = $hook->class_hookable_context;
This method is accessor of context object.
Context object specified by this method is passed as the second argument of the subroutine registered with hook and method.
see also "run_hook" and "call_method".
my $hooks = $hook->class_hookable_hooks;
This method is accessor to hash reference which keeps hooks. all method of Class::Hookable is accessing hooks through this method.
my $methods = $hook->class_hookable_methods;
This method is accesor to hash reference which keeps methods. all method of Class::Hookable is accessing methods through this method.
my $filters = $hook->class_hookable_filters;
This method is accessor to hash reference which keeps filters. all filter of Class::Hookable is accessing filters through this method.
Original idea by Tatsuhiko Miyagawa http://search.cpan.org/~miyagawa in Plagger
Code by Naoki Okamura (Nyarla) <nyarla[:)]thotep.net>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::Hookable, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Hookable
CPAN shell
perl -MCPAN -e shell install Class::Hookable
For more information on module installation, please visit the detailed CPAN module installation guide.