DANCING MOOS
and other things I like
Luís Miguel Braga
@microft
[email protected]
ONCE UPON A TIME...
" PODES FAZER NO QUE TU
QUISERES.
DESDE QUE SEJA EM PERL. "
("Do it with whatever you like. Just as long as it is Perl")
CATALYST
"Catalyst is the most popular Perl MVC framework and makes creating web
applications fun, rewarding and quick."
MVC
Flexible
Production Ready
Large contributing community
Hundreds of plugins on CPAN
" JÁ EXPERIMENTASTE DANCER? "
("Have you tried Dancer?")
DANCER
"The easiest way to write web application in Perl"
MVC(-ish)
Flexible
Pluggable
Lightweight
Modeled after Sinatra (Ruby Framework)
DANCER
Install:
cpanm Dancer
Create project:
dancer -a MyWeb::App
Run:
cd MyWeb-App
./bin/app.pl
DANCER
Hello world:
use Dancer;
get '/' => sub {
return "Hello world";
};
dance;
DANCER
Hello world
get '/hello/:name?' => sub {
"Hello there " . (param('name') || "whoever you are!");
};
DANCER
Hello world++
any ['get', 'post'] => '/login' => sub {
my $err;
if ( request->method() eq "POST" ) {
# process form input
if ( params->{'username'} ne setting('username') ) {
$err = "Invalid username";
}
elsif ( params->{'password'} ne setting('password') ) {
$err = "Invalid password";
}
else {
session 'logged_in' => true;
set_flash('You are logged in.');
return redirect '/';
}
}
# display login form
template 'login.tt', {
'err' => $err,
};
};
DANCER
The stuff I like:
Not in the way
Simple (the good kind)
Small DSL
Fast
DANCER
Stuff I missed
Object Oriented (like django)
Solutions:
Moose?
Mouse?
MOO
Minimalist Object Orientation
(with Moose support)
"[..] almost - but no quite - two thirds of Moose"
MOO
"as little as possible"
- Extend Moose classes
- Consume Moose Roles
- Extend Mouse classes
- Consume Mouse Roles
MOO
What's the catch?
No metaprotocol
No Moose Types (built in)
No super, override, inner, augment, dump
(author's choice)
lazy_build, auto_deref are not supported
"default" only takes coderefs
MOO
package Small;
use Moo;
use namespace::autoclean;
# __PACKAGE__->meta->make_immutable();
1;
<<<----
SOME NUMBERS
Loading
Dancer:
Moose:
Mouse:
Moo:
Mo:
real
real
real
real
real
0m0.232s
0m0.275s
0m0.044s
0m0.031s
0m0.009s
user
user
user
user
user
0m0.164s
0m0.242s
0m0.028s
0m0.023s
0m0.004s
sys
sys
sys
sys
sys
0m0.030s
0m0.024s
0m0.008s
0m0.007s
0m0.004s
Object Creation
Moose:
Mouse:
Moo:
Mo:
9
3
4
2
wallclock
wallclock
wallclock
wallclock
secs
secs
secs
secs
(
(
(
(
8.03
3.19
3.46
1.15
usr
usr
usr
usr
+
+
+
+
0.02
0.01
0.01
0.01
sys
sys
sys
sys
=
=
=
=
8.05
3.20
3.47
1.16
CPU)
CPU)
CPU)
CPU)
...
...
...
...
MORE NUMBERS
Getters
Moose:
Mouse:
Moo:
Mo:
1
1
1
1
wallclock
wallclock
wallclock
wallclock
secs
secs
secs
secs
(
(
(
(
0.57
0.26
0.51
0.53
usr
usr
usr
usr
+
+
+
+
0.00
0.00
0.01
0.00
sys
sys
sys
sys
=
=
=
=
0.57
0.26
0.52
0.53
CPU)
CPU)
CPU)
CPU)
Setters
Moose:
Mouse:
Moo:
Moo:
1
1
1
1
wallclock
wallclock
wallclock
wallclock
secs
secs
secs
secs
(
(
(
(
0.66
0.22
0.49
0.49
usr
usr
usr
usr
+
+
+
+
0.00
0.01
0.00
0.01
sys
sys
sys
sys
=
=
=
=
0.66
0.23
0.49
0.50
CPU)
CPU)
CPU)
CPU)
MOO
Start with Moo
package MyProject::Small;
use Moo;
sub do_something { ... }
has a_attribute => (
is => 'ro',
);
has another_attribute => (
is => 'rw',
isa => sub { die "$_[0] is over 9000!" unless $_[0] < 9000 },
);
1;
MOO
Continue with Moose
package MyProject::Big;
use Moose;
extends "MyProject::Small";
MORE CODE HERE
1;
ROLES
package MyProject::Small;
use Moo;
with "MyProject::SmallRole";
# Moo::Role
sub do_something {...}
has attr => {
is => 'ro',
....
}
1;
package MyProject::Big;
use Moose;
extends "MyProject::Small";
with "MyProject::BigRole" ;
...
1;
# Moose::Role
DANCER + MOO
MAKES SENSE!
DANCER 2
In the works
Complete re-write
Object Oriented
Based on Moo (likely)
FUN
simple function signatures
sub float_eq {
my ($a, $b, $e) = @_;
$e = 0.0001 unless defined $e;
return abs($a - $b) < $e
}
use Fun;
fun float_eq ($a, $b, $e = 0.0001) {
return abs($a - $b) < $e;
}
THANKS!
Download

and other things I like Luís Miguel Braga @microft microft