Jiro Nishiguchi( 西口次郎 ) id:spiritloose [email_address] 2009/09/09 Yokohama.pm  出張版  in YAPC::Asia 2009  前夜祭 Schenker a DSL...
$self <ul><li>PAUSE ID: JIRO
http://search.cpan.org/~jiro/
Image::ObjectDetect
Text::Migemo
etc...
http://d.hatena.ne.jp/spiritloose/ </li></ul>
What's Schenker? <ul><li>A DSL for quickly creating web applications in Perl
Inspired by Ruby's Sinatra </li></ul>
What's Sinatra? # myapp.rb require 'sinatra' get '/' do 'hello, world!' end
Schenker package MyApp; use Schenker; get '/' => sub { 'hello, world!'; }; どう見てもパク(ry
Similar frameworks <ul><li>Mojolicious::Lite
http://search.cpan.org/dist/Mojo/
Dancer
http://search.cpan.org/dist/Dancer/ </li></ul>
Requirements <ul><li>Schenker
-> HTTP::Engine + HTTP::Engine::Middleware
(feature Plack + Plack::Middleware)
Sinatra
-> Rack + Rack::Middleware </li></ul>
What's HTTP::Engine? Web Server(Apache, Lighttpd and etc) ↓ HTTP::Engine .= Rack(Ruby) .= WSGI(Python) ↓ Web Application F...
SYNOPSIS $ cat > myapp.pl package MyApp; use Schenker; get '/' => sub { 'Hello, world!'; }; $ ./myapp.pl & && firefox http...
Easy! <ul><li>No helpers
No configuration </li></ul>
Schenker supports <ul><li>Configurations
Routing
Controller
Views
No models (use your favorite modules) </li></ul>
Command line options ./myapp.pl --help Usage: ./hoge.pl [OPTIONS] -h, --help  display this help -H, --host  set the host -...
Configuration configure development => sub { set port => 8080; set host => '127.0.0.1'; set server => 'AnyEvent'; enable '...
Routing get '/'  => sub {}; post '/' => sub {}; put '/'  => sub {}; Delete '/' => sub {}; get '/' => host => 'example.com'...
Method override <form action=”/” method=”post”> <input type=”hidden” name=”_method” value=”put” /> post '/' => sub {}; put...
Paramaters get '/' => sub { params->{foo}; param('foo'); };
Nested paramaters # index.tt <form action=&quot;./&quot; method=&quot;post&quot;> <input type=&quot;text&quot; name=&quot;...
Upcoming SlideShare
Loading in …5
×

Schenker - DSL for quickly creating web applications in Perl

3,252 views
3,183 views

Published on

Yokohama.pm in YAPC::Asia 2009 arrival party
http://yokohama.pm.org/2009/08/yokohamapm-in-yapcasia-2009-1.html

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,252
On SlideShare
0
From Embeds
0
Number of Embeds
564
Actions
Shares
0
Downloads
11
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Schenker - DSL for quickly creating web applications in Perl

  1. 1. Jiro Nishiguchi( 西口次郎 ) id:spiritloose [email_address] 2009/09/09 Yokohama.pm 出張版 in YAPC::Asia 2009 前夜祭 Schenker a DSL for quickly creating web applications in Perl.
  2. 2. $self <ul><li>PAUSE ID: JIRO
  3. 3. http://search.cpan.org/~jiro/
  4. 4. Image::ObjectDetect
  5. 5. Text::Migemo
  6. 6. etc...
  7. 7. http://d.hatena.ne.jp/spiritloose/ </li></ul>
  8. 8. What's Schenker? <ul><li>A DSL for quickly creating web applications in Perl
  9. 9. Inspired by Ruby's Sinatra </li></ul>
  10. 10. What's Sinatra? # myapp.rb require 'sinatra' get '/' do 'hello, world!' end
  11. 11. Schenker package MyApp; use Schenker; get '/' => sub { 'hello, world!'; }; どう見てもパク(ry
  12. 12. Similar frameworks <ul><li>Mojolicious::Lite
  13. 13. http://search.cpan.org/dist/Mojo/
  14. 14. Dancer
  15. 15. http://search.cpan.org/dist/Dancer/ </li></ul>
  16. 16. Requirements <ul><li>Schenker
  17. 17. -> HTTP::Engine + HTTP::Engine::Middleware
  18. 18. (feature Plack + Plack::Middleware)
  19. 19. Sinatra
  20. 20. -> Rack + Rack::Middleware </li></ul>
  21. 21. What's HTTP::Engine? Web Server(Apache, Lighttpd and etc) ↓ HTTP::Engine .= Rack(Ruby) .= WSGI(Python) ↓ Web Application Framework(Catalyst, Rails, Django and etc)
  22. 22. SYNOPSIS $ cat > myapp.pl package MyApp; use Schenker; get '/' => sub { 'Hello, world!'; }; $ ./myapp.pl & && firefox http://localhost:4567/
  23. 23. Easy! <ul><li>No helpers
  24. 24. No configuration </li></ul>
  25. 25. Schenker supports <ul><li>Configurations
  26. 26. Routing
  27. 27. Controller
  28. 28. Views
  29. 29. No models (use your favorite modules) </li></ul>
  30. 30. Command line options ./myapp.pl --help Usage: ./hoge.pl [OPTIONS] -h, --help display this help -H, --host set the host -p, --port=PORT set the port -e, --environment=ENV set the environment -s, --server=SERVER HTTP::Engine interface === snip ===
  31. 31. Configuration configure development => sub { set port => 8080; set host => '127.0.0.1'; set server => 'AnyEvent'; enable 'sessions'; disable 'run'; };
  32. 32. Routing get '/' => sub {}; post '/' => sub {}; put '/' => sub {}; Delete '/' => sub {}; get '/' => host => 'example.com' => sub {}; get '/' => agent => qr/MobileSafari/ => sub {};
  33. 33. Method override <form action=”/” method=”post”> <input type=”hidden” name=”_method” value=”put” /> post '/' => sub {}; put '/' => sub {};
  34. 34. Paramaters get '/' => sub { params->{foo}; param('foo'); };
  35. 35. Nested paramaters # index.tt <form action=&quot;./&quot; method=&quot;post&quot;> <input type=&quot;text&quot; name=&quot;entry[title]&quot; /> <textarea name=&quot;entry[body]&quot;></textarea> </form> # myapp.pl params->{entry}; # { title => '', body => '' }
  36. 36. Path paramaters get '/user/:user' => sub { my $args = shift; &quot;hello, $args->{user}!&quot;; };
  37. 37. Encoding set encode => { encode => 'utf-8', decode => 'utf-8' }; get '/user/:name' => sub { my $args = shift; ok utf8::is_utf8($args->{name}); ok utf8::is_utf8(params->{foo}); };
  38. 38. View <ul><li>Template-Toolkit
  39. 39. Text::MicroTemplate </li></ul>
  40. 40. Template-Toolkit tt_options WRAPPER => 'layout.tt'; get '/tt' => sub { tt 'index'; # views/index.tt };
  41. 41. In-file Template get '/' => sub { stash name => 'Michael Schenker'; tt 'index'; }; __END__ @@ index hello, [% name %]!
  42. 42. TT with PadWalker get '/hello/:name' => sub { my $args = shift; tt 'index'; }; __END__ @@ index hello, [% args.name %]!
  43. 43. Text::MicroTemplate get '/mt' => sub { mt 'index' # views/index.mt };
  44. 44. Helpers helper foo => sub { &quot;foo$_[0]&quot;; }; get '/' => sub { foo('bar') }; [% foo('bar') %] <?= foo('bar') ?>
  45. 45. Before filters Before sub { stash now => DateTime->now; }; get '/' =>sub { stash->{now}; }; [% now %]
  46. 46. Functions get '/' => sub { request; # HTTP::Engine::Request response; # HTTP::Engine::Response status 200; body 'hello, world!'; headers 'X-Hoge' => 'hoge'; back; # Referer };
  47. 47. Example: Image File get '/qrcode' => sub { my $text = params->{text}; content_type 'image/png'; GD::Barcode::QRcode->new($text)->plot->png; };
  48. 48. Example: Send file get '/' => sub { send_file 'image.jpg'; }; get '/' => sub { send_file 'image.jpg', filename => 'image.jpg'; };
  49. 49. Halting get '/' => sub { halt; do_something(); # NOTREACHED }; get '/' => sub { redirect 'http://www.yahoo.co.jp/'; do_something(); # NOTREACHED };
  50. 50. Error Handling define_error MyError => sub { status 500; tt 'myerror'; }; get '/' => sub { raise MyError; do_something(); # NOTREACHED };
  51. 51. Sessions enable 'sessions'; get '/' => sub { session; # HTTP::Session session->get('foo'); session->set(foo => 'bar'); };
  52. 52. Sample Application <ul><li>Rails scaffold like CRUD interface
  53. 53. with DBIx::Class
  54. 54. http://github.com/spiritloose/schenker-sample/
  55. 55. Runs on CGI, FastCGI, mod_perl, AnyEvent, POE...
  56. 56. Includes configuration file (eg: httpd.conf) </li></ul>
  57. 57. TODO <ul><li>Release
  58. 58. PSGI, Plack
  59. 59. Documentation
  60. 60. Extending Schenker </li></ul>
  61. 61. Thanks! <ul>References <li>http://github.com/spiritloose/Schenker
  62. 62. http://www.sinatrarb.com/
  63. 63. http://d.hatena.ne.jp/spiritloose/ </li></ul>

×