SlideShare a Scribd company logo
Dancing Tutorial

Alberto Manuel Brand˜o Sim˜es
                    a     o
        ambs@perl.pt


      Braga Geek Nights
          March 1st




    Alberto Sim˜es
               o     Dancing Tutorial
Part 0: Ballet




                 Alberto Sim˜es
                            o     Dancing Tutorial
What’s Dancer?
A micro framework
for writing web applications
So, Yet Another Web Framework?
Well, yes!
CGI.pm webapps are spaghetti
Catalyst is HUGE
Ruby introduced something different
Dancing Tutorial
We needed something similar in Perl
Definitely
And they also imitated us!
Available at
http://perldancer.org
     and in GitHub.
Part 1: Tango




                Alberto Sim˜es
                           o     Dancing Tutorial
Installing



  §                                                        ¤
       $ sudo cpan Dancer
  ¦                                                        ¥
  or
  §                                                        ¤
       $ sudo cpan Task : : Dancer
  ¦                                                        ¥
  or yet
  §                                                        ¤
       $ cpanm −S Task : : Dancer
  ¦                                                        ¥




                       Alberto Sim˜es
                                  o     Dancing Tutorial
Bootstrap application
 §                                                           ¤
      $   dancer −a MyApp
      +   MyApp / bin / app . pl
      +   MyApp / config . yml
      +   MyApp / environments / development . yml
      +   MyApp / environments / production . yml
      +   MyApp / views / index . tt
      +   MyApp / views / layouts / main . tt
      +   MyApp / lib / MyApp . pm
      +   MyApp / public / javascripts / jquery . js
      +   MyApp / public / css / style . css
      +   MyApp / public / css / error . css
      +   MyApp / public / images / . . .
      +   MyApp / public / 5 0 0 . html
      +   MyApp / public / 4 0 4 . html
      +   MyApp / public / dispatch . fcgi
      +   MyApp / public / dispatch . cgi
      +   MyApp / Makefile . PL
      +   MyApp /t /002 _index_route . t
      +   MyApp /t /001 _base . t
 ¦                                                           ¥
                         Alberto Sim˜es
                                    o     Dancing Tutorial
What’s inside

 §                                                              ¤
      + MyApp / bin / app . pl
 ¦                                                              ¥

      a standalone light server (starts server in port 3000);
      also used for fast-cgi or common cgi backends;

 §                                                              ¤
      + MyApp / config . yml
      + MyApp / environments / development . yml
      + MyApp / environments / production . yml
 ¦                                                              ¥

      main configuration file (plugins, modules, etc);
      configuration files for production and development:
           defines what to report, where to report, etc.



                         Alberto Sim˜es
                                    o     Dancing Tutorial
What’s inside

 §                                                              ¤
      + MyApp / bin / app . pl
 ¦                                                              ¥

      a standalone light server (starts server in port 3000);
      also used for fast-cgi or common cgi backends;

 §                                                              ¤
      + MyApp / config . yml
      + MyApp / environments / development . yml
      + MyApp / environments / production . yml
 ¦                                                              ¥

      main configuration file (plugins, modules, etc);
      configuration files for production and development:
           defines what to report, where to report, etc.



                         Alberto Sim˜es
                                    o     Dancing Tutorial
What’s inside
 §                                                              ¤
      + MyApp / views / index . tt
      + MyApp / views / layouts / main . tt
 ¦                                                              ¥
      Templates and layouts:
            templates are page portions/scraps;
            layouts are full page designs
            (they are automagically filled by the template);
 §                                                              ¤
      +   MyApp / public / javascripts / jquery . js
      +   MyApp / public / css / style . css
      +   MyApp / public / css / error . css
      +   MyApp / public / images / . . .
 ¦                                                              ¥

      public/static files:
            javascript (Dancer ships with jsquery);
            cascade style sheets;
            images (for default design);

                            Alberto Sim˜es
                                       o     Dancing Tutorial
What’s inside
 §                                                              ¤
      + MyApp / views / index . tt
      + MyApp / views / layouts / main . tt
 ¦                                                              ¥
      Templates and layouts:
            templates are page portions/scraps;
            layouts are full page designs
            (they are automagically filled by the template);
 §                                                              ¤
      +   MyApp / public / javascripts / jquery . js
      +   MyApp / public / css / style . css
      +   MyApp / public / css / error . css
      +   MyApp / public / images / . . .
 ¦                                                              ¥

      public/static files:
            javascript (Dancer ships with jsquery);
            cascade style sheets;
            images (for default design);

                            Alberto Sim˜es
                                       o     Dancing Tutorial
What’s inside

 §                                                            ¤
      + MyApp / public / 5 0 0 . html
      + MyApp / public / 4 0 4 . html
 ¦                                                            ¥

      pages for 500 and 404 errors;


 §                                                            ¤
      + MyApp / public / dispatch . fcgi
      + MyApp / public / dispatch . cgi
 ¦                                                            ¥
      wrappers to configure fast-cgi and cgi backends;
           will be back on this later (future talk?);




                          Alberto Sim˜es
                                     o     Dancing Tutorial
What’s inside

 §                                                            ¤
      + MyApp / public / 5 0 0 . html
      + MyApp / public / 4 0 4 . html
 ¦                                                            ¥

      pages for 500 and 404 errors;


 §                                                            ¤
      + MyApp / public / dispatch . fcgi
      + MyApp / public / dispatch . cgi
 ¦                                                            ¥
      wrappers to configure fast-cgi and cgi backends;
           will be back on this later (future talk?);




                          Alberto Sim˜es
                                     o     Dancing Tutorial
What’s inside

 §                                                            ¤
      + MyApp / Makefile . PL
      + MyApp /t /002 _index_route . t
      + MyApp /t /001 _base . t
 ¦                                                            ¥
      Main module Makefile:
           usefull to make module of your application
      Your test suite;


 §                                                            ¤
      + MyApp / lib / MyApp . pm
 ¦                                                            ¥

      Your application!



                          Alberto Sim˜es
                                     o     Dancing Tutorial
What’s inside

 §                                                            ¤
      + MyApp / Makefile . PL
      + MyApp /t /002 _index_route . t
      + MyApp /t /001 _base . t
 ¦                                                            ¥
      Main module Makefile:
           usefull to make module of your application
      Your test suite;


 §                                                            ¤
      + MyApp / lib / MyApp . pm
 ¦                                                            ¥

      Your application!



                          Alberto Sim˜es
                                     o     Dancing Tutorial
Part 2: Flamenco




                   Alberto Sim˜es
                              o     Dancing Tutorial
Traffic Control



  Your MyApp/lib/MyApp.pm includes:
 §                                                                 ¤
  p a c k a g e MyApp ;
  u s e Dancer ’ : s y n t a x ’ ;

  our $VERSION = ’ 0 . 1 ’ ;

  get ’ / ’ => sub {
      template ’ i n d e x ’ ;
  };

  true ;
 ¦                                                                 ¥




                               Alberto Sim˜es
                                          o     Dancing Tutorial
How to test it?

  Start the standalone server,
 §                                                                                                  ¤
  [ ambs@rachmaninoff MyApp ] $ bin / app . pl
 ¦                                                                                                  ¥



  that shows debug info,
 §                                                                                                  ¤
  [ 9 3 8 7 ] core @0 .000017 > loading Dancer : : Handler : : Standalone
  handler in / opt / lib / perl5 / site_perl / 5 . 1 2 . 3 / Dancer / Handler . pm l . 39
  [ 9 3 8 7 ] core @0 .000442 > loading handler ’ Dancer : : Handler : :
  Standalone ’ in / opt / l o c a l / lib / perl5 / site_perl / 5 . 1 2 . 3 / Dancer . pm l . 230
  >> Dancer 1 . 3 0 1 1 server 9387 listening on http : / / 0 . 0 . 0 . 0 : 3 0 0 0
   subsection { Entering the development dance floor . . . }
 ¦                                                                                                  ¥



  and open a browser in the specified port.


                                       Alberto Sim˜es
                                                  o         Dancing Tutorial
It Works!
Route Handlers


     A Dancer app is a collection of route handlers;
     A route handler is, basically, a sub;
     It is bound to an http method;
     And to a path or a path pattern;

  Example

        get ’ / ’ => sub { . . .            };

        post ’ / s u b m i t / : f i l e ’ => sub { . . .            };

        del ’ / r e s o u r c e /∗ ’ => sub { . . .             };




                           Alberto Sim˜es
                                      o          Dancing Tutorial
Route Handlers


     A Dancer app is a collection of route handlers;
     A route handler is, basically, a sub;
     It is bound to an http method;
     And to a path or a path pattern;

  Example

        get ’ / ’ => sub { . . .            };

        post ’ / s u b m i t / : f i l e ’ => sub { . . .            };

        del ’ / r e s o u r c e /∗ ’ => sub { . . .             };




                           Alberto Sim˜es
                                      o          Dancing Tutorial
Route Handlers

     Static patterns (paths):
     §                                                                     ¤
          get ’ / ’ => sub { . . .        };

          get ’ / a b o u t ’ => sub { . . .       };
     ¦                                                                     ¥
     Patterns with named tokens:
     §                                                                     ¤
          get ’ / book / : i d ’ => sub {
             # do s o m e t h i n g w i t h params −>{i d }
             ...
          };

          get ’ / u s e r / : g r o u p / : u i d ’ => sub {
             # u s e params −>{u i d } and params −>{g r o u p } h e r e
          };
     ¦                                                                     ¥



                         Alberto Sim˜es
                                    o     Dancing Tutorial
Route Handlers

     Static patterns (paths):
     §                                                                     ¤
          get ’ / ’ => sub { . . .        };

          get ’ / a b o u t ’ => sub { . . .       };
     ¦                                                                     ¥
     Patterns with named tokens:
     §                                                                     ¤
          get ’ / book / : i d ’ => sub {
             # do s o m e t h i n g w i t h params −>{i d }
             ...
          };

          get ’ / u s e r / : g r o u p / : u i d ’ => sub {
             # u s e params −>{u i d } and params −>{g r o u p } h e r e
          };
     ¦                                                                     ¥



                         Alberto Sim˜es
                                    o     Dancing Tutorial
Route Handlers

     Patterns with anonymous tokens:
     §                                                     ¤
         get ’ / f i l e / ∗ . ∗ ’ => sub {
           my ( $file , $ext ) = splat ;
           ...
         }

          get ’ / show /∗/∗ ’ => sub {
             my ( $cat , $subcat ) = splat ;
          };
     ¦                                                     ¥
     Regular expressions:
     §                                                     ¤
         get qr { post / (  d+)−(d+)−(d+)} => sub {
            my ( $year , $month , $day ) = splat ;
         }
     ¦                                                     ¥



                       Alberto Sim˜es
                                  o     Dancing Tutorial
Route Handlers

     Patterns with anonymous tokens:
     §                                                     ¤
         get ’ / f i l e / ∗ . ∗ ’ => sub {
           my ( $file , $ext ) = splat ;
           ...
         }

          get ’ / show /∗/∗ ’ => sub {
             my ( $cat , $subcat ) = splat ;
          };
     ¦                                                     ¥
     Regular expressions:
     §                                                     ¤
         get qr { post / (  d+)−(d+)−(d+)} => sub {
            my ( $year , $month , $day ) = splat ;
         }
     ¦                                                     ¥



                       Alberto Sim˜es
                                  o     Dancing Tutorial
Part 3: Mambo




                Alberto Sim˜es
                           o     Dancing Tutorial
Templating



  Dancer has plugins for most templating systems:
      Mason, Template Toolkit and others.
      Default is a Simple template system, built-in Dancer.

  Use the template inside the route:
 §                                                                  ¤
      get ’ / u s e r / : name ’ => sub {
         template ’ p r o f i l e ’ => { username => params−>{name } } ;
      };
 ¦                                                                  ¥




                        Alberto Sim˜es
                                   o     Dancing Tutorial
Templating



  Dancer has plugins for most templating systems:
      Mason, Template Toolkit and others.
      Default is a Simple template system, built-in Dancer.

  Use the template inside the route:
 §                                                                  ¤
      get ’ / u s e r / : name ’ => sub {
         template ’ p r o f i l e ’ => { username => params−>{name } } ;
      };
 ¦                                                                  ¥




                        Alberto Sim˜es
                                   o     Dancing Tutorial
Rerouting


  You can reroute by:

  Passing the control to the next matching handler:
 §                                                               ¤
      get ’ / l a z y ’ => sub {
         pass and r e t u r n false ;
      };
 ¦                                                               ¥

  Redirecting to other URI:
 §                                                               ¤
      get ’ / f o r b i d d e n ’ => sub {
         r e t u r n redirect ’ / b e t t e r / p l a c e ’
      };
 ¦                                                               ¥




                             Alberto Sim˜es
                                        o     Dancing Tutorial
Rerouting


  You can reroute by:

  Passing the control to the next matching handler:
 §                                                               ¤
      get ’ / l a z y ’ => sub {
         pass and r e t u r n false ;
      };
 ¦                                                               ¥

  Redirecting to other URI:
 §                                                               ¤
      get ’ / f o r b i d d e n ’ => sub {
         r e t u r n redirect ’ / b e t t e r / p l a c e ’
      };
 ¦                                                               ¥




                             Alberto Sim˜es
                                        o     Dancing Tutorial
Rerouting


  You can reroute by:

  Passing the control to the next matching handler:
 §                                                               ¤
      get ’ / l a z y ’ => sub {
         pass and r e t u r n false ;
      };
 ¦                                                               ¥

  Redirecting to other URI:
 §                                                               ¤
      get ’ / f o r b i d d e n ’ => sub {
         r e t u r n redirect ’ / b e t t e r / p l a c e ’
      };
 ¦                                                               ¥




                             Alberto Sim˜es
                                        o     Dancing Tutorial
Serving Files

  You can serve a static file:
 §                                                              ¤
       get ’ / dowload / : f i l e ’ => sub {
         my $file = params−>{file } ;

            pass and r e t u r n false u n l e s s −f $file ;

            send_file $file ;
       };
 ¦                                                              ¥

  If the content is generated, just change content-type:
 §                                                              ¤
       get ’ / readme . t x t ’ => sub {
          content_type ’ t e x t / p l a i n ’ ;
          return ’ this is plain text ’
       };
 ¦                                                              ¥


                            Alberto Sim˜es
                                       o     Dancing Tutorial
Serving Files

  You can serve a static file:
 §                                                              ¤
       get ’ / dowload / : f i l e ’ => sub {
         my $file = params−>{file } ;

            pass and r e t u r n false u n l e s s −f $file ;

            send_file $file ;
       };
 ¦                                                              ¥

  If the content is generated, just change content-type:
 §                                                              ¤
       get ’ / readme . t x t ’ => sub {
          content_type ’ t e x t / p l a i n ’ ;
          return ’ this is plain text ’
       };
 ¦                                                              ¥


                            Alberto Sim˜es
                                       o     Dancing Tutorial
Thanks to Alexis Sukrieh
  (I stole some slides)

More Related Content

ODP
Perl Dancer, FPW 2010
PDF
Perl Dancer for Python programmers
ODP
Writing webapps with Perl Dancer
PDF
PerlDancer for Perlers (FOSDEM 2011)
ODP
An introduction to Rex - FLOSS UK DevOps York 2015
ODP
Dancer's Ecosystem
KEY
Keeping it small: Getting to know the Slim micro framework
KEY
Quality Use Of Plugin
Perl Dancer, FPW 2010
Perl Dancer for Python programmers
Writing webapps with Perl Dancer
PerlDancer for Perlers (FOSDEM 2011)
An introduction to Rex - FLOSS UK DevOps York 2015
Dancer's Ecosystem
Keeping it small: Getting to know the Slim micro framework
Quality Use Of Plugin

What's hot (20)

KEY
PDF
Keeping it Small: Getting to know the Slim Micro Framework
PDF
Keeping it small - Getting to know the Slim PHP micro framework
PDF
RESTful web services
PDF
Your own (little) gem: building an online business with Ruby
PDF
Intro to Rails
PDF
Modern Perl Web Development with Dancer
PDF
Mojolicious
PDF
JSON and the APInauts
PDF
Task 1
PDF
Magical WordPress Development with Vagrant
PDF
Bullet: The Functional PHP Micro-Framework
KEY
Silex, the microframework
PDF
PDF
I motion
PPT
CoffeeScript: A beginner's presentation for beginners copy
PDF
Interface de Voz con Rails
ODP
Modern Perl
PDF
PerlでWeb API入門
PDF
Hubot: a look inside our robot friend
Keeping it Small: Getting to know the Slim Micro Framework
Keeping it small - Getting to know the Slim PHP micro framework
RESTful web services
Your own (little) gem: building an online business with Ruby
Intro to Rails
Modern Perl Web Development with Dancer
Mojolicious
JSON and the APInauts
Task 1
Magical WordPress Development with Vagrant
Bullet: The Functional PHP Micro-Framework
Silex, the microframework
I motion
CoffeeScript: A beginner's presentation for beginners copy
Interface de Voz con Rails
Modern Perl
PerlでWeb API入門
Hubot: a look inside our robot friend
Ad

Similar to Dancing Tutorial (20)

ODP
Perl dancer
PDF
Zend Framework MVC driven ExtJS
PDF
Melody Designer Training
PDF
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
PDF
<img src="../i/r_14.png" />
PDF
New ways to present your images
PPTX
Prairie Dev Con West - 2012-03-14 - Webmatrix, see what the matrix can do fo...
PDF
Dreamweaver reference
PPTX
They why behind php frameworks
PDF
Cake php cookbook
PDF
Curso rails
PDF
Flask docs
PDF
Web Developing In Search
PDF
Drupal Front End PHP
PDF
Build Your Own Website A Comic Guide to HTML, CSS and Wordpress (Nate Cooper)...
PDF
TEST UPLOAD
PDF
Crash Course HTML/Rails Slides
PDF
Organizing Your PHP Projects (2010 ConFoo)
PDF
Learning PHP for Drupal Theming, DC Chicago 2009
PDF
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
Perl dancer
Zend Framework MVC driven ExtJS
Melody Designer Training
symfony: An Open-Source Framework for Professionals (Dutch Php Conference 2008)
<img src="../i/r_14.png" />
New ways to present your images
Prairie Dev Con West - 2012-03-14 - Webmatrix, see what the matrix can do fo...
Dreamweaver reference
They why behind php frameworks
Cake php cookbook
Curso rails
Flask docs
Web Developing In Search
Drupal Front End PHP
Build Your Own Website A Comic Guide to HTML, CSS and Wordpress (Nate Cooper)...
TEST UPLOAD
Crash Course HTML/Rails Slides
Organizing Your PHP Projects (2010 ConFoo)
Learning PHP for Drupal Theming, DC Chicago 2009
DevTeach Ottawa - Webmatrix, see what the matrix can do for you!!
Ad

More from Alberto Simões (20)

PDF
Source Code Quality
PDF
Language Identification: A neural network approach
PDF
Google Maps JS API
PPTX
Making the most of a 100-year-old dictionary
PDF
Dictionary Alignment by Rewrite-based Entry Translation
PDF
EMLex-A5: Specialized Dictionaries
PDF
Modelação de Dados
PDF
Aula 04 - Introdução aos Diagramas de Sequência
PDF
Aula 03 - Introdução aos Diagramas de Atividade
PDF
Aula 02 - Engenharia de Requisitos
PDF
Aula 01 - Planeamento de Sistemas de Informação
PDF
Building C and C++ libraries with Perl
PDF
PLN em Perl
PDF
Classification Systems
PDF
Redes de Pert
PDF
Processing XML: a rewriting system approach
PDF
Sistemas de Numeração
PDF
Álgebra de Boole
PDF
Arquitecturas de Tradução Automática
PDF
Extracção de Recursos para Tradução Automática
Source Code Quality
Language Identification: A neural network approach
Google Maps JS API
Making the most of a 100-year-old dictionary
Dictionary Alignment by Rewrite-based Entry Translation
EMLex-A5: Specialized Dictionaries
Modelação de Dados
Aula 04 - Introdução aos Diagramas de Sequência
Aula 03 - Introdução aos Diagramas de Atividade
Aula 02 - Engenharia de Requisitos
Aula 01 - Planeamento de Sistemas de Informação
Building C and C++ libraries with Perl
PLN em Perl
Classification Systems
Redes de Pert
Processing XML: a rewriting system approach
Sistemas de Numeração
Álgebra de Boole
Arquitecturas de Tradução Automática
Extracção de Recursos para Tradução Automática

Recently uploaded (20)

PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Spectroscopy.pptx food analysis technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
sap open course for s4hana steps from ECC to s4
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectroscopy.pptx food analysis technology
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
Understanding_Digital_Forensics_Presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Chapter 3 Spatial Domain Image Processing.pdf
sap open course for s4hana steps from ECC to s4

Dancing Tutorial

  • 1. Dancing Tutorial Alberto Manuel Brand˜o Sim˜es a o ambs@perl.pt Braga Geek Nights March 1st Alberto Sim˜es o Dancing Tutorial
  • 2. Part 0: Ballet Alberto Sim˜es o Dancing Tutorial
  • 4. A micro framework for writing web applications
  • 5. So, Yet Another Web Framework?
  • 7. CGI.pm webapps are spaghetti
  • 11. We needed something similar in Perl
  • 13. And they also imitated us!
  • 15. Part 1: Tango Alberto Sim˜es o Dancing Tutorial
  • 16. Installing § ¤ $ sudo cpan Dancer ¦ ¥ or § ¤ $ sudo cpan Task : : Dancer ¦ ¥ or yet § ¤ $ cpanm −S Task : : Dancer ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 17. Bootstrap application § ¤ $ dancer −a MyApp + MyApp / bin / app . pl + MyApp / config . yml + MyApp / environments / development . yml + MyApp / environments / production . yml + MyApp / views / index . tt + MyApp / views / layouts / main . tt + MyApp / lib / MyApp . pm + MyApp / public / javascripts / jquery . js + MyApp / public / css / style . css + MyApp / public / css / error . css + MyApp / public / images / . . . + MyApp / public / 5 0 0 . html + MyApp / public / 4 0 4 . html + MyApp / public / dispatch . fcgi + MyApp / public / dispatch . cgi + MyApp / Makefile . PL + MyApp /t /002 _index_route . t + MyApp /t /001 _base . t ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 18. What’s inside § ¤ + MyApp / bin / app . pl ¦ ¥ a standalone light server (starts server in port 3000); also used for fast-cgi or common cgi backends; § ¤ + MyApp / config . yml + MyApp / environments / development . yml + MyApp / environments / production . yml ¦ ¥ main configuration file (plugins, modules, etc); configuration files for production and development: defines what to report, where to report, etc. Alberto Sim˜es o Dancing Tutorial
  • 19. What’s inside § ¤ + MyApp / bin / app . pl ¦ ¥ a standalone light server (starts server in port 3000); also used for fast-cgi or common cgi backends; § ¤ + MyApp / config . yml + MyApp / environments / development . yml + MyApp / environments / production . yml ¦ ¥ main configuration file (plugins, modules, etc); configuration files for production and development: defines what to report, where to report, etc. Alberto Sim˜es o Dancing Tutorial
  • 20. What’s inside § ¤ + MyApp / views / index . tt + MyApp / views / layouts / main . tt ¦ ¥ Templates and layouts: templates are page portions/scraps; layouts are full page designs (they are automagically filled by the template); § ¤ + MyApp / public / javascripts / jquery . js + MyApp / public / css / style . css + MyApp / public / css / error . css + MyApp / public / images / . . . ¦ ¥ public/static files: javascript (Dancer ships with jsquery); cascade style sheets; images (for default design); Alberto Sim˜es o Dancing Tutorial
  • 21. What’s inside § ¤ + MyApp / views / index . tt + MyApp / views / layouts / main . tt ¦ ¥ Templates and layouts: templates are page portions/scraps; layouts are full page designs (they are automagically filled by the template); § ¤ + MyApp / public / javascripts / jquery . js + MyApp / public / css / style . css + MyApp / public / css / error . css + MyApp / public / images / . . . ¦ ¥ public/static files: javascript (Dancer ships with jsquery); cascade style sheets; images (for default design); Alberto Sim˜es o Dancing Tutorial
  • 22. What’s inside § ¤ + MyApp / public / 5 0 0 . html + MyApp / public / 4 0 4 . html ¦ ¥ pages for 500 and 404 errors; § ¤ + MyApp / public / dispatch . fcgi + MyApp / public / dispatch . cgi ¦ ¥ wrappers to configure fast-cgi and cgi backends; will be back on this later (future talk?); Alberto Sim˜es o Dancing Tutorial
  • 23. What’s inside § ¤ + MyApp / public / 5 0 0 . html + MyApp / public / 4 0 4 . html ¦ ¥ pages for 500 and 404 errors; § ¤ + MyApp / public / dispatch . fcgi + MyApp / public / dispatch . cgi ¦ ¥ wrappers to configure fast-cgi and cgi backends; will be back on this later (future talk?); Alberto Sim˜es o Dancing Tutorial
  • 24. What’s inside § ¤ + MyApp / Makefile . PL + MyApp /t /002 _index_route . t + MyApp /t /001 _base . t ¦ ¥ Main module Makefile: usefull to make module of your application Your test suite; § ¤ + MyApp / lib / MyApp . pm ¦ ¥ Your application! Alberto Sim˜es o Dancing Tutorial
  • 25. What’s inside § ¤ + MyApp / Makefile . PL + MyApp /t /002 _index_route . t + MyApp /t /001 _base . t ¦ ¥ Main module Makefile: usefull to make module of your application Your test suite; § ¤ + MyApp / lib / MyApp . pm ¦ ¥ Your application! Alberto Sim˜es o Dancing Tutorial
  • 26. Part 2: Flamenco Alberto Sim˜es o Dancing Tutorial
  • 27. Traffic Control Your MyApp/lib/MyApp.pm includes: § ¤ p a c k a g e MyApp ; u s e Dancer ’ : s y n t a x ’ ; our $VERSION = ’ 0 . 1 ’ ; get ’ / ’ => sub { template ’ i n d e x ’ ; }; true ; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 28. How to test it? Start the standalone server, § ¤ [ ambs@rachmaninoff MyApp ] $ bin / app . pl ¦ ¥ that shows debug info, § ¤ [ 9 3 8 7 ] core @0 .000017 > loading Dancer : : Handler : : Standalone handler in / opt / lib / perl5 / site_perl / 5 . 1 2 . 3 / Dancer / Handler . pm l . 39 [ 9 3 8 7 ] core @0 .000442 > loading handler ’ Dancer : : Handler : : Standalone ’ in / opt / l o c a l / lib / perl5 / site_perl / 5 . 1 2 . 3 / Dancer . pm l . 230 >> Dancer 1 . 3 0 1 1 server 9387 listening on http : / / 0 . 0 . 0 . 0 : 3 0 0 0 subsection { Entering the development dance floor . . . } ¦ ¥ and open a browser in the specified port. Alberto Sim˜es o Dancing Tutorial
  • 30. Route Handlers A Dancer app is a collection of route handlers; A route handler is, basically, a sub; It is bound to an http method; And to a path or a path pattern; Example get ’ / ’ => sub { . . . }; post ’ / s u b m i t / : f i l e ’ => sub { . . . }; del ’ / r e s o u r c e /∗ ’ => sub { . . . }; Alberto Sim˜es o Dancing Tutorial
  • 31. Route Handlers A Dancer app is a collection of route handlers; A route handler is, basically, a sub; It is bound to an http method; And to a path or a path pattern; Example get ’ / ’ => sub { . . . }; post ’ / s u b m i t / : f i l e ’ => sub { . . . }; del ’ / r e s o u r c e /∗ ’ => sub { . . . }; Alberto Sim˜es o Dancing Tutorial
  • 32. Route Handlers Static patterns (paths): § ¤ get ’ / ’ => sub { . . . }; get ’ / a b o u t ’ => sub { . . . }; ¦ ¥ Patterns with named tokens: § ¤ get ’ / book / : i d ’ => sub { # do s o m e t h i n g w i t h params −>{i d } ... }; get ’ / u s e r / : g r o u p / : u i d ’ => sub { # u s e params −>{u i d } and params −>{g r o u p } h e r e }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 33. Route Handlers Static patterns (paths): § ¤ get ’ / ’ => sub { . . . }; get ’ / a b o u t ’ => sub { . . . }; ¦ ¥ Patterns with named tokens: § ¤ get ’ / book / : i d ’ => sub { # do s o m e t h i n g w i t h params −>{i d } ... }; get ’ / u s e r / : g r o u p / : u i d ’ => sub { # u s e params −>{u i d } and params −>{g r o u p } h e r e }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 34. Route Handlers Patterns with anonymous tokens: § ¤ get ’ / f i l e / ∗ . ∗ ’ => sub { my ( $file , $ext ) = splat ; ... } get ’ / show /∗/∗ ’ => sub { my ( $cat , $subcat ) = splat ; }; ¦ ¥ Regular expressions: § ¤ get qr { post / ( d+)−(d+)−(d+)} => sub { my ( $year , $month , $day ) = splat ; } ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 35. Route Handlers Patterns with anonymous tokens: § ¤ get ’ / f i l e / ∗ . ∗ ’ => sub { my ( $file , $ext ) = splat ; ... } get ’ / show /∗/∗ ’ => sub { my ( $cat , $subcat ) = splat ; }; ¦ ¥ Regular expressions: § ¤ get qr { post / ( d+)−(d+)−(d+)} => sub { my ( $year , $month , $day ) = splat ; } ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 36. Part 3: Mambo Alberto Sim˜es o Dancing Tutorial
  • 37. Templating Dancer has plugins for most templating systems: Mason, Template Toolkit and others. Default is a Simple template system, built-in Dancer. Use the template inside the route: § ¤ get ’ / u s e r / : name ’ => sub { template ’ p r o f i l e ’ => { username => params−>{name } } ; }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 38. Templating Dancer has plugins for most templating systems: Mason, Template Toolkit and others. Default is a Simple template system, built-in Dancer. Use the template inside the route: § ¤ get ’ / u s e r / : name ’ => sub { template ’ p r o f i l e ’ => { username => params−>{name } } ; }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 39. Rerouting You can reroute by: Passing the control to the next matching handler: § ¤ get ’ / l a z y ’ => sub { pass and r e t u r n false ; }; ¦ ¥ Redirecting to other URI: § ¤ get ’ / f o r b i d d e n ’ => sub { r e t u r n redirect ’ / b e t t e r / p l a c e ’ }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 40. Rerouting You can reroute by: Passing the control to the next matching handler: § ¤ get ’ / l a z y ’ => sub { pass and r e t u r n false ; }; ¦ ¥ Redirecting to other URI: § ¤ get ’ / f o r b i d d e n ’ => sub { r e t u r n redirect ’ / b e t t e r / p l a c e ’ }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 41. Rerouting You can reroute by: Passing the control to the next matching handler: § ¤ get ’ / l a z y ’ => sub { pass and r e t u r n false ; }; ¦ ¥ Redirecting to other URI: § ¤ get ’ / f o r b i d d e n ’ => sub { r e t u r n redirect ’ / b e t t e r / p l a c e ’ }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 42. Serving Files You can serve a static file: § ¤ get ’ / dowload / : f i l e ’ => sub { my $file = params−>{file } ; pass and r e t u r n false u n l e s s −f $file ; send_file $file ; }; ¦ ¥ If the content is generated, just change content-type: § ¤ get ’ / readme . t x t ’ => sub { content_type ’ t e x t / p l a i n ’ ; return ’ this is plain text ’ }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 43. Serving Files You can serve a static file: § ¤ get ’ / dowload / : f i l e ’ => sub { my $file = params−>{file } ; pass and r e t u r n false u n l e s s −f $file ; send_file $file ; }; ¦ ¥ If the content is generated, just change content-type: § ¤ get ’ / readme . t x t ’ => sub { content_type ’ t e x t / p l a i n ’ ; return ’ this is plain text ’ }; ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  • 44. Thanks to Alexis Sukrieh (I stole some slides)