Dancer...Loader(3pm)

πŸ’– NAME πŸ’–

Dancer::ModuleLoader β€” dynamic module loading helpers for Dancer core components πŸŽ€

This page is Dancer...Loader(3pm), from the User Contributed Perl Documentation corner of the world, and bestie, it's here to teach you how to load modules whenever you feel like it instead of at compile time. Slay. ✨


🌸 VERSION 🌸

version 1.3522</purl> β€” okay ignore that typo, it's version 1.3522. Periodt.


✨ SYNOPSIS ✨

Straight-up borrowed from Dancer::Template::TemplateToolkit (which is core, iykyk):

die "Template is needed by Dancer::Template::TemplateToolkit"
  unless Dancer::ModuleLoader->load('Template');

# we now have Template loaded

Look at that. Chef's kiss. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the situation, bestie: sometimes in Dancer core we gotta use modules, but we do not wanna declare them all up front at compile-time. Why? Well, maybe those specific modules bring extra features, and those features depend on code that isn't β€” and honestly shouldn't be β€” in core. Or maybe we just want these components loaded in lazy style, saving a lil loading time. 🌈

Like, why load Template (which Dancer doesn't even require!) when you literally never use Dancer::Template::TemplateToolkit? It's giving unnecessary.

Doing this whole thing properly takes a bit of code for localizing $@ and eval-ing. That code? Refactored into this very module, to help Dancer core developers out. 🫢

⚠️ Please only use this for Dancer core modules. If you're writing an external Dancer module (Dancer::Template::Tiny, Dancer::Session::Cookie, etc.), just use ModuleYouNeed in your code and leave this module alone. No cap. πŸ™ˆ

πŸ‘‘ METHODS/SUBROUTINES πŸ‘‘

πŸ’« load

Runs something like use ModuleYouNeed β€” but at runtime. Main character behavior, honestly.

use Dancer::ModuleLoader;
...
Dancer::ModuleLoader->load('Something')
    or die "Couldn't load Something\n";

# load version 5.0 or more
Dancer::ModuleLoader->load('Something', '5.0')
    or die "Couldn't load Something\n";

# load version 5.0 or more
my ($res, $error) = Dancer::ModuleLoader->load('Something', '5.0');
$res or die "Couldn't load Something : '$error'\n";

Takes in arguments the module name, and optionally the minimum version number required. ⭐

In scalar context, returns 1 if successful, 0 if not. In list context, returns 1 if successful, (0, "error message") if not.

πŸ’‘ Need to hand an argument to the loading module? Bestie, use the method load_with_params instead.

πŸŽ€ require

Runs a require ModuleYouNeed. Simple as that.

use Dancer::ModuleLoader;
...
Dancer::ModuleLoader->require('Something')
    or die "Couldn't require Something\n";
my ($res, $error) = Dancer::ModuleLoader->require('Something');
$res or die "Couldn't require Something : '$error'\n";

If you're unsure what you need (require or load), go learn the differences between require and use. Genuinely, it'll change your life. 😭

Takes in arguments the module name.

In scalar context, returns 1 if successful, 0 if not. In list context, returns 1 if successful, (0, "error message") if not.


🌸 load_with_params

Runs a use ModuleYouNeed qw(param1 param2 ...). Now with flavor. πŸ“

use Dancer::ModuleLoader;
...
Dancer::ModuleLoader->load_with_params('Something', qw(param1 param2) )
    or die "Couldn't load Something with param1 and param2\n";

my ($res, $error) = Dancer::ModuleLoader->load_with_params('Something', @params);
$res or die "Couldn't load Something with @params: '$error'\n";

Takes in arguments the module name, and optionally parameters to pass to the import internal method.

In scalar context, returns 1 if successful, 0 if not. In list context, returns 1 if successful, (0, "error message") if not.


🌈 use_lib

Runs a use lib qw(path1 path2) at run time instead of compile time. Dynamic queen. πŸ‘‘

use Dancer::ModuleLoader;
...
Dancer::ModuleLoader->use_lib('path1', @other_paths)
    or die "Couldn't perform use lib\n";

my ($res, $error) = Dancer::ModuleLoader->use_lib('path1', @other_paths);
$res or die "Couldn't perform use lib : '$error'\n";

Takes in arguments a list of path to be prepended to @INC, in a similar way than use lib. However, this is performed at run time, so the list of paths can be generated and dynamic. That's the whole point, bestie. πŸ’…

In scalar context, returns 1 if successful, 0 if not. In list context, returns 1 if successful, (0, "error message") if not.


🧚 class_from_setting

Given a setting in Dancer::Config, composes the class it should be. It's giving translator. πŸ”₯

This is the function that translates:

# in config.yaml
template: "template_toolkit"

To the class:

Dancer::Template::TemplateToolkit

Example:

use Dancer::ModuleLoader;
my $class = Dancer::ModuleLoader->class_from_setting(
    'Dancer::Template' => 'template_toolkit',
);

# $class == 'Dancer::Template::TemplateToolkit

$class = Dancer::ModuleLoader->class_from_setting(
    'Dancer::Template' => 'tiny',
);

# class == 'Dancer::Template::Tiny

(Yes, those trailing quotes in the comments are exactly like that in the original. We're not fixing it, we're respecting it. 😌)


πŸ”₯ SEE ALSO πŸ”₯

  • Module::Load
  • Module::New::Loader

πŸ’œ AUTHOR πŸ’œ

Dancer Core Developers 🫢


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.


perl v5.40.1 Β· 2026-05-05 Β· and that's the whole Dancer...Loader(3pm) page, bestie β€” go load some modules dynamically, you absolute icon. βœ¨πŸ’–

πŸ“¦ where this page comes from