Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Dancing Tutorial

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


      Braga Geek Nights...
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
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
  ¦                  ...
Bootstrap application
 §                                                           ¤
      $   dancer −a MyApp
      +   M...
What’s inside

 §                                                              ¤
      + MyApp / bin / app . pl
 ¦        ...
What’s inside

 §                                                              ¤
      + MyApp / bin / app . pl
 ¦        ...
What’s inside
 §                                                              ¤
      + MyApp / views / index . tt
      +...
What’s inside
 §                                                              ¤
      + MyApp / views / index . tt
      +...
What’s inside

 §                                                            ¤
      + MyApp / public / 5 0 0 . html
     ...
What’s inside

 §                                                            ¤
      + MyApp / public / 5 0 0 . html
     ...
What’s inside

 §                                                            ¤
      + MyApp / Makefile . PL
      + MyApp...
What’s inside

 §                                                            ¤
      + MyApp / Makefile . PL
      + MyApp...
Part 2: Flamenco




                   Alberto Sim˜es
                              o     Dancing Tutorial
Traffic Control



  Your MyApp/lib/MyApp.pm includes:
 §                                                                 ¤
...
How to test it?

  Start the standalone server,
 §                                                                        ...
It Works!
Route Handlers


     A Dancer app is a collection of route handlers;
     A route handler is, basically, a sub;
     It i...
Route Handlers


     A Dancer app is a collection of route handlers;
     A route handler is, basically, a sub;
     It i...
Route Handlers

     Static patterns (paths):
     §                                                                     ¤...
Route Handlers

     Static patterns (paths):
     §                                                                     ¤...
Route Handlers

     Patterns with anonymous tokens:
     §                                                     ¤
        ...
Route Handlers

     Patterns with anonymous tokens:
     §                                                     ¤
        ...
Part 3: Mambo




                Alberto Sim˜es
                           o     Dancing Tutorial
Templating



  Dancer has plugins for most templating systems:
      Mason, Template Toolkit and others.
      Default is...
Templating



  Dancer has plugins for most templating systems:
      Mason, Template Toolkit and others.
      Default is...
Rerouting


  You can reroute by:

  Passing the control to the next matching handler:
 §                                 ...
Rerouting


  You can reroute by:

  Passing the control to the next matching handler:
 §                                 ...
Rerouting


  You can reroute by:

  Passing the control to the next matching handler:
 §                                 ...
Serving Files

  You can serve a static file:
 §                                                              ¤
       get ...
Serving Files

  You can serve a static file:
 §                                                              ¤
       get ...
Thanks to Alexis Sukrieh
  (I stole some slides)
Dancing Tutorial
Upcoming SlideShare
Loading in …5
×

of

Dancing Tutorial Slide 1 Dancing Tutorial Slide 2 Dancing Tutorial Slide 3 Dancing Tutorial Slide 4 Dancing Tutorial Slide 5 Dancing Tutorial Slide 6 Dancing Tutorial Slide 7 Dancing Tutorial Slide 8 Dancing Tutorial Slide 9 Dancing Tutorial Slide 10 Dancing Tutorial Slide 11 Dancing Tutorial Slide 12 Dancing Tutorial Slide 13 Dancing Tutorial Slide 14 Dancing Tutorial Slide 15 Dancing Tutorial Slide 16 Dancing Tutorial Slide 17 Dancing Tutorial Slide 18 Dancing Tutorial Slide 19 Dancing Tutorial Slide 20 Dancing Tutorial Slide 21 Dancing Tutorial Slide 22 Dancing Tutorial Slide 23 Dancing Tutorial Slide 24 Dancing Tutorial Slide 25 Dancing Tutorial Slide 26 Dancing Tutorial Slide 27 Dancing Tutorial Slide 28 Dancing Tutorial Slide 29 Dancing Tutorial Slide 30 Dancing Tutorial Slide 31 Dancing Tutorial Slide 32 Dancing Tutorial Slide 33 Dancing Tutorial Slide 34 Dancing Tutorial Slide 35 Dancing Tutorial Slide 36 Dancing Tutorial Slide 37 Dancing Tutorial Slide 38 Dancing Tutorial Slide 39 Dancing Tutorial Slide 40 Dancing Tutorial Slide 41 Dancing Tutorial Slide 42 Dancing Tutorial Slide 43 Dancing Tutorial Slide 44
Upcoming SlideShare
Writing webapps with Perl Dancer
Next
Download to read offline and view in fullscreen.

3 Likes

Share

Download to read offline

Dancing Tutorial

Download to read offline

A brief tutorial to the Dancer web framework. Held in 1/March/2011 in Braga Geek Nights.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Dancing Tutorial

  1. 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. 2. Part 0: Ballet Alberto Sim˜es o Dancing Tutorial
  3. 3. What’s Dancer?
  4. 4. A micro framework for writing web applications
  5. 5. So, Yet Another Web Framework?
  6. 6. Well, yes!
  7. 7. CGI.pm webapps are spaghetti
  8. 8. Catalyst is HUGE
  9. 9. Ruby introduced something different
  10. 10. We needed something similar in Perl
  11. 11. Definitely
  12. 12. And they also imitated us!
  13. 13. Available at http://perldancer.org and in GitHub.
  14. 14. Part 1: Tango Alberto Sim˜es o Dancing Tutorial
  15. 15. Installing § ¤ $ sudo cpan Dancer ¦ ¥ or § ¤ $ sudo cpan Task : : Dancer ¦ ¥ or yet § ¤ $ cpanm −S Task : : Dancer ¦ ¥ Alberto Sim˜es o Dancing Tutorial
  16. 16. 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
  17. 17. 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
  18. 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. 19. 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
  20. 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. 21. 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
  22. 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. 23. 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
  24. 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. 25. Part 2: Flamenco Alberto Sim˜es o Dancing Tutorial
  26. 26. 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
  27. 27. 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
  28. 28. It Works!
  29. 29. 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
  30. 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. 31. 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
  32. 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. 33. 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
  34. 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. 35. Part 3: Mambo Alberto Sim˜es o Dancing Tutorial
  36. 36. 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
  37. 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. 38. 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
  39. 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. 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. 41. 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
  42. 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. 43. Thanks to Alexis Sukrieh (I stole some slides)
  • jeanclaudedalot1

    Mar. 10, 2019
  • OrazioAiello1

    Jan. 29, 2017
  • lpirir

    Jan. 9, 2012

A brief tutorial to the Dancer web framework. Held in 1/March/2011 in Braga Geek Nights.

Views

Total views

21,190

On Slideshare

0

From embeds

0

Number of embeds

13,551

Actions

Downloads

40

Shares

0

Comments

0

Likes

3

×