8. What is
Mojolicious?
пятница, 26 февраля 2010 г.
9. What is Mojolicious?
• Pure Perl amazing web framework, without
any hidden magiс
• No requirements besides Perl 5.8.1
• Very clean and object oriented API
• Full stack HTTP 1.1, WebSocket, IPv6, SSL
• CGI, FastCGI, PSGI, Daemon and Prefork
• There are 3 frameworks:
Mojo, Mojolicious and Mojolicious::Lite
пятница, 26 февраля 2010 г.
10. «Fresh code,
based upon years of
experience developing
Catalyst»
пятница, 26 февраля 2010 г.
15. Mojo::Base
Minimal Base Class For Mojo Projects
пятница, 26 февраля 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 { ... }
пятница, 26 февраля 2010 г.
17. 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 { ... }
пятница, 26 февраля 2010 г.
18. 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 { ... }
пятница, 26 февраля 2010 г.
62. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
63. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
64. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
65. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
66. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
67. my $client = Mojo::Client->new;
$self->client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
68. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
69. my $client = Mojo::Client->new;
$client->post(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
70. my $client = Mojo::Client->new;
$client->head(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
71. my $client = Mojo::Client->new;
$client->put(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
72. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
73. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
74. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
75. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
76. my $client = Mojo::Client->new;
$client->get(
‘http://goo.gl’ => sub {
my ($self, $tx) = @_;
say $tx->res;
}
)->process;
пятница, 26 февраля 2010 г.
77. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
78. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
79. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
80. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
81. • Mojo::Trasaction, Mojo::Stateful
• Mojo::IOLoop
• Mojo::Client, Mojo::Server
• Mojo::Server::CGI, Mojo::Server::FastCGI
• Mojo::Server::PSGI
• Mojo::Server::Daemon and ::Prefork
• Mojo::Command
• Mojo::Command::Generate and ~ Server
пятница, 26 февраля 2010 г.
82. Mojolicious
is the web in a box!
пятница, 26 февраля 2010 г.
83. No requirements
besides Perl 5.8.1
пятница, 26 февраля 2010 г.
90. 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!');
}
пятница, 26 февраля 2010 г.
91. 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!');
}
пятница, 26 февраля 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!');
}
пятница, 26 февраля 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!');
}
пятница, 26 февраля 2010 г.
94. use overload '""' => sub
{ shift->to_string },
fallback => 1;
пятница, 26 февраля 2010 г.
97. 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!');
}
пятница, 26 февраля 2010 г.
98. 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!');
}
пятница, 26 февраля 2010 г.
99. A generation
of the mojo app
пятница, 26 февраля 2010 г.
102. 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’)
пятница, 26 февраля 2010 г.
104. 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!');
}
пятница, 26 февраля 2010 г.
111. WebSocket protocol
Google, Inc
December 16, 2009
пятница, 26 февраля 2010 г.
112. use Mojolicious::Lite;
websocket ‘/echo’ => sub {
my $self = shift;
$self->receive_message(sub {
my ($self, $msg) = @_;
$self->send_massage("echo: $msg");
});
};
shagadelic;
пятница, 26 февраля 2010 г.
113. use Mojolicious::Lite;
websocket ‘/echo’ => sub {
my $self = shift;
$self->receive_message(sub {
my ($self, $msg) = @_;
$self->send_massage("echo: $msg");
});
};
shagadelic;
пятница, 26 февраля 2010 г.
132. script/test_mojo
COMMAND [OPTIONS]
script/test_mojo cgi
script/test_mojo fastcgi
script/test_mojo daemon
script/test_mojo daemon_prefork
пятница, 26 февраля 2010 г.
133. script/test_mojo
COMMAND [OPTIONS]
script/test_mojo cgi
script/test_mojo fastcgi
script/test_mojo daemon
script/test_mojo daemon_prefork
script/test_mojo psgi
пятница, 26 февраля 2010 г.
134. script/test_mojo
generate psgi
my_app.psgi
пятница, 26 февраля 2010 г.
135. use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/../lib";
use Mojo::Server::PSGI;
my $psgi = Mojo::Server::PSGI->new(
app_class => 'MyApp'
);
my $app = sub { $psgi->run(@_) };
пятница, 26 февраля 2010 г.
136. script/test_mojo
COMMAND [OPTIONS]
script/test_mojo cgi
script/test_mojo fastcgi
script/test_mojo daemon
script/test_mojo daemon_prefork
script/test_mojo psgi
script/test_mojo test
пятница, 26 февраля 2010 г.
137. script/test_mojo
COMMAND [OPTIONS]
script/test_mojo cgi
script/test_mojo fastcgi
script/test_mojo daemon
script/test_mojo daemon_prefork
script/test_mojo psgi
script/test_mojo test
script/test_mojo get
пятница, 26 февраля 2010 г.
138. script/test_mojo
COMMAND [OPTIONS]
script/test_mojo cgi
script/test_mojo fastcgi
script/test_mojo daemon
script/test_mojo daemon_prefork
script/test_mojo psgi
script/test_mojo test
script/test_mojo get http://goo.gl
пятница, 26 февраля 2010 г.
140. Mojo
Base framework
пятница, 26 февраля 2010 г.
141. 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!');
}
пятница, 26 февраля 2010 г.
187. • extension of temlplates is .ep
• caсhing templates with stash parameters
• $self->stash(foo => ‘bar’) – $foo
• any helpers
• default handler
пятница, 26 февраля 2010 г.
205. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to('example#welcome', id => 1);
}
1;
пятница, 26 февраля 2010 г.
206. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to('example#welcome', id => 1);
}
1;
пятница, 26 февраля 2010 г.
207. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to('example#welcome', id => 1);
}
1;
пятница, 26 февраля 2010 г.
208. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to('example#welcome', id => 1);
}
1;
пятница, 26 февраля 2010 г.
209. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to('example#welcome', id => 1);
}
1;
пятница, 26 февраля 2010 г.
210. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->route('/:controller/:action/:id')
->to('example#welcome', id => 1);
}
1;
пятница, 26 февраля 2010 г.
211. package TestApp;
use base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$r->bridge->to('auth#check') # check auth
->route(...)->to(...);
}
1;
пятница, 26 февраля 2010 г.
212. package TestApp::Example;
use base 'Mojolicious::Controller';
sub welcome {
my $self = shift;
warn $self->stash(‘id’);
$self->render(message => 'Perlicious!');
}
1;
пятница, 26 февраля 2010 г.
253. [write] test_lite
[chmod] test_lite 744
пятница, 26 февраля 2010 г.
254. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
255. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
256. use Mojolicious::Lite;
get '/' => 'index'; # /
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
257. use Mojolicious::Lite;
post '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
258. use Mojolicious::Lite;
any '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
259. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub { # /*
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
260. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => [groovy => qr/d+/] => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
261. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => {groovy => 42} => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
262. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => (agent=>qr/Firefox/) => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
263. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
app->renderer->default_handler(‘epl’);
shagadelic;
пятница, 26 февраля 2010 г.
264. use Mojolicious::Lite;
websocket '/echo' => sub { ... }; # websocket
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
265. use Mojolicious::Lite;
ladder sub { ... }; # auth
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic;
пятница, 26 февраля 2010 г.
266. use Mojolicious::Lite;
get '/' => 'index';
any '/:groovy' => sub {
my $self = shift;
$self->render_text($self->stash('groovy'));
};
shagadelic; # shagadelic(‘cgi’)
пятница, 26 февраля 2010 г.
271. Mojolicious Style Guide
• Keep it simple, no magic 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
пятница, 26 февраля 2010 г.
272. Mojolicious Style Guide
• Keep it simple, no magic 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
пятница, 26 февраля 2010 г.
273. Mojolicious Style Guide
• Keep it simple, no magic 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
пятница, 26 февраля 2010 г.
274. Mojolicious Style Guide
• Keep it simple, no magic 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
пятница, 26 февраля 2010 г.
275. Mojolicious Style Guide
• Keep it simple, no magic 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
пятница, 26 февраля 2010 г.
276. Mojolicious Style Guide
• Keep it simple, no magic 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
пятница, 26 февраля 2010 г.