SlideShare a Scribd company logo
Dancer A  micro framework  for  writing web applications Alexis Sukrieh OSDCfr 2009
Alexis Sukrieh http://sukria.net http://twitter.com/sukria http://search.cpan.org/~sukria/
Dancer http://dancer.sukria.net $ sudo cpan Dancer http://github.com/sukria/Dancer
Description Syntax Application Tree Environments Deployment
webapp  framework  for Perl
Again, another one !!?
YES.
Why ?
Web Development in Perl can be :
CGI.pm : fat webapp junkfood
Catalyst : it's huge, but, it's huge.
Plenty of other frameworks alike
Ruby introduced something else
A new approach to  webapp development
Perl needed the same
definetly
Kindly stolen from Ruby
So, Dancer is a framework ...
a  micro  framework
Lightweight
Standalone
PSGI/Plack compliant
expressive syntax
few configuration
painless  for the developer
enough talking, let's dance
 
Sorry, I mean ...
package   MyApp ; use  Dancer; get  '/'  =>  sub  { 'Hello OSDCfr 2009!' }; dance;
A dancer script is a webserver
$ ./myApp.pl >> Listening on 127.0.0.1:3000 == Entering the development dance floor ...
Working webapp ready to be hacked
» happiness : he can WSFC now
Description Syntax Application Tree Environments Deployment
A Dancer app is defined with  route handlers
a route handler is basically a  sub
bound to an HTTP  method   (typically ' get ' or ' post ')
with a path  pattern
/foo /bar/:var /baz/* r('/stuff/([a-z0-9]+)/')
Static pattern (path) / /foo /bar/baz
pattern with named tokens /hello/:buddy my $name = params->{buddy}
pattern with anonymous tokens /show/*.* my ($file, $ext) = splat;
pseudo-regexp-defined patterns r('/post/([a-z0-9]+)') my ($post) = splat; # $1
processing a request = finding a  matching route handler
404 No matching route handler » no joy
Route handler found » Joy
When found,  a route handler is executed
it can alter the response headers get '/' => sub { content_type  'text/plain'; return 'this is plain text'  };
pass to the next matching handler get '/lazy' => sub { pass  and return false; };
serve a static file get '/dowload/:file' => sub { my $file = params->{file}; if (! -f $file) {  pass and return false;  } send_file $file; };
throw an error get '/forbidden' => sub { send_error 'Nope'; };
redirect to somewhere else get '/forbidden' => sub { redirect  '/better/place'  };
and of course, render a content get '/showme' => sub { template 'showme', {  var => 'foo'}; };
return value  = content to render
logging ?
get '/' => sub { # ... debug 'here it is'; }; » logs/development.log
pre-processing ?
before sub {  do_some_init_stuff(); params->{foo} = 'beenthere'; };
static file serving ?
existing file  in  $appdir/public = file served , no handler needed
Description Syntax Application Tree Environments Deployment
app.pl
app.pl views/ index.tt
app.pl views/ index.tt layouts/ main.tt
app.pl views/ index.tt layouts/ main.tt config.yml
app.pl views/ index.tt layouts/ main.tt config.yml environments/ development.yml production.yml
app.pl views/ index.tt layouts/ main.tt config.yml environments/ development.yml production.yml public/
bootstraping ?
$ dancer -a MyApp
+ MyApp + MyApp/views + MyApp/views/index.tt + MyApp/views/layouts + MyApp/views/layouts/main.tt + MyApp/environments + MyApp/environments/development.yml + MyApp/environments/production.yml + MyApp/config.yml + MyApp/app.psgi + MyApp/MyApp.pm + MyApp/MyApp.pl + MyApp/public + MyApp/public/css + MyApp/public/css/style.css + MyApp/public/css/error.css + MyApp/public/images + MyApp/public/404.html + MyApp/public/500.html
» Working webapp, pre-configured, pre-designed and waiting for your hacks
Description Syntax Application Tree Environments Deployment
settings : customizing the app
settings in the app code
use Dancer; set layout => 'application' set foo => 42; set content_type =>'text/plain';
handy at first, but dirty
YAML config files: goodness
# config.yml layout: 'application' foo: 42 content_type: 'text/plain'
config.yml = global configuration
environments/foo.yml « foo » configuration
$ dancer -e foo runs the app in « foo » environment
development.yml warnings: 1 show_errors: 1 ...
production.yml warnings: 0 show_errors: 0 ...
show_errors: 1
show_errors: 0
Description Syntax Application Tree Environments Deployment
For production needs,  use PSGI/Plack
Plack is to Perl : what Rack is to Ruby what WSGI is to Python
universal  server <-> application  gateway
PSGI/Plack miyagawa http://plackperl.org
Dancer supports Plack since 0.9904
app.psgi tells Plack how to dance
use CGI::PSGI; use MyApp; my $handler = sub { my $env = shift; my $cgi = CGI::PSGI->new($env); Dancer->dance($cgi); };
The app is now PSGI/Plack aware
$ plackup -a app.psgi &
Webservers have PSGI adapters
Apache (mod_psgi) nginx Perlbal HTTP::Server::Fast Plack (standalone)
Conf example with Apache  <Location /> SetHandler perl-script PerlHandler  Plack::Server::Apache2 PerlSetVar psgi_app /path/app.psgi </Location>
Works pretty well
enjoy web development with Perl
DANCE!

More Related Content

PDF
Perl Dancer for Python programmers
ODP
Perl Dancer, FPW 2010
PDF
PerlDancer for Perlers (FOSDEM 2011)
ODP
An introduction to Rex - FLOSS UK DevOps York 2015
PDF
Dancing Tutorial
ODP
Dancer's Ecosystem
KEY
Quality Use Of Plugin
PDF
RESTful web services
Perl Dancer for Python programmers
Perl Dancer, FPW 2010
PerlDancer for Perlers (FOSDEM 2011)
An introduction to Rex - FLOSS UK DevOps York 2015
Dancing Tutorial
Dancer's Ecosystem
Quality Use Of Plugin
RESTful web services

What's hot (20)

PDF
Lightweight Webservices with Sinatra and RestClient
PDF
Intro to Rails
PDF
JSON and the APInauts
PDF
Mojolicious
PDF
Using Sinatra to Build REST APIs in Ruby
PDF
Go初心者がGoでコマンドラインツールの作成に挑戦した話
PDF
The Puppet Debugging Kit: Building Blocks for Exploration and Problem Solving...
PDF
Magical WordPress Development with Vagrant
PDF
Web development automatisation for fun and profit (Artem Daniliants)
PDF
Your own (little) gem: building an online business with Ruby
PDF
Babushka
PDF
Ansible leveraging 2.0
PDF
V2 and beyond
PDF
Task 1
PDF
AnsibleFest 2014 - Role Tips and Tricks
PDF
PerlでWeb API入門
PPT
Writing Friendly libraries for CodeIgniter
PDF
Hubot: a look inside our robot friend
PDF
More tips n tricks
PDF
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Lightweight Webservices with Sinatra and RestClient
Intro to Rails
JSON and the APInauts
Mojolicious
Using Sinatra to Build REST APIs in Ruby
Go初心者がGoでコマンドラインツールの作成に挑戦した話
The Puppet Debugging Kit: Building Blocks for Exploration and Problem Solving...
Magical WordPress Development with Vagrant
Web development automatisation for fun and profit (Artem Daniliants)
Your own (little) gem: building an online business with Ruby
Babushka
Ansible leveraging 2.0
V2 and beyond
Task 1
AnsibleFest 2014 - Role Tips and Tricks
PerlでWeb API入門
Writing Friendly libraries for CodeIgniter
Hubot: a look inside our robot friend
More tips n tricks
Cloud meets Fog & Puppet A Story of Version Controlled Infrastructure
Ad

Similar to Writing webapps with Perl Dancer (20)

ODP
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
ODP
Summit2011 satellites-robinf-20110605
PPT
Writing Pluggable Software
ODP
Modern Perl
PPT
Optimize Site Deployments with Drush (DrupalCamp WNY 2011)
ODP
Maintaining your own branch of Drupal core
ODP
Pyramid Lighter/Faster/Better web apps
PPT
Ratpack - Classy and Compact Groovy Web Apps
PPTX
PSGI and Plack from first principles
ODP
Mojolicious on Steroids
PPT
Building your first Node app with Connect & Express
PPT
Rush, a shell that will yield to you
ODP
Extending Zend Framework
PPT
A Quick Introduction to Sinatra
PPT
A Quick Introduction to Sinatra
ODP
Multi tenant CMSes using php
PPT
Going crazy with Node.JS and CakePHP
PPTX
Think Generic - Add API's To Your Custom Modules
PPT
Render API - Pavel Makhrinsky
ODP
Evolving Software with Moose
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Summit2011 satellites-robinf-20110605
Writing Pluggable Software
Modern Perl
Optimize Site Deployments with Drush (DrupalCamp WNY 2011)
Maintaining your own branch of Drupal core
Pyramid Lighter/Faster/Better web apps
Ratpack - Classy and Compact Groovy Web Apps
PSGI and Plack from first principles
Mojolicious on Steroids
Building your first Node app with Connect & Express
Rush, a shell that will yield to you
Extending Zend Framework
A Quick Introduction to Sinatra
A Quick Introduction to Sinatra
Multi tenant CMSes using php
Going crazy with Node.JS and CakePHP
Think Generic - Add API's To Your Custom Modules
Render API - Pavel Makhrinsky
Evolving Software with Moose
Ad

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PPTX
1. Introduction to Computer Programming.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Electronic commerce courselecture one. Pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
cuic standard and advanced reporting.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Tartificialntelligence_presentation.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Teaching material agriculture food technology
1. Introduction to Computer Programming.pptx
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
Electronic commerce courselecture one. Pdf
Programs and apps: productivity, graphics, security and other tools
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
cuic standard and advanced reporting.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced methodologies resolving dimensionality complications for autism neur...
Tartificialntelligence_presentation.pptx
MIND Revenue Release Quarter 2 2025 Press Release

Writing webapps with Perl Dancer