Catalyst, briefly

Chris Dolan

Equilibrious LLC

cdolan@cpan.org

June 14, 2007

Overview

http://www.catalystframework.org/

Flow

Why/why not Catalyst?

Why Catalyst?


Why not Catalyst?

Getting started

cpan Catalyst::Devel
cpan Catalyst::View::TT
cpan Catalyst::Model::DBIC::Schema
cpan DBIx::Class::Schema::Loader

and accept the default for a lot of questions...

catalyst.pl MadMongers
cd MadMongers
script/madmongers_create.pl view TT TT
script/madmongers_server.pl
open http://localhost:3000/
<

Start page

Code: Application

package MadMongers;

use strict;
use warnings;

use Catalyst::Runtime;
use Catalyst qw/-Debug ConfigLoader Static::Simple/;
__PACKAGE__->config( name => 'MadMongers' );
__PACKAGE__->setup;
1;

Code: Controller

package MadMongers::Controller::Root;

use strict;
use warnings;
use base 'Catalyst::Controller';

__PACKAGE__->config->{namespace} = '';
sub default : Private {
    my ( $self, $c ) = @_;

    $c->response->body( $c->welcome_message );
}
sub end : ActionClass('RenderView') {}
1;

Code: Model

create table meetings (
  id INTEGER PRIMARY KEY NOT NULL,
  topic text,
  mtg_when date,
  location text
);
insert into meetings VALUES (1, 'Lightning talks',
         '2007-06-14', 'Madison Public Library');
insert into meetings VALUES (2, 'Quick and Dirty',
         '2007-07-12', 'Madison Public Library');

Create:
sqlite3 /tmp/madmongers.db < schema.sql

Code: Model

Tell Catalyst about the database:
script/madmongers_create.pl model DB DBIC::Schema
      Schema create=static \
      'dbi:SQLite:/tmp/madmongers.db'

Code: Controller

sub meetings : Local {
    my ( $self, $c ) = @_;

    $c->stash->{meetings} = 
       [ $c->model('DB::meetings')->all ];
    $c->stash->{template} = 'meetings.tt';
}

Code: View

<h1>Meetings</h1>
<ul>
[% FOR meeting = meetings %]
 <li><b>[% meeting.topic %]</b>
      when: [% meeting.mtg_when %],
      where: [% meeting.location %]</li>
[% END %]
</ul>

Done