Post3

PERL DANCER2

Posted by Vijayant Kumar

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.

Key Features

  • 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

  • Dancer2
  • Template Toolkit
  • File::Slurper
  • DBD::SQLite

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.

Installing Dancer

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

+ MyWeb-App

+ MyWeb-App/config.yml

+ MyWeb-App/cpanfile

+ MyWeb-App/Makefile.PL

+ MyWeb-App/MANIFEST.SKIP

+ MyWeb-App/bin

+ MyWeb-App/bin/app.pl

+ MyWeb-App/bin/app.psgi

+ MyWeb-App/environments

+ MyWeb-App/environments/development.yml

+ MyWeb-App/environments/production.yml

+ MyWeb-App/lib/MyWeb

+ MyWeb-App/lib/MyWeb/App.pm

+ MyWeb-App/public

+ MyWeb-App/public/dispatch.cgi

+ MyWeb-App/public/dispatch.fcgi

+ MyWeb-App/public/404.html

+ MyWeb-App/public/500.html

+ MyWeb-App/public/favicon.ico

+ MyWeb-App/public/css

+ MyWeb-App/public/css/error.css

+ MyWeb-App/public/css/style.css

+ MyWeb-App/public/images

+ MyWeb-App/public/images/perldancer-bg.jpg

+ MyWeb-App/public/images/perldancer.jpg

+ MyWeb-App/public/javascripts

+ MyWeb-App/public/javascripts/jquery.js

+ MyWeb-App/t

+ MyWeb-App/t/001_base.t

+ MyWeb-App/t/002_index_route.t

+ MyWeb-App/views

+ MyWeb-App/views/index.tt

+ MyWeb-App/views/layouts

+ MyWeb-App/views/layouts/main.tt


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:


use Dancer2;

# 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:


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 JSON more, the config 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 prefer Apache, your file may contain the following code:


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>


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.

TechBlog!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *