1Dancer::ModuleLoader(3)User Contributed Perl DocumentatioDnancer::ModuleLoader(3)
2
3
4

NAME

6       Dancer::ModuleLoader - dynamic module loading helpers for Dancer core
7       components
8

VERSION

10       version 1.3513
11

SYNOPSIS

13       Taken directly from Dancer::Template::TemplateToolkit (which is core):
14
15           die "Template is needed by Dancer::Template::TemplateToolkit"
16             unless Dancer::ModuleLoader->load('Template');
17
18           # we now have Template loaded
19

DESCRIPTION

21       Sometimes in Dancer core we need to use modules, but we don't want to
22       declare them all in advance in compile-time. These could be because the
23       specific modules provide extra features which depend on code that isn't
24       (and shouldn't) be in core, or perhaps because we only want these
25       components loaded in lazy style, saving loading time a bit. For
26       example, why load Template (which isn't required by Dancer) when you
27       don't use Dancer::Template::TemplateToolkit?
28
29       To do such things takes a bit of code for localizing $@ and "eval"ing.
30       That code has been refactored into this module to help Dancer core
31       developers.
32
33       Please only use this for Dancer core modules. If you're writing an
34       external Dancer module (Dancer::Template::Tiny,
35       Dancer::Session::Cookie, etc.), please simply ""use ModuleYouNeed"" in
36       your code and don't use this module.
37

METHODS/SUBROUTINES

39   load
40       Runs something like ""use ModuleYouNeed"" at runtime.
41
42           use Dancer::ModuleLoader;
43           ...
44           Dancer::ModuleLoader->load('Something')
45               or die "Couldn't load Something\n";
46
47           # load version 5.0 or more
48           Dancer::ModuleLoader->load('Something', '5.0')
49               or die "Couldn't load Something\n";
50
51           # load version 5.0 or more
52           my ($res, $error) = Dancer::ModuleLoader->load('Something', '5.0');
53           $res or die "Couldn't load Something : '$error'\n";
54
55       Takes in arguments the module name, and optionally the minimum version
56       number required.
57
58       In scalar context, returns 1 if successful, 0 if not.  In list context,
59       returns 1 if successful, "(0, "error message")" if not.
60
61       If you need to give argument to the loading module, please use the
62       method "load_with_params"
63
64   require
65       Runs a ""require ModuleYouNeed"".
66
67           use Dancer::ModuleLoader;
68           ...
69           Dancer::ModuleLoader->require('Something')
70               or die "Couldn't require Something\n";
71           my ($res, $error) = Dancer::ModuleLoader->require('Something');
72           $res or die "Couldn't require Something : '$error'\n";
73
74       If you are unsure what you need ("require" or "load"), learn the
75       differences between "require" and "use".
76
77       Takes in arguments the module name.
78
79       In scalar context, returns 1 if successful, 0 if not.  In list context,
80       returns 1 if successful, "(0, "error message")" if not.
81
82   load_with_params
83       Runs a ""use ModuleYouNeed qw(param1 param2 ...)"".
84
85           use Dancer::ModuleLoader;
86           ...
87           Dancer::ModuleLoader->load_with_params('Something', qw(param1 param2) )
88               or die "Couldn't load Something with param1 and param2\n";
89
90           my ($res, $error) = Dancer::ModuleLoader->load_with_params('Something', @params);
91           $res or die "Couldn't load Something with @params: '$error'\n";
92
93       Takes in arguments the module name, and optionally parameters to pass
94       to the import internal method.
95
96       In scalar context, returns 1 if successful, 0 if not.  In list context,
97       returns 1 if successful, "(0, "error message")" if not.
98
99   use_lib
100       Runs a ""use lib qw(path1 path2)"" at run time instead of compile time.
101
102           use Dancer::ModuleLoader;
103           ...
104           Dancer::ModuleLoader->use_lib('path1', @other_paths)
105               or die "Couldn't perform use lib\n";
106
107           my ($res, $error) = Dancer::ModuleLoader->use_lib('path1', @other_paths);
108           $res or die "Couldn't perform use lib : '$error'\n";
109
110       Takes in arguments a list of path to be prepended to @INC, in a similar
111       way than "use lib". However, this is performed at run time, so the list
112       of paths can be generated and dynamic.
113
114       In scalar context, returns 1 if successful, 0 if not.  In list context,
115       returns 1 if successful, "(0, "error message")" if not.
116
117   class_from_setting
118       Given a setting in Dancer::Config, composes the class it should be.
119
120       This is the function that translates:
121
122           # in config.yaml
123           template: "template_toolkit"
124
125       To the class:
126
127           Dancer::Template::TemplateToolkit
128
129       Example:
130
131           use Dancer::ModuleLoader;
132           my $class = Dancer::ModuleLoader->class_from_setting(
133               'Dancer::Template' => 'template_toolkit',
134           );
135
136           # $class == 'Dancer::Template::TemplateToolkit
137
138           $class = Dancer::ModuleLoader->class_from_setting(
139               'Dancer::Template' => 'tiny',
140           );
141
142           # class == 'Dancer::Template::Tiny
143

SEE ALSO

145       Module::Load, Module::New::Loader
146

AUTHOR

148       Dancer Core Developers
149
151       This software is copyright (c) 2010 by Alexis Sukrieh.
152
153       This is free software; you can redistribute it and/or modify it under
154       the same terms as the Perl 5 programming language system itself.
155
156
157
158perl v5.32.0                      2020-07-28           Dancer::ModuleLoader(3)
Impressum