0
Dancer (the Effortless Perl Web Framework)
About me <ul><li>Sawyer X
Sysadmin / Perl Ninja
Israel.pm / Haifa.pm / TelAviv.pm / Rehovot.pm
I do system, networking, web, applications, etc.
http://blogs.perl.org/users/sawyer_x/
http://search.cpan.org/~xsawyerx/ </li></ul>
Perl web recap <ul>1995 CGI </ul>
Perl web recap <ul>2010 Many frameworks (including micro-frameworks like  Dancer ) credit to Dave Rolsky for this intro </ul>
The big web religions, illustrated
Ruby – the fanboys
Python – the sticklers
PHP – the nonsensical
Perl – the nutcases
Nutcases? <ul><li>Yes ,  we are insane (but not LISP-insane)
Insanity is a whole lot of fun!
Insanity gives us flexibility
Flexibility gives us cool stuff
Like Moose and meta-programming
Like DBIx::Class
Like Dancer </li></ul>
Flask  (Pythonese) <ul>from  flask  import  Flask app = Flask(__name__) @ app.route ( &quot;/&quot; , methods=[ 'GET' ] ) ...
Dancer  (Perlesque) <ul>use  Dancer; get   “/”  =>  sub  { “ Hello, World!” }; dance ; </ul>
In comparison <ul>from  flask  import  Flask app = Flask(__name__) @ app.route ( &quot;/&quot; , methods=[ 'GET' ] ) def  ...
Web development models
CGI <ul><li>Most common web programming model
Easy to configure on a web server
Programmer gets everything in %ENV
First paragraph in output is for the browser
Second paragraph is content for the browser
Error prone, depressing, boring, long </li></ul>
MVC <ul><li>Separation of Model, View, Controller
Much more flexible and scalable
Uses “Namespace Matching”
Sometimes an over-kill
MVC framework example: Catalyst </li></ul>
Upcoming SlideShare
Loading in...5
×

PerlDancer for Perlers (FOSDEM 2011)

5,996

Published on

This are the slides for the Dancer talk I gave at the Perl devroom at FOSDEM 2011.

It is more up-to-date than the Pythoners version.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,996
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
35
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "PerlDancer for Perlers (FOSDEM 2011)"

  1. 1. Dancer (the Effortless Perl Web Framework)
  2. 2. About me <ul><li>Sawyer X
  3. 3. Sysadmin / Perl Ninja
  4. 4. Israel.pm / Haifa.pm / TelAviv.pm / Rehovot.pm
  5. 5. I do system, networking, web, applications, etc.
  6. 6. http://blogs.perl.org/users/sawyer_x/
  7. 7. http://search.cpan.org/~xsawyerx/ </li></ul>
  8. 8. Perl web recap <ul>1995 CGI </ul>
  9. 9. Perl web recap <ul>2010 Many frameworks (including micro-frameworks like Dancer ) credit to Dave Rolsky for this intro </ul>
  10. 10. The big web religions, illustrated
  11. 11. Ruby – the fanboys
  12. 12. Python – the sticklers
  13. 13. PHP – the nonsensical
  14. 14. Perl – the nutcases
  15. 15. Nutcases? <ul><li>Yes , we are insane (but not LISP-insane)
  16. 16. Insanity is a whole lot of fun!
  17. 17. Insanity gives us flexibility
  18. 18. Flexibility gives us cool stuff
  19. 19. Like Moose and meta-programming
  20. 20. Like DBIx::Class
  21. 21. Like Dancer </li></ul>
  22. 22. Flask (Pythonese) <ul>from flask import Flask app = Flask(__name__) @ app.route ( &quot;/&quot; , methods=[ 'GET' ] ) def hello (): return &quot;Hello World!&quot; if __name__ == &quot;__main__&quot; : app.run() </ul>
  23. 23. Dancer (Perlesque) <ul>use Dancer; get “/” => sub { “ Hello, World!” }; dance ; </ul>
  24. 24. In comparison <ul>from flask import Flask app = Flask(__name__) @ app.route ( &quot;/&quot; , methods=[ 'GET' ] ) def hello (): return &quot;Hello World!&quot; if __name__ == &quot;__main__&quot; : app.run() </ul><ul>use Dancer; get “/” => sub { “ Hello, World!” }; dance ; </ul>
  25. 25. Web development models
  26. 26. CGI <ul><li>Most common web programming model
  27. 27. Easy to configure on a web server
  28. 28. Programmer gets everything in %ENV
  29. 29. First paragraph in output is for the browser
  30. 30. Second paragraph is content for the browser
  31. 31. Error prone, depressing, boring, long </li></ul>
  32. 32. MVC <ul><li>Separation of Model, View, Controller
  33. 33. Much more flexible and scalable
  34. 34. Uses “Namespace Matching”
  35. 35. Sometimes an over-kill
  36. 36. MVC framework example: Catalyst </li></ul>
  37. 37. Route dispatching <ul><li>Succinct
  38. 38. Clean
  39. 39. Lightweight
  40. 40. MVC-ish
  41. 41. Less enterprisey
  42. 42. Example: Dancer </li></ul>
  43. 43. Dancer treats <ul><li>Both read and write , easily!
  44. 44. Route-based (started as a port of Sinatra)
  45. 45. PSGI/Plack compliant
  46. 46. Minimal dependencies
  47. 47. Any app is also a web server
  48. 48. CPAN-friendly (<3 CPAN) </li></ul>
  49. 49. Recipe for Dancing <ul><li>Take an HTTP method
  50. 50. Add a path to that
  51. 51. Mix with a subroutine
  52. 52. And sprinkle plugins and keywords on top </li></ul>
  53. 53. Dancer route structure <ul>get '/path' => sub { … }; post '/path' => sub { … }; put '/path' => sub { … }; del '/path' => sub { … }; options '/path' => sub { … }; any '/path' => sub { … }; </ul>
  54. 54. Dancer <ul><li>Paths can contain variables
  55. 55. get '/hello/:entity/'
  56. 56. Support optional variables
  57. 57. get '/hello/:entity?'
  58. 58. Paths can be Regular Expressions
  59. 59. get qr {/ (w+) / d {2,3} (.+)? } x </li></ul>
  60. 60. Dancer login example <ul>post '/login' => sub { # Validate the username and password if ( params ->{ user } eq 'bob' && params ->{ pass } eq 'LetMeIn' ) { session user => params ->{ user }; redirect params ->{ path } || '/' ; } else { redirect '/login?failed=1' ; } }; </ul>
  61. 61. Templating <ul>get '/' => sub { template index => { greeting => 'welcome' } }; </ul>
  62. 62. More nifty stuff <ul><li>headers 'My-X-Header' => 'Value'
  63. 63. send_file ( 'report.tar.gz' )
  64. 64. set_cookie name => 'value' ,
  65. 65. expires => ( time + 3600 ),
  66. 66. domain => 'foo.com'
  67. 67. status 'not_found'
  68. 68. to_json , to_yaml , to_xml
  69. 69. my $file = upload ( 'file_input' )
  70. 70. my $all_uploads = request ->uploads </li></ul>
  71. 71. Dancer as Perl philosophy <ul><li>Dancer is succinct, efficient and easy to work with
  72. 72. Dancer is daring
  73. 73. (Probably the only web framework with route caching)
  74. 74. (Websockets in the works!)
  75. 75. Dancer has a lot of plugins:
  76. 76. (engines for sessions , logging , templates)
  77. 77. Serializers (JSON , YAML , XML)
  78. 78. Route filters (before , after , before_template) </li></ul>
  79. 79. Oh yeah, route caching...
  80. 80. Dancer::Plugin::REST <ul>get '/user/:id.:format' => sub { UserRS ->find( params ->{ id } ); }; # curl http://mywebservice/user/42.json { &quot;id&quot;: 42, &quot;name&quot;: &quot;John Foo&quot;, &quot;email&quot;: &quot;john.foo@hopkins.com&quot; } # curl http://mywebservice/user/42.yml -- id: 42 name: &quot;John Foo&quot; email: &quot;john.foo@hopkins.com&quot; </ul>
  81. 81. Dancer::Plugin::SiteMap <ul>use Dancer::Plugin::SiteMap; <li>You get: /sitemap and /sitemap.xml
  82. 82. “ Yup, it's that simple.” </li></ul>
  83. 83. Dancer::Plugin::Email <ul>post '/contact' => sub { email { to => 'a@b.com' , subject => 'Test' , message => $msg , attach => [ path => 'name' ], } }; </ul>
  84. 84. Dancer::Plugin::Auth::RBAC <ul>post '/login' => sub { my $user = params ->{ 'user' }; my $pass = params ->{ 'pass' }; if ( auth ( $user , $pass ) ) { if ( auth_asa ( 'guest' ) ) {...} if ( auth_can ( 'create' ) ) {...} } }; </ul>
  85. 85. Dancer::Plugin::Ajax <ul>ajax '/check_for_update' => sub { # some ajax code }; <li>Pass if X-Request-With not “XMLHttpRequest”
  86. 86. Disable the layout
  87. 87. The action built is a POST request </li></ul>
  88. 88. Dancer::Plugin::DBIC <ul><li>DBIC (DBIx::Class) – a sophisticated ORM
  89. 89. Configure the connection in the config file
  90. 90. Make the ResultSets available in routes </li></ul>
  91. 91. Dancer::Plugin::Database <ul><li>Database(s) connection in Dancer
  92. 92. get '/widget/view/:id' => sub {
  93. 93. my $sth = database ->prepare(
  94. 94. 'select * from widgets where id = ?'
  95. 95. );
  96. 96. $sth ->execute( params ->{ id } );
  97. 97. template display_widget => {
  98. 98. widget => $sth ->fetchrow_hashref,
  99. 99. };
  100. 100. }; </li></ul>
  101. 101. Even more plugins <ul><li>Dancer::Plugin::WebSocket
  102. 102. Dancer::Plugin::FlashMessage
  103. 103. Dancer::Plugin::Auth::Twitter
  104. 104. Dancer::Plugin::MobileDevice
  105. 105. Dancer::Plugin::Feed
  106. 106. Dancer::Plugin::Redis </li></ul>
  107. 107. In culmination Dancer is beautiful and fun The way programming should be PerlDancer.org search.cpan.org/perldoc?Dancer http://advent.perldancer.org/2010 (http://advent.perldancer.org/2010/2)
  108. 108. Thank you!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×