Documente Academic
Documente Profesional
Documente Cultură
• MVC architecture
• Flexible URL dispatcher
– Self-contained, no external file
• Philosophy / coding practices
– DRY – Don’t Repeat Yourself
– Glue – Thin Controllers / Model Wrappers
– Maintainability, maintainability, and maintainability
MyApp/
lib/
MyApp.pm
MyApp/
Model/
View/
Controller/
Root.pm
root/
static/
script/
myapp.conf
MyApp/
lib/
MyApp.pm Core application
MyApp/ • MyApp.pm
Model/ • Setup, plugins,
View/ default config
Controller/ • Root.pm
Root.pm • Root controller
root/
static/
script/
myapp.conf
MyApp/
lib/
MyApp.pm Data
MyApp/ • Static files
Model/ • CSS, images,
View/ JavaScript, etc
Controller/ • Templates
Root.pm • Form definitions
root/
static/
script/
myapp.conf
MyApp/
lib/
MyApp.pm Scripts
MyApp/ • Development server
Model/ • Production server
View/ • Component creation
Controller/ • Models, Views,
Root.pm Controllers
root/
static/
script/
myapp.conf
MyApp/
lib/
MyApp.pm Extras
MyApp/ • Config file
Model/ • Tests (t/ directory)
View/ • Makefile.PL
Controller/ • Dependencies
Root.pm • README / Changes
root/
static/
script/
myapp.conf
• Edit "lib/MyApp.pm"
__PACKAGE__->config->{default_view} = 'TT';
__PACKAGE__->config(
'View::TT' => {
INCLUDE_PATH =>
__PACKAGE__->path_to('root','templates'),
}
);
• Edit "lib/MyApp/Controller/Root.pm"
sub index :Path :Args(0) {
Attributes – controls
my ( $self, $c ) = @_; URL dispatcher
$c->stash->{template} = 'homepage.tt';
}
• Edit "lib/MyApp/Controller/Search.pm"
sub index :Path :Args(0) {
my ( $self, $c ) = @_;
my $q = $c->request->param('q') or do {
$c->response->redirect($c->uri_for('/'));
return;
};
"Thin Controller"
no business logic
$c->stash->{query} = $q;
$c->stash->{template} = 'results.tt';
$c->stash->{tweets} = $c->model('Twitter')->search($q);
$c->stash->{blogs} = $c->model('Google')->search($q);
$c->stash->{photos} = $c->model('Flickr')->search($q);
}
• Helper modules
– Catalyst::Model::Adaptor
– Catalyst::Model::Factory
– Catalyst::Model::Factory::PerRequest
• Which one should you use?
– Depends when you want the class instantiated
• Edit "lib/MyApp.pm"
__PACKAGE__->config('Model::Google' => {
referer => 'http://yoursite.com'
});
sub search {
my ($self, $query) = @_;
REST::Google::Search::Blogs->http_referer(
$self->{referer}
);
return REST::Google::Search::Blogs->new(
q => $query,
rsz => 'large',
);
}
Any questions?
http://perl.jonallen.info/talks