Catalyst Inside - DRY

YAPC::Asia 2006 Tokyo

March 30th, 2006

Catalyst is DRY

Catalyst handlers

use Catalyst;

sub import {
    my ( $class, @arguments ) = @_;

    my $caller = caller(0);

    unless ( $caller->isa('Catalyst') ) {
        no strict 'refs';
        push @{"$caller\::ISA"}, $class, 'Catalyst::Controller';
    }

    # snip....
}

push @{"MyApp::ISA"}, 'Catalyst';

Class structure

<Plugins>  <Catalyst>
  \        /
    MyApp

$c = MyApp->new

$c

Catalyst context object

Plugin

or

Plugin load trick

use Catalyst qw/FormValidator::Simple FillInForm/;

__PACKAGE__->setup(
    do {
        my @plugins;
        push @plugins, 'StackTrace' if $ENV{CATALYST_DEBUG};
        push @plugins, 'Static::Simple'
            if ($ENV{CATALYST_ENGINE} || '') =~ /^HTTP/;

        return @plugins;
    }
);

Simple plugin

package Catalyst::Plugin::Dumper;
use strict;
use warnings;

use Data::Dumper;

sub dumper {
    my $c = shift;

    $c->log->debug( Dumper @_ );
}

1;

Usage

$c->dumper( ... );

Override catalyst's build-in functions

package Catalyst::Plugin::XFramework;
use strict;
use warnings;

use NEXT;

sub finalize {
    my $c = shift;

    $c->res->header(
        'X-Framework' =>
            'Catalyst/'.$c->version);
    $c->NEXT::finalize(@_);
}

1;

Helper

./script/myapp_create.pl \
 model|view|controller \
 name [helper] [options]

Usage

myapp_create.pl controller Page

MyApp/Controller/Page.pm が作られる。

Use own helper

myapp_create.pl MyHelper

myapp_create.pl controller Page MyHelper

Make helper

package Catalyst::Helper::MyHelper;
use strict;
use warnings;

use File::Spec;

sub mk_stuff {
    my ( $self, $helper, @args ) = @_;
    $helper->render_file(
        'file',
        File::Spec->catfile(
            $helper->{base}, 'root', 'js', 'prototype.js'
        )
    );
}

1;

__DATA__

__file__
ここにprototype.js

Make component helper

package Catalyst::Helper::Controller::MyHelper;
use strict;
use warnings;

sub mk_compclass {
    my ( $self, $helper, @args ) = @_;
    my $file = $helper->{file};
    $helper->render_file( 'compclass', $file );
}

1;

__DATA__

__compclass__
package [% class %]

# なんかコード

$helper の中身

共通のもの

component class

Summary

continued...

Summary

continued...

Summary

continued...

Summary

continued...

Summary

continued...

Summary

continued...

Summary

That's All