Perl Dancer,Ever since I learned perl, i was dreaming to write a web app of my own. I was scared by the terms sessions, cookies, content rendering, SQL integration etc. But Dancer eased my way.
Thanks to Perl Dancer, i just deployed a Music Web App – “Spark Impulse” in our office.HIRE PERL DEVELOPERS INDIA
What is Dancer ?
Dancer is a simple but powerful web application framework in Perl. Dancer stole the approach from Sinatra, a web framework for Ruby. Yes, The bad artists imitate, The great artists steal !!
Why Dancer ?
- LightWeight
- StandAlone
- PSGI/Plack Compliant
- Expressive Syntax
- Few Dependencies
- Few Configuration Steps
Installation
Using cpanminus
spark$ curl -L http://cpanmin.us | perl - --sudo Dancer
Using cpan shell
spark$ perl -MCPAN -e shell
cpan shell -- CPAN exploration and modules installation (v1.9402)
Enter 'h' for help.
cpan[1]> install Dancer
Or even by hand
spark$ wget http://search.cpan.org/CPAN/authors/id/X/XS/XSAWYERX/Dancer-1.3095.tar.gz
spark$ tar -zxf Dancer-1.3095.tar.gz
spark$ cd Dancer-1.3095
spark$ perl Makefile.PL
spark$ make
spark$ make test
spark$ make install
A Dancer script itself is a webserver. Yes, you read it correctly – run the script from command prompt and your web application will spring into existance.
spark$ ./myapp.pl
>> Dancer 1.3092 server 9194 listening on http://0.0.0.0:3000
>> Dancer::Plugin::Database (1.81)
>> Dancer::Plugin::Database::Handle (0.12)
== Entering the development dance floor ...
With the following code in your myapp.pl script you say a big hello to the world at http://:3000/
#!/usr/bin/env perl
use Dancer;
get '/' => sub {
"Hello World !"
};
dance;
Dancer app is defined with route handlers. A route handler is basically a ‘sub’ ( function ) associated to an HTTP method and a path pattern. Valid HTTP methods are GET, POST, PUT, DELETE. But usually it will either be a GET or a POST !
Path Patterns includes Named Matching, WildCards Matching, Regex Matching, Conditional Matching as shown below. A route pattern can also contain one or more tokens (a word prefixed with ‘:’).
get '/hello/:name' => sub {
"Hi" . param('name') . ", Welcome here!";
}
When you access http://localhost:3000/hello/Peter, server should respond with a page – Hi Peter, Welcome here!
To make the token optional suffix it with a “?”
get '/hello/:name?' => sub {
"Hello there " . (param('name') || "whoever you are!");
};
You may use regex patterns for defining routes. And Dancer will return the matches in an arrayref, accessible via the keyword ‘splat’.
get '/download/*.*' => sub {
my ($file, $ext) = splat;
# do something with $file.$ext here
};
get qr{/hello/([\w]+)} => sub {
my ($name) = splat;
return "Hello $name";
}
So processing a HTTP request is equal to finding a matching route handler. When a request matches with a route handler, dancer executes that route handler.
Enough to start with !
I’m planning to add more blogs under the same topic. Till then, make some simple moves and start dancing !!
Reference: wikipedia