Catalyst - refactor large apps with it and have fun!
1. Catalyst - refactor & have fun
β refactor large apps
with tE@M (of individuals)
and have fun!
Adam Bartosik, krakow.pm
2. What we had?
24
Existing datawarehouse with CGI web interface
ξ
365
working (mostly) 24h*365
ξ
build by many people
ξ
Perl supports individuality
ξ
Large projects HATE this
ξ
3. What we had?
Codebase: different styles of programming /
ξ
how perl was growing
in-line script / eval { main() } / procedural
ξ
split into packages
ξ
3 approaches to html templates
ξ
3 database wrappers
ξ
own date-time counting libs
ξ
hacks, hacks, hacks.../ bugs, bugs, bugs...
ξ
4. What we had / we want
perl is not too formal a language. Great for
ξ
things you need to change,
develop the way you've never thought before
/what we mostly do/.
The bigger a perl project is,
ξ
the more rules you need to obey.
warnings, strict, perl-critic are just about code
ξ
quality, not about design practices
XP, test-approach, test-suits - hard to start but
ξ
make work easier, safer and faster (changes
are welcome!)
5. What we want?
make it the best possible way
ξ
make it easier
ξ
maintainable
ξ
learn something new, have fun :)
ξ
6. Choosing new platform
hours of talking, advocating
ξ
use CPAN instead of self-made libs (opposit to
ξ
company politic?)
they have better doc
ξ
better tested
ξ
are still being developed
ξ
eg. DateTime is slower than simple $date, but
ξ
checks ranges
SQL::Abstract can quote everything
ξ
TT can move view-logic to templates
ξ
7. web framework needed!
we don't like to deal with sessions, url handling,
ξ
redirecting (handling simple redirect in cgi is
NOT simple)
CGI::App is like glue, but we need fundaments
ξ
it should be popular enough to have some
ξ
support level /work must be done/
so the winner is...
ξ
8. Catalyst ++
Catalyst β similar to Rails
ξ
MVC β code / layout / data source
ξ
MUST (/should) be split :)
directory layout
ξ
can start web app in a minute
ξ
most of web tasks (sessions, url mapping,
ξ
redirects) are one-liners
self server to test changes (quick start for
ξ
developers)
9. Catalyst --
hard to find good doc (but going better)
ξ
too flexible, TIMTOWTDI
ξ
what to use for O/R mapping?
ξ
which templates?
ξ
type of config file?
ξ
session storage?
ξ
view type?
ξ
Rails, Django are easier to start with, have
ξ
better doc, marketing, hype, βproper way to do
itβ
10. Learning curve
?
start-up: first 2-3 weeks are the worst
ξ
tutos, docs, advent calendars, Handel, different
ξ
approaches, different results
logical or a crap we wasting time on?
ξ
11. Learning curve
Template Toolkit make life easier:
ξ
pass complex data [ {name=>bar}, {name=>baz} ]
ξ
to templates, iterations are done in tpl
<ul> [% FOREACH row IN rows %]
ξ
<li>[% row.name %]
[% END %] </ul>
easy Ajax integration
ξ
can switch totally different layouts (we needed it 3
ξ
months later β nice to be possible do this without big
hacks)
Don't forget Mason => no another lang in tpl
ξ
12. Learning curve
DBIx::Class β rapid ORM interface
ξ
use when it is worth to use
ξ
it is not trivial to rewrite complex SQL queries to
ξ
dbix, does not simplify complicated joins (we have
datawarehouse, a little more than blog β SQL can be simpler)
dbix is powerfull for data updating
ξ
$post = $c->model('app::comment')->create({
ξ
nick => $c->req->{'nick'},
value => $c->req->{'value'} });
$post->update;
13. Learning curve
Easy βhard CGI thingsβ
ξ
dynamic redirect (after save, redirect user to eg.
ξ
main page)
sub save : Local {
my ($self, $c) = @_;
# save
# redirect
$c->res->redirect($c->uri_for('/'));
}
14. Learning curve
Global βcatch-flagβ, eg:
ξ
change language
ξ
choose output format
ξ
app/Controller/Root.pm
sub pdf : Regex('.?pdf$') {
my ($self, $c) = @_;
my $redir = $c->req->path;
$redir =~ s/.pdf$//;
$c->stash->{output_pdf} = 1;
$redirect =~ s{/$}{/index};
$c->forward($redirect);
}
15. Learning curve
Preserve link layout (when SEO matters)
ξ
URL: /news/what-about-perl6.html
sub news : LocalRegex('^/(.+).html$') {
my ($self, $c) = @_;
my $title = $c->req->captures->[0];
# find this article by $title...
}
16. Learning curve
Still no best answer to all
ξ
form/multiform/validation tasks.
HTML::Widget
ξ
Form::Builder
ξ
everything is possible, but typically breaks mvc
ξ
MVC: Forms design should be in templates
ξ
(view, css, layout), not in controllers,
validation rules for input data should be placed
in models (see Rails approach)
17. Learning curve
Web-services β SOAP, XML-RPC, REST β to
ξ
share data with other systems/projects
just output xml/yaml/anything
ξ
sub xml : Local {
$c->res->body(XMLout($data)); # kiss/works
}
build simple controller with pure TT and xml
ξ
template (fast when you must pass defined output,
DTD/Schema)
many plugins/controllers in CPAN, eg.
ξ
C::P::Flavour, see also Catalyst Cookbook / simply
ask what kind of abstraction for WS you need
18. Catalyst is social!
easy to work together β code is split /by design/
ξ
into many parts
no global switches/routing rules => less
ξ
conflicts in code repository
the team can scale: start with 2, growth to 5 or
ξ
more
19. Catalyst is social!
We must obey some rules & conventions β what,
ξ
where, how β work in team need some rules
=> they make life easier
why develop own libs when there are so many
ξ
good on CPAN? We can fix bugs, extend them,
contribute to open source
less own codebase => lower costs
ξ
more developers => better approaches
ξ
other features => can need them in 2 or 3 months
ξ
20. Benefits after time
Months of active developemt but codebase is
ξ
still fresh (and it is perl, true!)
Clear design
ξ
Only βbusiness applicationβ code
ξ
Less to type
ξ
We can make deep changes:
ξ
multi-level caches for balanced nodes
ξ
with pre-caching
change layout as often as is needed by marketing :)
ξ
21. Benefits after time
Catalyst works like a web-processing
ξ
meta-language
It is not that (dirty) perl, it is a Catalyst
ξ