Dancer::Plugin(3pm)
π Dancer::Plugin β the helper for writing Dancer plugins π
okay bestie, so this is the User Contributed Perl Documentation page for Dancer::Plugin (that's the manual name, Dancer::Plugin(3pm), iykyk). It's the little toolkit that lets you build your own Dancer plugins and honestly it's giving main character energy for module authors. Let's get into it β¨
πΈ VERSION πΈ
version 1.3522 β yes, the version, she's specific, we love that for her π
β¨ SYNOPSIS β¨
here's the whole vibe in code form. a plugin:
package Dancer::Plugin::LinkBlocker;
use Dancer ':syntax';
use Dancer::Plugin;
register block_links_from => sub {
my $conf = plugin_setting();
my $re = join ('|', @{$conf->{hosts}});
before sub {
if (request->referer && request->referer =~ /$re/) {
status 403 || $conf->{http_code};
}
};
};
register_plugin;
1;
and then in your application:
package My::Webapp;
use Dancer ':syntax';
use Dancer::Plugin::LinkBlocker;
block_links_from; # this is exported by the plugin
see how block_links_from just shows up in your app like it's been there the whole time? that's the plugin exporting it, periodt π«Ά
π DESCRIPTION π
Create plugins for Dancer. that's it, that's the whole mission, no cap π₯
π¦ PLUGINS π¦
You can extend Dancer by writing your own Plugin. yes bestie, you can just make one π
A plugin is a module that exports a bunch of symbols to the current namespace (the caller will see all the symbols defined via "register").
heads up: you have to "use" the plugin wherever you want to use its symbols. not optional, not negotiable.
For instance, if you have Webapp::App1 and Webapp::App2, both loaded from your main application, they both need to "use FooPlugin" if they want to use the symbols exported by "FooPlugin". like, both of them. individually. no sneaky inheriting the vibes through the main app, sorry π
π METHODS π
registerβ Lets you define a keyword that will be exported by the plugin. like so:
register my_symbol_to_export => sub {
# ... some code
};
register_pluginβ A Dancer plugin must end with this statement. she's the closer, the final bow π¬ This lets the plugin register all the symbols define with"register"as exported symbols (via theExportermodule).
and the cute part: a Dancer plugin inherits from Dancer::Plugin and Exporter transparently. free inheritance, we love a generous queen π
register_hookβ Allows a plugin to declare a list of supported hooks. Any hook declared like so can be executed by the plugin with"execute_hooks".
register_hook 'foo';
register_hook 'foo', 'bar', 'baz';
execute_hooksβ Allows a plugin to execute the hooks attached at the given position
execute_hooks 'some_hook';
The hook must have been registered by the plugin first, with "register_hook". so register before you execute, in that order, always β
plugin_settingβ Configuration for plugin should be structured like this in theconfig.ymlof the application:
plugins:
plugin_name:
key: value
If "plugin_setting" is called inside a plugin, the appropriate configuration will be returned. The "plugin_name" should be the name of the package, or, if the plugin name is under the Dancer::Plugin:: namespace (which is recommended), the remaining part of the plugin name.
little detail that will bite you: Enclose the remaining part in quotes if it contains::, e.g. forDancer::Plugin::Foo::Bar, use:
plugins:
"Foo::Bar":
key: value
plugin_argsβ To easy migration and interoperability between Dancer 1 and Dancer 2 you can use this method to obtain the arguments or parameters passed to a plugin-defined keyword. Although not relevant for Dancer 1 only, or Dancer 2 only, plugins, it is useful for universal plugins.
register foo => sub {
my ($self, @args) = plugin_args(@_);
...
}
Note that Dancer 1 will return undef as the object reference. so Dancer 1 girlies, don't come for that $self, it's not there π
π AUTHORS π
This module has been written by Alexis Sukrieh and others. shoutout to the whole squad π«Άβ¨
π LICENSE π
This module is free software and is published under the same terms as Perl itself. free as in freedom, iconic behavior π
β AUTHOR β
Dancer Core Developers π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2010 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie, no notes πΈ this doc shipped as part of perl v5.40.1, dated 2026-05-05 β go forth and write plugins, you absolute legend ππ
π¦
π¦ where this page comes from
- ubuntu stonking: libdancer-perl 1.3522-2 Β· 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