9. What is Mojo?
• Pure Perl framework, without
any hidden magick
• No requirements besides Perl 5.8.1
• Very clean and object oriented API
• Full stack HTTP 1.1 (client / server)
• CGI, FastCGI, PSGI, Daemon и Prefork
• Two MVC web frameworks:
Mojolicious and Mojolicious::Lite
6 января 2010 г.
13. Mojo::Base
Minimal Base Class For Mojo Projects
6 января 2010 г.
14. package App;
use base ‘Mojo::Base’;
__PACKAGE__->attr(conf => sub {
return do ‘conf/app.conf’;
});
__PACKAGE__->attr(db => sub {
my $self = shift;
return Util->db($self->conf->{'db'});
});
sub dispatch { ... }
6 января 2010 г.
15. package App;
use base ‘Mojo::Base’;
__PACKAGE__->attr(conf => sub {
return do ‘conf/app.conf’;
});
__PACKAGE__->attr(db => sub {
my $self = shift;
return Util->db($self->conf->{'db'});
});
sub dispatch { ... }
6 января 2010 г.
16. package App;
use base ‘Mojo::Base’;
__PACKAGE__->attr(conf => sub {
return do ‘conf/app.conf’;
});
__PACKAGE__->attr(db => sub {
my $self = shift;
return Util->db($self->conf->{'db'});
});
sub dispatch { ... }
6 января 2010 г.
85. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
86. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
87. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
88. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
89. use overload '""' => sub
{ shift->to_string },
fallback => 1;
6 января 2010 г.
92. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
93. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
97. use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../../lib";
# Application
$ENV{MOJO_APP} ||= 'TestMojo';
use Mojo::Commands;
Mojo::Commands->start; # ->start(‘cgi’)
6 января 2010 г.
99. package TestMojo;
use base 'Mojo';
sub handler {
my ($self, $tx) = @_;
warn $tx->req; warn $tx->req->url;
$tx->res->headers
->content_type('text/plain');
$tx->res->body('Hello Mojo!');
}
6 января 2010 г.
146. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
147. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
148. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
149. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
150. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
151. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
152. • Data::Dumper (Maxdepth: 2, Indent: 1, Terse: 1)
• param
• layout
• include
• content
• extends
6 января 2010 г.
153. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
154. • dumper
• $self->req->param(...)
• layout
• include
• content
• extends
6 января 2010 г.
155. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
156. • dumper
• param
• $self->stash(layout => ...)
• include
• content
• extends
6 января 2010 г.
157. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
158. • dumper
• param
• layout
• $self->render_partial(...)
• content
• extends
6 января 2010 г.
159. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
160. • dumper
• param
• layout
• include
• $self->render_inner(...)
• extends
6 января 2010 г.
161. • dumper
• param
• layout
• include
• content
• extends
6 января 2010 г.
162. • dumper
• param
• layout
• include
• content
• $self->stash(extends => ...)
6 января 2010 г.
163. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
164. • extention of temlplates is .epl
• my $self = shift;
• $self->stash(foo => ‘bar’)
6 января 2010 г.
165. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
166. • extention of temlplates is .ep
• cashing templates with stash parameters
• $self->stash(foo => ‘bar’) – $foo
• any helpers
• default handler
6 января 2010 г.
167. • AgentCondition
• Charset
• DefaultHelpers
• EpRenderer
• EplRenderer
• RequestTimer
6 января 2010 г.
168. • Mojolicous::Controller
• Mojolicious::Plugin
• Mojolicious::Commands
• MojoX::Types
• MojoX::Dispatcher, MojoX::Routes
6 января 2010 г.
169. script/test_app
COMMAND [OPTIONS]
script/test_app cgi
script/test_app fastcgi
script/test_app daemon
script/test_app daemon_prefork --port 80
script/test_app test
script/test_app get
6 января 2010 г.
170. script/test_app
COMMAND [OPTIONS]
script/test_app cgi
script/test_app fastcgi
script/test_app daemon
script/test_app daemon_prefork --port 80
script/test_app test
script/test_app get
scritp/test_app routes
6 января 2010 г.
171. • Mojolicous::Controller
• Mojolicious::Plugin
• Mojolicious::Commands
• MojoX::Types
• MojoX::Dispatcher, MojoX::Routes
6 января 2010 г.
172. • Mojolicous::Controller
• Mojolicious::Plugin
• Mojolicious::Commands
• MojoX::Types
• MojoX::Dispatcher, MojoX::Routes
6 января 2010 г.
173. A generation
the mojolicious app
6 января 2010 г.
179. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
180. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
181. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
182. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
183. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
184. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
185. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->bridge->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
186. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to(controller => 'example', action =>
'welcome', id => 1);
}
6 января 2010 г.
187. package TestApp::Example;
use base 'Mojolicious::Controller';
sub welcome {
my $self = shift;
warn $self->stash(‘id’);
$self->render(message => 'Welcome!');
}
1;
6 января 2010 г.
229. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
230. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
231. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
232. use Mojolicious::Lite;
post '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
233. use Mojolicious::Lite;
any '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
234. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
235. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => [groovy => qr/d+/] => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
236. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => {groovy => 42} => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
237. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => (agent=>qr/Firefox/) => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
238. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
app->renderer->default_handler(‘epl’);
shagadelic;
6 января 2010 г.
239. use Mojolicious::Lite;
ladder sub { ... }; # auth
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
6 января 2010 г.
240. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic; # shagadelic(‘cgi’)
6 января 2010 г.
245. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama
6 января 2010 г.
246. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama
6 января 2010 г.
247. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama
6 января 2010 г.
248. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama
6 января 2010 г.
249. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama
6 января 2010 г.
250. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama
6 января 2010 г.
251. Mojo Guide Style
• Keep it simple, no magick unless absolutely nessary
• Code should be written with a Perl6 port in mind
• It’s not a feature without a test
• Only add prereqs if absolutely necessary
• No inline POD
• No spaghetti code
• Every files should contain at least one quote from
The Simpsons or Futurama :)
6 января 2010 г.
258. get ‘/bug/’ => sub {
my $self = shift;
# open my $fh, ‘|-’, ‘/usr/sbin/sendmail -t’;
open my $fh, ‘|-’, ‘perl test.pl’;
print $fh join "n",
'To: sharifulin at gmail.com',
'Subject: Test', '',
'Test!';
$self->render_text(‘Test’);;
};
6 января 2010 г.
259. get ‘/bug/’ => sub {
my $self = shift;
# open my $fh, ‘|-’, ‘/usr/sbin/sendmail -t’;
open my $fh, ‘|-’, ‘perl test.pl’;
print $fh join "n",
'To: sharifulin at gmail.com',
'Subject: Test', '',
'Test!';
$self->render_text(‘Test’);;
};
6 января 2010 г.
260. test.pl
#!/usr/bin/perl
warn $_ while <STDIN>;
6 января 2010 г.
271. package App;
use base ‘Mojo::Base’;
__PACKAGE__->attr(conf => sub {
return do ‘conf/app.conf’;
});
__PACKAGE__->attr(db => sub {
my $self = shift;
return Util->db($self->conf->{'db'});
});
sub dispatch { ... }
6 января 2010 г.
272. package App;
use base ‘Mojo::Base’;
__PACKAGE__->attr(conf => sub {
return do ‘conf/app.conf’;
});
__PACKAGE__->attr(db => sub {
my $self = shift;
return Util->db($self->conf->{'db'});
});
sub dispatch { ... }
6 января 2010 г.
273. package App;
use base ‘Mojo::Base’;
__PACKAGE__->attr(conf => sub {
return do ‘conf/app.conf’;
});
__PACKAGE__->attr(db => sub {
my $self = shift;
return $::DB ||= Util->db(...->{'db'});
});
sub dispatch { ... }
6 января 2010 г.
280. Links
• irc://irc.perl.org/#mojo
• http://lists.kraih.com/cgi-bin/mailman/
listinfo/mojo
• Twitter: @kraih, @vtivti, @sharifulin
• Juick: @vti, @sharifulin
6 января 2010 г.
281. The latest news
• PSGI and Plack is the future of HTTP::Engine that
has support for servers like CGI, FastCGI, Apache,
AnyEvent and Mojo.
• New #mod_perlite release looks great,
#Mojolicious with CGI backend should "just
work™" on it.
• Slowly starting with documentation work, crazy
how much #Mojolicious has grown recently, looks
like it needs a few extra chapters now. :)
• Added native #PSGI support to #Mojolicious
6 января 2010 г.
282. My experience
in the development
using Mojo
6 января 2010 г.