Main Content

Perl Dancer - a Perl Framework - Installation and first test

Archive - Originally posted on "The Horse's Mouth" - 2013-05-23 09:48:37 - Graham Ellis

The most commonly requested Web Frameworks for our training courses are CodeIgniter and Zend (using PHP), Django (using Python) and Rails (using Ruby). However, Perl is also an excellent language for web use - in fact it was the original predominant web language prior to the growth of those other languages. For large projects, Perl 5 may no longer be the optimum language to use... but for smaller and medium size it's still a good choice - especially where the web use of code is ancillary to substantial data munging. But should you use a Framework in Perl, and if so, which one?

To answer the first question - if you want templates, sessions, URL routing in your application... if you want to separate out the look and feel from the computational work... then, yes, you should consider using a framework. Someone else has done the hard work to implement those features shared between many applications, and you'll be more efficient in your code development if you make use of that hard work rather than repeating it. Further savings come later on, when extra elements can be easily added, and upgrades to the framework can be reflected in upgrades to the application - in both features and security.

So - which framework for Perl applications? Over the last couple of days, I've taken a look at Perl Dancer - at http://www.perldancer.org/.

From the Dance web site:

What is Dancer?

Dancer is a simple but powerful web application framework for Perl

Key features

Dead 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


Yes - it is simple. But much of the documentation is written for people who are already Perl and web experts, and for newcomers or even intermediates, there's a feeling of "help, how do I do this", "can I have some more examples", and "why do I need so many extra CPAN modules" in spite of what the headlines say.

So let's take a look at...
(a) installation
(b) first application
(c) scaling up the web server
for the newcomer

• installation

First, get the software...

Download, unpack... we used Dancer-1.3113
cd to directory created

You then need to build the software, but:

  munchkin:Dancer-1.3113 grahamellis$ perl Makefile.PL
  Checking if your kit is complete...
  Looks good
  Warning: prerequisite HTTP::Body 1.07 not found.
  Warning: prerequisite HTTP::Server::Simple::PSGI 0.11 not found.
  Warning: prerequisite MIME::Types 0 not found.
  Warning: prerequisite Module::Runtime 0 not found.
  Warning: prerequisite URI 1.59 not found. We have 1.56.
  Writing Makefile for Dancer
  munchkin:Dancer-1.3113 grahamellis


That's a series of packages that aren't in the standard Perl disrtibution and need to be sourced from the CPAN... so as adminsitrator / root

install prerequisites:

  munchkin:Dancer-1.3113 grahamellis$ perl -MCPAN -e shell
  cpan[1]> install HTTP::Body
  [lots of output]
  etc for all the other prerequistites, includind URI
  cpan[7]> quit


And you can then work out how to build... and build... and install

  munchkin:Dancer-1.3113 grahamellis$ perl Makefile.PL
  Writing Makefile for Dancer
  munchkin:Dancer-1.3113 grahamellis$


  munchkin:Dancer-1.3113 grahamellis$ make
  cp lib/Dancer/Session/Abstract.pm blib/lib/Dancer/Session/Abstract.pm
  cp lib/Dancer/SharedData.pm blib/lib/Dancer/SharedData.pm
  cp lib/Dancer/Serializer/Mutable.pm blib/lib/Dancer/Serializer/Mutable.pm
  cp lib/Dancer/Route.pm blib/lib/Dancer/Route.pm
  etc


  munchkin:Dancer-1.3113 grahamellis$ sudo make install
  Password:
  Installing /Library/Perl/5.12/Dancer.pm
  Installing /Library/Perl/5.12/Dancer/App.pm


• first application

Having installed Dancer, we can now generate our first application and try it out. The application generation script makes a default "hello" application - we'll do that to test our installation, then we'll tailor it to do what we need in a next lesson.

So:

  munchkin:Dancer-1.3113 grahamellis$ ./script/dancer -a MyWeb::App
  + MyWeb-App
  + MyWeb-App/bin
  + MyWeb-App/bin/app.pl
  + MyWeb-App/config.yml
  + MyWeb-App/environments
  etc
  *****
  WARNING: YAML.pm is not installed. This is not a full dependency, but is highly
  recommended; in particular, the scaffolded Dancer app being created will not be
  able to read settings from the config file without YAML.pm being installed.
  
  To resolve this, simply install YAML from CPAN, for instance using one of the
  following commands:
  
    cpan YAML
    perl -MCPAN -e 'install YAML'
    curl -L http://cpanmin.us | perl - --sudo YAML
  *****
  munchkin:Dancer-1.3113 grahamellis$


It's going to be a good idea to install YAML:

  munchkin:Dancer-1.3113 grahamellis$ sudo perl -MCPAN -e shell
  cpan[1]> install YAML
  cpan[2]> quit
  munchkin:Dancer-1.3113 grahamellis


We can then test the application though the built in server (well - actually the server we downloaded from the CPAN!)

  munchkin:MyWeb-App grahamellis$ ./bin/app.pl
  [9296] core @0.000022> loading Dancer::Handler::Standalone handler in /Library/Perl/5.12/Dancer/Handler.pm l. 45
  [9296] core @0.000326> loading handler 'Dancer::Handler::Standalone' in /Library/Perl/5.12/Dancer.pm l. 483
  >> Dancer 1.3113 server 9296 listening on http://0.0.0.0:3000
  == Entering the development dance floor...


And we then browse to view the page:



and you'll find the following extra lines in the window that's running the server:

  [9296] core @0.000218> request: GET / from 127.0.0.1 in /Library/Perl/5.12/Dancer/Handler.pm l. 56
  [9296] core @0.000797> [hit #1]Trying to match 'GET /' against /^\/$/ (generated from '/') in /Library/Perl/5.12/Dancer/Route.pm l. 84


Next lesson to follow... tailoring the standard application to do what you want!