Dancer2 is a more powerful, new generation of Dancer, the lightweight web-framework for Perl. It is a complete rewrite based on Moo and is known as a “micro” web framework, modeled after a Ruby framework called Sinatra that is used to build web applications by building a list of HTTP verbs, URLs (called routes), and methods to handle that type of traffic to that specific URL.
Dancer2 ecosystem is growing with several new features embedded regularly. Dancer2 is also a future proof platform, which is why it is suggested for all new developments.
- Very Easy and Simple – Intuitive, minimalist and very expressive syntax.
- Flexible – PSGI support, plugins and modular design allow for strong scalability.
- Few Dependencies – Dancer depends on as few CPAN modules as possible making it easy to install.
Required PERL Modules
- Template Toolkit
You can configure Dancer2 as per your needs. The Dancer2 configuration (as implemented by Dancer2::core::Role::ConfigReader) handles reading and changing the configuration of your Dancer2 apps. Though the document you’ll learn to manipulate Dancer2’s configuration settings and also document the different settings available in Dancer2.
For easy and automatic installation of Dancer, we recommend using cpanminus:
curl -L http://cpanmin.us | perl – –sudo Dancer2
Creating an Application
Although, a Dancer application can be a simple one-liner, it is imperative that you know the complete structure for its beneficial in creating complex applications. Dancer is shipped with a helper that can scaffold a new skeleton and assists you in developing complex applications.
$ dancer2 -a MyWeb::App
The code ensures everything is ready and works fine out-of-the-box, including the design, jQuery, and environment-related configurations.
Starting to Dance
$ cd MyWeb-App
$ plackup -r bin/app.psgi
>> Dancer server 16622 listening on http://0.0.0.0:5000
== Entering the development dance floor …
Changing Settings via Code
You can change a setting with the keyword set as follows:
# changing default settings
set port => 8080;
set content_type => ‘text/plain’;
set startup_info => 0;
Changing Settings via Configuration Files
Configuration File Path and File Names
Dancer2 first searches for the file “config.EXT” (where EXT is the type of configuration file in use – ini or json or yml) in the root directory of your application. This file is considered as your global Dancer2 config file. If you do not wish to have separate settings for production and development environments, which isn’t a recommended practice by experts, then this file is all that you require.
After this, Dancer2 will search for a file named as “config_local.EXT.” This file is meant for deployment-specific configuration and you shouldn’t use it for source control. For instance, the file may contain database credentials. Settings stored in this file are merged into the existing configuration. This means the setting with similar name in your global configuration file will override the settings defined in the global file.
Dancer2 will next search the “environments” directory for a configuration file specific to the deployment platform (the name of the file might be production.EXT or development.EXT). The configuration in the file is merged with the existing configuration with the deployment config overriding other settings.
Lastly, Dancer2 searches the “environments” directory for a local configuration for the specific deployment platform. The file name might be prodcution_local.EXT or development_local.EXT. The configuration in this file is merged as before.
Supported Configuration File Formats
Dancer2 supports all configuration file format supported by Config::Any, including YAML (.yml and .yaml), JSON (.jsn and .json), INI (.ini), Apache-style configurations (.cnf and .conf), XML (.xml), and Perl-style hashes (.pl and .perl).
Dancer2 goes over over the file extensions in a pre-defined order provided by Config::Any and loads config files that it finds with the latest configuration information overriding earlier config information. If you wish to restrict the file extension that Dancer2 searches, you may set the DANCER_CONFIG_EXT environment variable to the particular extension and Dancer2 will only search for config files with particular extension.
Also, Dancer2 guesses the type of format based on the file extension, which makes it crucial for you to pick the correct extension for your configuration file.
Sample Configuration Files
A sample YAML based config file might look like this:
If you like JSON more, the config file might look more like this:
If you prefer Apache, your file may contain the following code:
appname = Hello
charset = UTF-8
auto_page = 1
session = YAML
serializer = JSON
dsn = dbi =SQLite =db/mydata.db
schema_class = Hello = =Schema
Wish to Learn More Dance Moves? Check out these sources
Dancer2::Manual – a gentle introduction to Dancer
Dancer2::Tutorial – a walkthrough creating an example application
Dancer2::Cookbook – examples for achieving common tasks with Dancer
Dancer2 – main documentation, listing all the Dancer DSL keywords
All in all, Perl Dancer2 is an improvisation over Dancer and offers a more stable and powerful web-framework. You may also try changing the configuration settings in Perl Dancer2 and explore a wider aspect of the magical world of Perl Dancer2.
Do not forget to tell us about your experiences in the comments section.
Until next time.
- Mobile App Automation Testing using ‘ESPRESSO’
If you are a Mobile Apps Test Engineer, you cannot overlook the very reliable Google Product i.e. Espresso. Espresso is an automatic UI testing or as we call it “hands…
- Microservices Architecture
Coined over a decade ago, Microservices is a buzzword in today’s technical arena. Microservices are known as micro web services based on SOAP and enables breaking of a large system…
- Perl One-Liners
What are Perl One-Liners? As the name suggests, Perl One-Liners are small single line of codes that can perform several tasks, ranging from changing line spacing and numbering lines to…
- Understanding Memory Tuning in JVM- A Case Study and Analysis
JVM Heap Model The JVM heap model consists of the Young generation and the Old generation memory. The newly created objects are allocated to the young generation memory, as they…
- Teradata and JSON – Monetizing the Internet of Things (IoT)
The prevalent influence of technology has resulted in a widespread use of a variety of devices such as cell phones, sensors, web API’s and browsers. These devices generate huge volume…
- REST API development: Node, Express, Mongo and Mongoose
The basic purpose of this post is to explain the concept about how we can develop REST API (web services) using node.js, express.js framework and Mongo DB using mongoose . For the purpose of this blog I…