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.

Modern Catalyst

5,253 views

Published on

  • Be the first to comment

Modern Catalyst

  1. 1. modern Catalyst Hideo Kimura
  2. 2. about me
  3. 3. about me • hide-k
  4. 4. about me • hide-k • hideki
  5. 5. about me • hide-k • hideki • hide
  6. 6. about me • hide-k • hideki • hide • DeNA
  7. 7. about me • hide-k • hideki • hide • DeNA • MobaSif
  8. 8. Catalyst
  9. 9. Agenda
  10. 10. Agenda • How to extend modern Catalyst on your applications
  11. 11. Agenda • How to extend modern Catalyst on your applications • Not Catalyst introducing
  12. 12. Agenda • How to extend modern Catalyst on your applications • Not Catalyst introducing • Not plugin recomendation
  13. 13. Agenda • How to extend modern Catalyst on your applications • Not Catalyst introducing • Not plugin recomendation • Not tips
  14. 14. Agenda • How to extend modern Catalyst on your applications • Not Catalyst introducing • Not plugin recomendation • Not tips • No
  15. 15. Catalyst 5.8
  16. 16. Catalyst 5.8 • 13 Oct 2008
  17. 17. Catalyst 5.8 • 13 Oct 2008 • Deprecation
  18. 18. Catalyst 5.8 • 13 Oct 2008 • Deprecation • Catamoose
  19. 19. Deprecation
  20. 20. Deprecation • ::[M V C]:: style
  21. 21. Deprecation • ::[M V C]:: style • NEXT
  22. 22. Deprecation • ::[M V C]:: style • NEXT • use MRO::Compat
  23. 23. Deprecation • ::[M V C]:: style • NEXT • use MRO::Compat • __PACKAGE__->mk_accessors()
  24. 24. Deprecation • ::[M V C]:: style • NEXT • use MRO::Compat • __PACKAGE__->mk_accessors() • use Moose attribute ‘has’
  25. 25. Deprecation • ::[M V C]:: style • NEXT • use MRO::Compat • __PACKAGE__->mk_accessors() • use Moose attribute ‘has’ • and more...
  26. 26. Moose
  27. 27. Moose • A postmodern object system for Perl5
  28. 28. Moose • A postmodern object system for Perl5 • Meta Object Protocol sugar syntax
  29. 29. Moose • A postmodern object system for Perl5 • Meta Object Protocol sugar syntax • attributes
  30. 30. Moose • A postmodern object system for Perl5 • Meta Object Protocol sugar syntax • attributes • method modifiers
  31. 31. Moose • A postmodern object system for Perl5 • Meta Object Protocol sugar syntax • attributes • method modifiers • roles
  32. 32. Application package MyApp; use strict; use warnings; use parent qw/Catalyst/; use Catalyst qw/ -Debug ConfigLoader Static::Simple/; __PACKAGE__->config( name => 'MyApp' ); __PACKAGE__->setup(); 1;
  33. 33. Moose-ified Applicatoion package MyApp; use Moose; extends 'Catalyst'; __PACKAGE__->config(name => 'MyApp'); __PACKAGE__->setup( qw/ -Debug ConfigLoader Static::Simple/ ); 1;
  34. 34. Extend Application
  35. 35. Extend Application • Plugin?
  36. 36. Extend Application • Plugin? • Application namespace
  37. 37. Extend Application • Plugin? • Application namespace • Helper Method
  38. 38. Extend Application • Plugin? • Application namespace • Helper Method • Role
  39. 39. Extend Application • Plugin? • Application namespace • Helper Method • Role • base Controller
  40. 40. Extend Applicatoion package MyApp; ... after finalize => sub { my $c = shift; $c->log->(‘finalize’); }; sub foo { my $c = shift; $c->log->debug(‘foo’); } 1;
  41. 41. Extend Applicatoion package MyApp::Role::Foo; use Moose::Role; after finalize => sub { my $c = shift; $c->log->(‘finalize’); }; sub foo { my $c = shift; $c->log->debug(‘foo’); } 1;
  42. 42. Extend Applicatoion package MyApp; ... __PACKAGE__->setup( qw/ -Debug ConfigLoader Static::Simple +MyApp::Role::Foo/ ); 1;
  43. 43. Controller package MyApp::Contorller::Foo; use strict; use warnings; use parent ‘Catalyst::Controller’; sub foo : Path('foo') { my ($self, $c) = @_; $c->res->body('foo'); } 1;
  44. 44. Moose-ified Controller package MyApp::Contorller::Foo; use Moose; BEGIN {extends ‘Catalyst::Controller}; sub foo : Path('foo') { my ($self, $c) = @_; $c->res->body('foo'); } 1;
  45. 45. Extend Controller
  46. 46. Extend Controller • Moose::Role
  47. 47. Extend Controller • Moose::Role • MooseX::MethodAttributes
  48. 48. Extend Controller • Moose::Role • MooseX::MethodAttributes • ActionClass
  49. 49. Extend Controller • Moose::Role • MooseX::MethodAttributes • ActionClass • ActionRole
  50. 50. Extend Controller package MyApp::Role; use Moose::Role -traits => 'MethodAttributes'; sub foo : Path('foo') { my ($self, $c) = @_; $c->log->debug('foo'); } 1;
  51. 51. Extend Controller package MyApp::Controller::Foo; use Moose; BEGIN {extends ‘Catalyst::Controller’} with MyApp::Role; 1;
  52. 52. ActionClass package MyApp::Action::Foo; use Moose; extends 'Catalyst::Action'; after execute => sub { my ($self, $controller, $c) = @_; $c->log->debug(‘foo’); }; 1;
  53. 53. ActionClass ... sub foo : Path('foo') ActionClass('+MyApp::Action::Foo') { my ($self, $c) = @_; $c->res->body('foo'); } ...
  54. 54. ActionClass ... sub _parse_Foo_attr { return ActionClass => ‘MyApp::Action::Foo’; } sub foo : Path('foo') Foo { ...
  55. 55. ActionClass
  56. 56. ActionClass • Pros
  57. 57. ActionClass • Pros • Reusable
  58. 58. Action Class
  59. 59. Action Class sub foobar : Path(‘foobar’) ActionClass(‘Foo’) ActionClass(‘Bar’) {
  60. 60. Action Class • Cons sub foobar : Path(‘foobar’) ActionClass(‘Foo’) ActionClass(‘Bar’) {
  61. 61. Action Class • Cons sub foobar : Path(‘foobar’) ActionClass(‘Foo’) ActionClass(‘Bar’) {
  62. 62. Action Class • Cons sub foobar : Path(‘foobar’) ActionClass(‘Foo’) ActionClass(‘Bar’) {
  63. 63. Action Class • Cons sub foobar : Path(‘foobar’) ActionClass(‘Foo’) ActionClass(‘Bar’) { • NG
  64. 64. Action Class • Cons sub foobar : Path(‘foobar’) ActionClass(‘Foo’) ActionClass(‘Bar’) { • NG • Sucks
  65. 65. ActionRole
  66. 66. ActionRole • Catalyst::Controller::ActionRole
  67. 67. ActionRole • Catalyst::Controller::ActionRole • Moose::Role as Action
  68. 68. ActionRole package MyApp::ActionRole::Foo; use Moose::Role; after execute => sub { my ($self, $controller, $c) = @_; $c->log->debug(‘foo’); }; 1;
  69. 69. ActionRole package MyApp::Contorller::Foo; use Moose; BEGIN {extends ‘Catalyst::Controller::ActionRole’}; sub foo : Path('foo') Does('Foo') { my ($self, $c) = @_; $c->res->body('foo'); } ...
  70. 70. ActionRole
  71. 71. ActionRole sub foobar : Path(‘foobar’) Does(‘Foo’) Does(‘Bar’) {
  72. 72. ActionRole • Pros sub foobar : Path(‘foobar’) Does(‘Foo’) Does(‘Bar’) {
  73. 73. ActionRole • Pros sub foobar : Path(‘foobar’) Does(‘Foo’) Does(‘Bar’) {
  74. 74. ActionRole • Pros sub foobar : Path(‘foobar’) Does(‘Foo’) Does(‘Bar’) {
  75. 75. ActionRole • Pros sub foobar : Path(‘foobar’) Does(‘Foo’) Does(‘Bar’) { • OK
  76. 76. Dispatcher • Path • Local • Global • Regex • LocalRegex
  77. 77. Dispatcher • begin :Private • end :Private • auto :Private
  78. 78. Dispatcher
  79. 79. Dispatcher • default :Private
  80. 80. Dispatcher • default :Private • default :Path
  81. 81. Dispatcher • default :Private • default :Path • catchall :Path
  82. 82. Dispatcher • default :Private • default :Path • catchall :Path • index :Private
  83. 83. Dispatcher • default :Private • default :Path • catchall :Path • index :Private • index :Path :Args(0)
  84. 84. Dispatcher • default :Private • default :Path • catchall :Path • index :Private • index :Path :Args(0) • home :Path :Args(0)
  85. 85. Dispatcher
  86. 86. Dispatcher • Chained
  87. 87. Dispatcher • Chained • dispatch chains of actions
  88. 88. Dispatcher package MyApp::Controller::Foo; use Moose; BEGIN {extends 'Catalyst::Controller'} sub root :Chained('/') PathPart('foo') CaptureArgs {} sub bar :Chained('root') PathPart Args(0) { #/foo/bar } sub baz :Chained('root') PathPart Args(1) { #/foo/baz/* } 1;
  89. 89. Dispatcher • $c->forward • $c->detach • $c->go • $c->visit
  90. 90. Model
  91. 91. Model • Thin Contoroller, Smart Model
  92. 92. Model • Thin Contoroller, Smart Model • Do not depend on Catalyst
  93. 93. Model • Thin Contoroller, Smart Model • Do not depend on Catalyst • Model::Adaptor
  94. 94. Model::Adaptor package MyApp::Model::Foo; use Moose; extends 'Catalyst::Model::Adaptor'; __PACKAGE__->config(     class       => 'MyApp::Foo' ); 1;
  95. 95. Test • Catalyst::Test • Test::WWW::Mechanize::Catalyst
  96. 96. Catalyst::Test use strict; use warnings; use Test::More; use HTTP::Request::Common; BEGIN { use_ok 'Catalyst::Test', 'MyApp' } my $req = GET('/'); my $res = request($req); #$res = get(‘/’) ok($res->is_success, 'index return ok'); is($res->content_type, 'text/html', 'content type is HTML'); like($res->content, qr/Home/, 'contains Home'); done_testing();
  97. 97. Catalyst::Test ... my ($res, $c) = ctx_request('/'); ok($res->is_success, 'index return ok'); is($c->stash->{foo}, 'foo', 'statsh foo is foo'); ...
  98. 98. Test::WWW::Mechanize::Catalyst use Test::WWW::Mechanize::Catalyst; my $mech = Test::WWW::Mecanize::Catalyst->new( catalyst_app => ‘MyApp’ ); $mech->get(‘/’); $mech->content_contains(‘Home’, ‘contains Home’); $mech->submit_form( form_number => 1, fields => { foo => ‘foo’, bar => ‘bar’ } ); $mech->content_contains(‘Foo’, ‘contains Foo’); ...
  99. 99. Resources • Catalyst::Delta • Catalyst::Upgrading • Catalyst::Manual::ExtendingCatalyst • Catalyst::Manual::CatalystAndMoose • Catalyst::Manual::Cookbook • Perl • http://gihyo.jp/dev/serial/01/modern-perl • The Definitive Guide to Catalyst
  100. 100. One more thing...
  101. 101. PSGI
  102. 102. PSGI
  103. 103. PSGI • Perl Server Gateway Interface • just specification, not implementation • start developing at 2009/09/04 • talk in this morning • miyaga-san, tokuhirom-san
  104. 104. Catalyst::Engine::PSGI MyApp Catalyst::Engine::PSGI Catalyst::Engine::* ::Impl::Coro ::Impl::Mojo
  105. 105. Thank you

×