Dancer2...::Config(3)
β¨ NAME β¨
Dancer2::Config β the Dancer2 Configuration Reference π (yes bestie, this is the Dancer2...::Config(3) page from the User Contributed Perl Documentation, and it's about to be your fave)
π VERSION π
version 2.2.1
π DESCRIPTION π
okay so. The Dancer2 default configuration system β implemented by Dancer2::ConfigReader::Config::Any β is the thing that handles reading and changing the configuration of your Dancer2 apps. Iconic behaviour honestly.
This doc walks you through how to manipulate Dancer2's configuration settings (either through code or via a file), and it also documents all the various settings that are available in Dancer2. So like, it's giving reference manual and tutorial energy. π¦
πΈ MANIPULATING SETTINGS VIA CODE πΈ
You can change a setting with the keyword set:
use Dancer2;
# changing default settings
set port => 8080;
set content_type => 'text/plain';
set startup_info => 0;
literally that easy. no cap. β¨
π MANIPULATING SETTINGS VIA CONFIGURATION FILES π
ngl, there is absolutely nothing wrong with using set to configure your application. In fact you might have some genuinely great reasons for doing so. But for greater flexibility, ease of deployment, etc., you should also consider extracting those settings into a configuration file. bestie behaviour, fr fr.
π Configuration file path and file names
Dancer2 will first look for the file config.EXT (where EXT is the type of configuration file you're using; e.g. ini or json or yml) in the root directory of your application. This is considered your global Dancer2 config file. If you do not care to have separate settings for production and development environments (and btw, that is not a recommended practice!), then this file is all you need.
Next, Dancer2 will look for a file called config_local.EXT. This file is typically useful for deployment-specific configuration that should not be checked into source control. For instance, database credentials could be stored in this file. Any settings in this file are merged into the existing configuration such that those with the same name in your local configuration file will take precedence over those settings in the global file. π
Next, Dancer2 will look in the environments directory for a configuration file specific to the platform you are deploying to (production.EXT or development.EXT, for example). Again, the configuration from the environment is merged with the existing configuration, with the deployment config taking precedence.
Finally, Dancer2 will look in the environments directory for a local configuration for the specific platform you are deploying to (e.g. production_local.EXT or development_local.EXT). The configuration in this file is merged as before.
Much like config_local.EXT, this file would be useful for environment-specific configuration that would not be checked into source control. For instance, when developing an application that talks to multiple services, each developer could have their own URLs to those services stored within their environments/development_local.yaml file. so cute, so organised. π
β οΈ Note, if there is noconfig.EXT, Dancer2 will not look for aconfig_local.EXT. The same is true for the local environment configuration. π
π§ Supported configuration file formats
Dancer2 supports any configuration file format that is supported by Config::Any. At the time of this writing, that includes:
- YAML (
.ymland.yaml) - JSON (
.jsnand.json) - INI (
.ini) - Apache-style configurations (
.cnfand.conf) - XML (
.xml) - Perl-style hashes (
.pland.perl)
Dancer2 iterates over these file extensions in the order provided by Config::Any and loads any config files that it finds, with later configuration information overriding earlier config information. To restrict which file extension Dancer2 looks for, you may set the DANCER_CONFIG_EXT environment variable to a specific extension and Dancer2 will only look for config files with that extension. π«Ά
Make sure you pick the appropriate extension for your configuration file name, as Dancer2 guesses the type of format based on the file extension.
π Sample configuration files
Note: Not all possibilities are covered here, only the most common options.
If you prefer YAML, a sample YAML based config file might look like this:
appname: "Hello"
charset: "UTF-8"
auto_page: 1
session: "YAML"
serializer: "JSON"
plugins:
DBIC:
default:
dsn: dbi:SQLite:db/mydata.db
schema_class: Hello::Schema
If JSON is more your thing, your file might look more like this:
{
"appname": "Hello",
"charset": "UTF-8",
"auto_page": "1",
"session": "YAML",
"serializer": "JSON",
"plugins": {
"DBIC": {
"default": {
"dsn": "dbi:SQLite:db/mydata.db",
"schema_class": "Hello::Schema"
}
}
}
}
If you like Apache configuration files, try something similar to:
appname = Hello
charset = UTF-8
auto_page = 1
session = YAML
serializer = JSON
<plugins>
<DBIC>
<default>
dsn = dbi =SQLite =db/mydata.db
schema_class = Hello = =Schema
</default>
</DBIC>
</plugins>
INI-style files are deliberately simplistic and not recommended for use in your Dancer2 applications. π
π¦ USING ALTERNATE CONFIG READERS π¦
Dancer2 allows you to use your own configuration system too. See the Dancer2::ConfigReader documentation for more information.
π Bootstrapping other config readers
You can use the default Dancer2 configuration reader to load additional configuration/configuration readers:
additional_config_readers:
- Dancer2::ConfigReader::SQLite:
path: /path/to/sqlite.db
table: config
See the Dancer2::ConfigReader documentation for more information and examples.
π SUPPORTED SETTINGS π
π₯ Run mode and listening interface/port
host(string) β The IP address that the Dancer2 app should bind to. Default is0.0.0.0, i.e. bind to all available interfaces.port(int) β The port Dancer2 will listen to. Default value is3000. This setting can be changed on the command-line with the--portswitch.behind_proxy(boolean) β If set to true, Dancer2 will look toX-Forwarded-ProtocolandX-Forwarded-hostwhen constructing URLs (for example, when usingredirectorhost). This is useful if your application is behind a proxy.
Note: If either of these are missing, the values of the proxy server will be used instead. For example, if the client sends a HTTP/1.0 request to a proxy that is hosted locally, thenhostwill return the valuelocalhost. In a similar vein, if the client makes a secure connection to the proxy, but the proxy does not passX-Forwarded-Protocol, thenbasewill returnhttp://.... For these reasons, it is recommended that the values are hard-configured in the proxy if possible. For Apache this would be:
RequestHeader set X_FORWARDED_PROTO "https"
RequestHeader set X_FORWARDED_HOST "www.example.com"
no_default_middleware(boolean) β If set to true, your Dancer2 application will NOT be wrapped with the default PSGI middleware. The default middleware wrappers are:Plack::Middleware::FixMissingBodyInRedirectPlack::Middleware::Head
additional_config_readersβDancer2::ConfigReaderinstances to bootstrap the application's configuration. See the ConfigReader's POD for more information.
π Content type / character set
content_type(string) β The default content type of outgoing content. Default value is'text/html'.charset(string) β This setting has multiple effects:- It sets the default charset of outgoing content.
charset=item will be added to Content-Type response header. - It makes Unicode bodies in HTTP responses of
text/*types to be encoded to this charset. - It also indicates to Dancer2 in which charset the static files and templates are encoded.
- If you're using
Dancer2::Plugin::Database, UTF-8 support will automatically be enabled for your database β see "AUTOMATIC UTF-8 SUPPORT" inDancer2::Plugin::Database
- It sets the default charset of outgoing content.
Default value is UTF-8. HTTP responses without charset will be interpreted as ISO-8859-1 by most clients.
If charset is empty, Dancer2 will not encode response bodies. Handlers should return already-encoded byte strings.
You can cancel any charset processing by specifying your own charset in Content-Type header or by ensuring that response body leaves your handler without Unicode flag set (by encoding it into some 8bit charset, for example).
Serialized JSON responses are UTF-8 encoded by the JSON serializer. Pass character strings to the serializer so it can do the encoding once. β¨
strict_utf8(boolean) β Controls how invalid UTF-8 bytes are handled at decoding boundaries (request path/parameters) and in JSON serialization.- If false (default), invalid UTF-8 is left as bytes and a warning is emitted.
- If true, invalid UTF-8 causes an immediate error. π
default_mime_type(string) β Dancer2'sDancer2::Core::MIMEmodule usesapplication/dataas a default mime type. This setting lets the user change it. For example, if you have a lot of files being served in the public folder that do not have an extension, and are text files, set thedefault_mime_typetotext/plain.
π Serializing responses
serializer(string) β When writing a webservice, data serialization/deserialization is a common issue to deal with. Dancer2 can automatically handle that for you, via a serializer.
π Available serializer engines
The following serializers are available, be aware they dynamically depend on Perl modules you may not have on your system.
- JSON β Requires
JSON. - YAML β Requires
YAML, - XML β Requires
XML::Simple. - Mutable β Will try to find the appropriate serializer using the Content-Type and Accept-type header of the request.
β Serializer engine
The serializer can be configured in a separate engines section, like so:
serializer: "JSON"
engines:
serializer:
JSON:
pretty: 1
See documentation for a particular serializer for supported options.
πΈ File / directory locations
environment(string) β This is the name of the environment that should be used. Standard Dancer2 applications have aenvironmentsfolder with specific configuration files for different environments (usually development and production environments). They specify different kind of error reporting, deployment details, etc. These files are read after the genericconfig.ymlconfiguration file.appdir(directory)
Okay so bestie, this is the path where your application will live π It's where Dancer2 goes looking by default for your config files, your templates and your static content.
It's typically set by use Dancer2 to use the same directory as your script.
public_dir (directory)
This is the directory where your static files get stored. Any file that already exists in there will be served as a static file, and it happens before anything gets matched against a route. so your CSS is safe, no cap.
See also static_handler.
Default: $appdir/public.
static_handler (boolean)
You have to declare this one and set it to true if you go and modify the standard public_dir location. yeah, it's a whole thing.
Default: true if $ENV{DANCER_PUBLIC} is set or public_dir is set to $appdir/public.
views (directory)
This is where your templates and layouts live. It's the "view" part of MVC (model, view, controller) β¨
Default: $appdir/views.
viewscan only be set once, preferably via config. Once the template engine is initialized, changing theviewssetting has no effect. periodt.
πΈ Templating & layouts πΈ
template
Lets you configure which template engine should be used. Like, to use Template Toolkit, you drop this into config.yml:
template: template_toolkit
layout (string)
The name of the layout to use when rendering a view. Dancer2 will look for a matching template in the directory $views/layouts.
You can override the default layout using the third argument of the template keyword. Check the Dancer2 manpage for the deets.
layout_dir (string)
A relative path where the layouts reside inside the views directory.
layout_dir: actual_layouts
Default: layouts.
π Logging, debugging and error handling π
startup_info (boolean)
Set it to true and it prints a banner when the server starts, with info like versions and the environment (or "dancefloor"). π
Conforms to the environment variable DANCER_STARTUP_INFO.
traces (boolean)
If you set it to true, Dancer2 will display full stack traces whenever a warning or a die happens. (Internally it sets Carp::Verbose.) Defaults to false.
strict_config (boolean)
If it's true, Dancer2 will warn about unknown configuration keys at the top level, and about unknown keys in built-in engine sections where the options are actually known. Set it to false if you intentionally store application-specific settings at the top level, or if you use custom engines with extra configuration keys.
strict_config_allow (array)
A list of top-level configuration keys that strict config warnings should just ignore. This lets you keep strict_config enabled while still permitting application-specific keys. best of both worlds honestly.
strict_config: 1
strict_config_allow:
- my_custom_key
- feature_flags
no_server_tokens (boolean)
If true, Dancer2 won't add an X-Powered-By header, and it will avoid setting the Server header when you're using Dancer2's standalone server.
Defaults to false.
You can also use the environment variable DANCER_NO_SERVER_TOKENS.
logger (enum)
Pick which logger to use. For example, to write to log files with Dancer2::Logger::File:
logger: File
Or to direct log messages to the console you started your Dancer2 app from, with Dancer2::Logger::Console:
logger: Console
Loggers are configured with a corresponding Logger engine section, shown below π
session (enum)
This setting lets you enable a session engine for your web application. By default, sessions are disabled in Dancer2 β you have to choose a session engine to use them.
Sessions are configured with a corresponding Session engine section, shown below π
show_errors (boolean)
Deprecated configuration. π Use show_stacktrace, described below, instead.
show_stacktrace (boolean)
If true, Dancer2 renders a detailed debug screen whenever an error gets caught. If false, Dancer2 renders the default error page, using $public/$error_code.html if it exists, $views/$error_code.tt, or the template specified by the error_template setting.
The error screen attempts to sanitise sensitive-looking information (passwords / card numbers in the request, etc), but you still should not have show_stacktrace enabled while in production β there's still a risk of divulging details. fr fr, do not.
error_template (template path)
This setting lets you specify a template to be used in case of a runtime error. Right now, the template (as well as the $views/$error_code.tt templates) can use four variables:
titleβ the error title.contentβ the error-specific content (if any).statusβ the HTTP status code throwing that error.exceptionβ the stringified exception (e.g.$@) if any.
Keep in mind that content and exception can vary depending on the problem.
For example:
- A 404 has an empty
exception, andcontentcontains the URI that was not found. Unless you do the 404 yourself viasend_error("You chose ... poorly!", 404);, and thencontentisYou chose ... poorly!. - A 500 because of, say, dividing 0 by 0 will have an empty
contentand anexceptionlikeIllegal division by zero at .... - A 401 from
send_error("You can not know the secret until you sign in grasshopper!", 401);will have an emptyexception, andcontentwill containYou can not know the secret until you sign in grasshopper!.
π Logger engine π
The logger must be configured in a separate engines section, like so:
logger: Console
engines:
logger:
Console:
log_level: core
All loggers support the configuration options below. See the documentation for a particular logger for other supported options.
log_level
This option tells which log messages should actually be logged. Possible values are core, info, debug, warning or error.
coreβ all messages are logged, including some from Dancer2 itselfdebugβ all messages are loggedinfoβ only info, warning and error messages are loggedwarningβ only warning and error messages are loggederrorβ only error messages are logged
During development, you'll probably want to use debug to see your own debug messages, and core if you need to see what Dancer2 is doing. In production, you'll likely want error or warning only, for less-chatty logs.
πͺ Session engine πͺ
The session engine is configured in the engines section.
session: Simple
engines:
session:
Simple:
cookie_name: dance.set
cookie_duration: '24 hours'
cookie_same_site: Lax
is_secure: 1
is_http_only: 1
See Dancer2::Core::Role::SessionFactory for more detailed documentation for these options, or the particular session engine for other supported options.
cookie_name
The name of the cookie to store the session ID in. Defaults to dancer.session. This can be overridden by certain session engines.
cookie_domain
The domain of the cookie. By default there is no domain defined for the cookie.
cookie_path
The path of the cookie. By default there is no path defined for the cookie.
cookie_duration
The session expiry time in seconds, or as e.g. 2 hours (see expires in Dancer2::Core::Cookie). By default, there is no specific expiry time.
cookie_same_site
Restricts the session cookie to a first-party or same-site context. Valid values are Strict, Lax, or None.
Refer to RFC6265bis https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site for further details regarding same-site context.
is_secure
The user's session ID is stored in a cookie. If the is_secure setting is set to a true value, the cookie will be marked as secure, meaning it should only be sent over HTTPS connections. π
is_http_only
This setting defaults to 1 and instructs the session cookie to be created with the HttpOnly option active, meaning that JavaScript will not be able to access its value. π
π¦ auto_page (boolean) π¦
For simple pages where you're not doing anything dynamic, but you still want to use the template engine to provide headers etc, you can use the auto_page feature to avoid needing to create a route for every single page. iconic.
With auto_page enabled, if the requested path doesn't match any specific route, Dancer2 will check in the views directory for a matching template, and use it to satisfy the request if it's found.
Simply enable auto_page in your config:
auto_page: 1
Then, if you request /foo/bar, Dancer2 will look in the views dir for /foo/bar.tt.
Dancer2 will honor your before_template_render code, and all default variables. They will be accessible and interpolated on automatically served pages.
π§ dsl_class π§
For complex applications that require extended DSL keywords or other functionality, the DSL class used can be specified at import time or in the config settings.
dsl_class: 'My::DSL'
This is the same as specifying
use Dancer2 dsl => 'My::DSL'
in your module. dsl_class defaults to Dancer2::Core::DSL if not specified.
π Environment variables π
DANCER_CONFIG_READERSβ Specifies which configuration reader classes can be used to load Dancer2 configuration.DANCER_CONFDIRβ Sets the configuration directory. This is only guaranteed to work with the default config reader.DANCER_CONFIG_VERBOSEβ Outputs a lot of debugging information when reading the configuration of the application. This is only guaranteed to work with the default config reader.DANCER_ENVDIRβ Sets the environment directory. This correlates to theenvdirconfig option. This is only guaranteed to work with the default config reader.PLACK_ENVβ Sets the given environment. This can be overridden byDANCER_ENVIRONMENT.DANCER_ENVIRONMENTβ Sets the given environment. This takes higher precedence overPLACK_ENV. If neitherPLACK_ENVorDANCER_ENVIRONMENTis set, the environment defaults to development.DANCER_APPHANDLERβ TheDANCER_APPHANDLERconfiguration controls what thedancekeyword does. If it is set toPSGI(which will automatically be set ifPLACK_ENVis set),dancewill return the PSGI application coderef. Otherwise (which is the default βStandalone), it runs the Plack standalone server with the application.DANCER_PORTβ Sets the port which will be used by the development server (if not run by plackup).DANCER_SERVERβ Sets the host the development server will be used by the development server (if not run by plackup).
Note: this might change in the future.
DANCER_STARTUP_INFOβ Controls whether to display start up info.DANCER_NO_SERVER_TOKENS- ...and this one, the one we were literally just talking about, controls whether to display the server tokens. π
DANCER_PUBLICβ sets the public directory location. that's where your static lil files live, bestie. πDANCER_TRACESβ sets the tracing flag, which sets Carp's$Verboseflag. so when stuff goes sideways you get the whole story. πDANCER_VIEWSβ sets the views (templates) directory. your templates are literally the moment. πDANCER_LOGGERβ sets the logger engine. pick your fave, she'll be logging everything. πDANCER_CHARSETβ sets the default charset. πDANCER_CONTENT_TYPEβ sets the default content type. and if you don't set it? it defaults totext/html, no cap. π«Ά
β¨ SEE ALSO β¨
Dancer2 β go say hi, she's the whole reason we're here. π
π AUTHOR π
Dancer Core Developers β the collective icons behind all of this. absolute legends, every single one. π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2026 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.
fr fr, free as in do-whatever-you-want-with-it (as long as you keep the same terms as Perl 5 itself). slay. π
and that's the whole page, bestie β we did it π¦ this was Dancer2...::Config(3) from the User Contributed Perl Documentation, shipped with perl v5.42.3, dated 2026-09-20. ty for reading, ily, go build something iconic β¨πΈ
π¦ where this page comes from
- fedora 44: perl-Dancer2 2.2.1-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms