NAME
Mojolicious::Plugin::Util::Callback - Reverse helpers for Mojolicious
SYNOPSIS
# Mojolicious
$app->plugin('Util::Callback');
# Mojolicious::Lite
plugin 'Util::Callback';
# In app or plugin
$self->callback(get_cached_profile => sub {
my ($c, $name) = @_;
return $c->cache->get( $name );
});
# In plugin or controller
my $profile = $self->callback(
get_cached_profile => 'Akron'
);
DESCRIPTION
Callbacks are similar to helpers, with a slightly different semantic. While helpers are usually established by plugins and called by controllers, callbacks are usually called by plugins and established by other plugins or on registration of other plugins in the application.
A typical usecase is the database agnostic access to data via plugins.
HELPERS
callback
# Call a callback
my $profile = $self->callback(
get_cached_profile => 'Akron'
);
# Establish callback
$self->callback(get_cached_profile => sub {
my ($c, $name) = @_;
return $c->cache->get( $name );
});
# Define multiple callbacks ...
my $param = {
my_callback_1 => sub { 'Yeah!' },
my_callback_2 => sub { 'Fine!' }
};
# ... and establish them, e.g. when registering a plugin
$self->callback(
[qw/my_callback_1 my_callback_2/] => $param, -once
);
Establish or call a callback. To call a callback, just pass the name and all parameters to the helper. To establish a callback, pass the name and a code reference to release to the helper. The arguments of the callback function will be the controller object followed by all passed parameters from the call. To establish multiple callbacks, e.g. at the start of the registration routine of a plugin, pass an array reference of callback names followed by a hash reference containing the callbacks to the helper. All callback references will be deleted from the hash, while the rest will stay intact.
An additional -once
flag when establishing indicates, that the callbacks are not allowed to be redefined later.
If there is no callback defined for a certain name, undef
is returned on calling.
AVAILABILITY
https://github.com/Akron/Mojolicious-Plugin-Util-Callback
COPYRIGHT AND LICENSE
Copyright (C) 2013, Nils Diewald.
This program is free software, you can redistribute it and/or modify it under the same terms as Perl.