1Dancer::Plugin(3) User Contributed Perl Documentation Dancer::Plugin(3)
2
3
4
6 Dancer::Plugin - helper for writing Dancer plugins
7
9 version 1.3513
10
12 package Dancer::Plugin::LinkBlocker;
13 use Dancer ':syntax';
14 use Dancer::Plugin;
15
16 register block_links_from => sub {
17 my $conf = plugin_setting();
18 my $re = join ('|', @{$conf->{hosts}});
19 before sub {
20 if (request->referer && request->referer =~ /$re/) {
21 status 403 || $conf->{http_code};
22 }
23 };
24 };
25
26 register_plugin;
27 1;
28
29 And in your application:
30
31 package My::Webapp;
32
33 use Dancer ':syntax';
34 use Dancer::Plugin::LinkBlocker;
35
36 block_links_from; # this is exported by the plugin
37
39 Create plugins for Dancer
40
42 You can extend Dancer by writing your own Plugin.
43
44 A plugin is a module that exports a bunch of symbols to the current
45 namespace (the caller will see all the symbols defined via "register").
46
47 Note that you have to "use" the plugin wherever you want to use its
48 symbols. For instance, if you have Webapp::App1 and Webapp::App2, both
49 loaded from your main application, they both need to "use FooPlugin" if
50 they want to use the symbols exported by "FooPlugin".
51
52 METHODS
53 register
54 Lets you define a keyword that will be exported by the plugin.
55
56 register my_symbol_to_export => sub {
57 # ... some code
58 };
59
60 register_plugin
61 A Dancer plugin must end with this statement. This lets the plugin
62 register all the symbols define with "register" as exported symbols
63 (via the Exporter module).
64
65 A Dancer plugin inherits from Dancer::Plugin and Exporter
66 transparently.
67
68 register_hook
69 Allows a plugin to declare a list of supported hooks. Any hook
70 declared like so can be executed by the plugin with
71 "execute_hooks".
72
73 register_hook 'foo';
74 register_hook 'foo', 'bar', 'baz';
75
76 execute_hooks
77 Allows a plugin to execute the hooks attached at the given position
78
79 execute_hooks 'some_hook';
80
81 The hook must have been registered by the plugin first, with
82 "register_hook".
83
84 plugin_setting
85 Configuration for plugin should be structured like this in the
86 config.yml of the application:
87
88 plugins:
89 plugin_name:
90 key: value
91
92 If "plugin_setting" is called inside a plugin, the appropriate
93 configuration will be returned. The "plugin_name" should be the
94 name of the package, or, if the plugin name is under the
95 Dancer::Plugin:: namespace (which is recommended), the remaining
96 part of the plugin name.
97
98 Enclose the remaining part in quotes if it contains ::, e.g. for
99 Dancer::Plugin::Foo::Bar, use:
100
101 plugins:
102 "Foo::Bar":
103 key: value
104
105 plugin_args
106 To easy migration and interoperability between Dancer 1 and Dancer
107 2 you can use this method to obtain the arguments or parameters
108 passed to a plugin-defined keyword. Although not relevant for
109 Dancer 1 only, or Dancer 2 only, plugins, it is useful for
110 universal plugins.
111
112 register foo => sub {
113 my ($self, @args) = plugin_args(@_);
114 ...
115 }
116
117 Note that Dancer 1 will return undef as the object reference.
118
120 This module has been written by Alexis Sukrieh and others.
121
123 This module is free software and is published under the same terms as
124 Perl itself.
125
127 Dancer Core Developers
128
130 This software is copyright (c) 2010 by Alexis Sukrieh.
131
132 This is free software; you can redistribute it and/or modify it under
133 the same terms as the Perl 5 programming language system itself.
134
135
136
137perl v5.36.0 2022-07-22 Dancer::Plugin(3)