2. Catalyst & Chained
Jay Shirley: ‘jshirley’
•Director, National Auto Sport Assoc.
•Business Director, Cold Hard Code
•Catalyst, DBIx::Class
•http://our.coldhardcode.com/jshirley/
•JAPH
Friday, February 6, 2009
5. Catalyst & Chained
What Catalyst Is Not
•An experiment or prototype.
•A toy.
•Opinionated.
•An “all in one” package.
•Complicated.
Friday, February 6, 2009
6. Catalyst & Chained
What Catalyst Is
•Simple Framework.
•Trusts the developer.
•Buckets.
•Easily extensible.
Friday, February 6, 2009
7. Catalyst & Chained
The Basics
•MVC Oriented.
•Promotes good code design.
•Use all of CPAN, easily.
•Built-in server.
Friday, February 6, 2009
8. Catalyst & Chained
Dispatching
•Getting from here to there
•/public/url => /private/action
•Types:
•Chained
•Local
•Regular Expressions
Friday, February 6, 2009
10. Catalyst & Chained
Chained in 4 Sentences
•Defined execution path
•“Route” of methods to take
•Configurable URLs
•Spans Multiple Controllers
Friday, February 6, 2009
11. Catalyst & Chained
In Code:
sub base { }
sub after_base { }
sub after_after_base { }
Friday, February 6, 2009
12. Catalyst & Chained: 999 Words
In Pictures (and code)
A B display
Private Path: /a/b/end
sub A : Chained(‘/’) CaptureArgs(0){}
sub B : Chained(‘A’) CaptureArgs(0){}
sub display : Chained(‘B’) Args(0){}
Friday, February 6, 2009
13. Catalyst & Chained
What’s the point?
•Public URI != Internal Actions
•Easily add “automatic” code
(no more sub auto { })
•Each step is executed, in order.
•Can capture URI arguments
Friday, February 6, 2009
14. Catalyst & Chained: If you know mst, you know it isn’t a...
Free Lunch
•Short-circuit any request at any
point.
•Easily add midpoint actions.
•Descend namespaces.
•Sane and programmatic URI
construction.
•Easy abstraction.
Friday, February 6, 2009
28. Catalyst & Chained
The Base Class
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘setup’)
PathPart(‘photos’) Args(0)
{
}
Friday, February 6, 2009
29. Catalyst & Chained
The Base Class
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘setup’)
PathPart(‘photos’) Args(0)
{
}
Friday, February 6, 2009
30. Catalyst & Chained
The Base Class
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub display : Chained(‘setup’)
PathPart(‘photos’) Args(0)
{
}
Friday, February 6, 2009
31. Catalyst & Chained
Person::Photos
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
sub setup : Chained(‘.’) PathPart(‘’)
CaptureArgs(0)
{
my ( $self, $c ) = @_;
$c->stash->{photos} =
$c->stash->{person}->photos;
}
Friday, February 6, 2009
32. Catalyst & Chained
Person::Photos
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
sub setup : Chained(‘.’) PathPart(‘’)
CaptureArgs(0)
{
my ( $self, $c ) = @_;
$c->stash->{photos} =
$c->stash->{person}->photos;
}
Friday, February 6, 2009
33. Catalyst & Chained
Person::Photos
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
sub setup : Chained(‘.’) PathPart(‘’)
CaptureArgs(0)
{
my ( $self, $c ) = @_;
$c->stash->{photos} =
$c->stash->{person}->photos;
}
Friday, February 6, 2009
34. Catalyst & Chained
A Note
•Person::Photos only:
•cares about itself.
•is very thin (just the stash, ma’am).
•can actually be abstracted to just
configuration
Friday, February 6, 2009
35. Catalyst & Chained
Tag::Photos
•Same as Person::Photos, except:
•$c->stash->{tag} instead of
$c->stash->{person}
•has a different template.
Friday, February 6, 2009
36. Catalyst & Chained
The Base Class, Version 2
package MyApp::ControllerBase::Photos;
use parent ‘Catalyst::Controller’;
sub setup : Chained(‘.’) PathPart(‘’) CaptureArgs(0) {
my ( $self, $c ) = @_;
my $stash = $self->{stash_key};
my $source = $self->{source_key};
$c->stash->{$stash} = $c->stash->{$source}->photos;
}
sub display ... { # unchanged }
Friday, February 6, 2009
37. Catalyst & Chained
Config: Person
package MyApp::Controller::Person::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
__PACKAGE__->config(
source_key => ‘person’,
stash_key => ‘photos’
);
Friday, February 6, 2009
38. Catalyst & Chained
Config: Tag
package MyApp::Controller::Tag::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
__PACKAGE__->config(
source_key => ‘tag’,
stash_key => ‘photos’
);
Friday, February 6, 2009
39. Catalyst & Chained
Config: Tag
package MyApp::Controller::Tag::Photos;
use parent ‘MyApp::ControllerBase::Photos’;
__PACKAGE__->config(
source_key => ‘tag’,
stash_key => ‘photos’
);
Friday, February 6, 2009
40. Catalyst & Chained
That’s it
•Now, you just work with:
•/person/photos/display.tt
•/tag/photos/display.tt
•Thin controllers, how to get photos
determined just from configuration.
Friday, February 6, 2009
41. Catalyst & Chained
Questions?
http://github.com/jshirley/catalystx-example-chained/tree
Friday, February 6, 2009