Mini aplicac˜es web com
            ¸o
Mojolicious::Lite
         Andr´ Santos
              e
       andrefs@cpan.org

       PtPW 2010
         5 de Julho
Disclaimer


   [Web] developer profissional
Disclaimer


   [Web] developer profissional
   Perl::Newbie
Disclaimer


   [Web] developer profissional
   Perl::Newbie

   #{coisas certas} > #{coisas erradas}
Ruby
Ruby



Perl
Ruby
Ruby on Rails
“open source MVC web app framework”

Perl
Ruby
Ruby on Rails
“open source MVC web app framework”

Perl
Catalyst
(outros)
Ruby



Perl
Ruby
Sinatra
pequenas aplicac˜es web
               ¸o

Perl
Ruby
Sinatra
pequenas aplicac˜es web
               ¸o

Perl
Mojolicious::Lite
“micro web framework”
/Mojo.*/

   Mojo
   Mojolicious
   Mojolicious::Lite
/Mojo.*/

   Mojo
   Mojolicious
   Mojolicious::Lite

   criados por Sebastian Riedel
       tamb´m criador de Catalyst
            e
       developer RoR
/Mojo.*/
.---------------------------------------------------------------.
|                             Fun!                              |
’---------------------------------------------------------------’
.---------------------------------------------------------------.
|                                                               |
|                .----------------------------------------------’
|                | .--------------------------------------------.
|   Application | |               Mojolicious::Lite             |
|                | ’--------------------------------------------’
|                | .--------------------------------------------.
|                | |                 Mojolicious                |
’----------------’ ’--------------------------------------------’
.---------------------------------------------------------------.
|                             Mojo                              |
’---------------------------------------------------------------’
.-------. .-----------. .--------. .------------. .-------------.
| CGI | | FastCGI | | PSGI | | HTTP 1.1 | | WebSocket |
’-------’ ’-----------’ ’--------’ ’------------’ ’-------------’
/Mojo.*/

Mojo
  “The box!”
  Encapsula as complexidades de CGI,
  FastCGI, PSGI, HTTP e WebSocket num
  s´ m´todo
   o e
  D´ suporte a frameworks web como
    a
  Mojolicious e Mojolicious::Lite
/Mojo.*/


Mojolicious
   “The web in a box!”
   framework MVC com API OO Perl
   depende apenas de Perl 5.8.1
Mojolicious::Lite

Single file micro web apps framework
 ˜
NAO!
    N˜o apropriado para produc˜o, software
      a                      ¸a
    comercial, ...
Mojolicious::Lite

Single file micro web apps framework
 ˜
NAO!
    N˜o apropriado para produc˜o, software
      a                       ¸a
    comercial, ...
    N˜o apropriado para aplicac˜es complexas
      a                       ¸o
Mojolicious::Lite

Single file micro web apps framework
 ˜
NAO!
    N˜o apropriado para produc˜o, software
      a                       ¸a
    comercial, ...
    N˜o apropriado para aplicac˜es complexas
      a                       ¸o
    N˜o est´ bem documentado (ainda)
      a    a
$ mojolicious generate lite_app
  [exist] ~/folder
  [write] ~/folder/myapp.pl
  [chmod] myapp.pl 744
$ myapp.pl
  ...
  generate         Generate files and directories from templates.
  routes           Show available routes.
  inflate          Inflate embedded files to real files.
  version          Show versions of installed modules.
  daemon_prefork   Start application w/prefork HTTP 1.1 backend.
  fastcgi          Start application with FastCGI backend.
  daemon           Start application with HTTP 1.1 backend.
  cgi              Start application with CGI backend.
  get              Get file from URL.
  psgi             Start application with PSGI backend.
  test             Run unit tests.
$ cat myapp.pl
#!/usr/bin/env perl
use Mojolicious::Lite;

get ’/’ => ’index’;
get ’/:groovy’ => sub {
    my $self = shift;
    $self->render_text($self->param(’groovy’), layout => ’funky’);
};
shagadelic;
__DATA__
@@ index.html.ep
% layout ’funky’;
Yea baby!

@@ layouts/funky.html.ep
<!doctype html><html>
    <head><title>Funky!</title></head>
    <body><%== content %></body>
</html>
$ cat myapp.pl
#!/usr/bin/env perl
use Mojolicious::Lite;

get ’/’ => ’index’;
get ’/:groovy’ => sub {
    my $self = shift;
    $self->render_text($self->param(’groovy’), layout => ’funky’);
};
shagadelic;
__DATA__
@@ index.html.ep
% layout ’funky’;
Yea baby!

@@ layouts/funky.html.ep
<!doctype html><html>
    <head><title>Funky!</title></head>
    <body><%== content %></body>
</html>
$ cat myapp.pl
#!/usr/bin/env perl
use Mojolicious::Lite;

get ’/’ => ’index’;
get ’/:groovy’ => sub {
    my $self = shift;
    $self->render_text($self->param(’groovy’), layout => ’funky’);
};
app->start;
__DATA__
@@ index.html.ep
% layout ’funky’;
Yea baby!

@@ layouts/funky.html.ep
<!doctype html><html>
    <head><title>Funky!</title></head>
    <body><%== content %></body>
</html>
Routes
   get ’/’ => ’index’ # GET ’/’
   get ’/:groovy’ => sub
   # GET ’/anyword’
Routes
   get ’/’ => ’index’ # GET ’/’
   get ’/:groovy’ => sub
   # GET ’/anyword’
   get ’/perl’ # GET ’/perl’
   any [qw/post delete/] ’/bar/:foo’
   # ’ POST or DELETE ’/bar/*’
   any ’/:foo’ => [foo => qr/+/]
                             .
   # ’/2010’
Routes
   get ’/’ => ’index’ # GET ’/’
   get ’/:groovy’ => sub
   # GET ’/anyword’
   get ’/perl’ # GET ’/perl’
   any [qw/post delete/] ’/bar/:foo’
   # ’ POST or DELETE ’/bar/*’
   any ’/:foo’ => [foo => qr/+/]
                             .
   # ’/2010’
   get ’/number/:num’ => {num => 42}
   get ’/path/(*everything)
Templates



   Sistema de templates pr´prio; ou
                          o
   Template Toolkit, ...
Exemplos
http://d.hatena.ne.jp/yukikimoto/20100220/1266588242



      Short message BSS (150 loc)
      Image BSS (156 loc)
      Simple search (106 loc)
      Simple real time clock (133 loc)
      Simple real time chat (207 loc)
$ myapp.pl inflate


 [mkdir]   ~/folder/templates/layouts
 [write]   ~/folder/templates/layouts/funky.html.
 [exist]   ~/folder/templates
 [write]   ~/folder/templates/index.html.ep
Trabalho semelhante
   Sinatra on Perl,
   github.com/jtarchie/
   sinatra-on-perl
   Schenker,
   github.com/spiritloose/Schenker
   Dancer,
   perldancer.org
Trabalho semelhante
   Sinatra on Perl,
   github.com/jtarchie/
   sinatra-on-perl
   Schenker,
   github.com/spiritloose/Schenker
   Dancer,
   perldancer.org

Mojolicious::Lite vs Dancer
http://use.perl.org/~Alias/journal/
Questions




            o/

Mojolicious lite

  • 1.
    Mini aplicac˜es webcom ¸o Mojolicious::Lite Andr´ Santos e andrefs@cpan.org PtPW 2010 5 de Julho
  • 2.
    Disclaimer [Web] developer profissional
  • 3.
    Disclaimer [Web] developer profissional Perl::Newbie
  • 4.
    Disclaimer [Web] developer profissional Perl::Newbie #{coisas certas} > #{coisas erradas}
  • 5.
  • 6.
  • 7.
    Ruby Ruby on Rails “opensource MVC web app framework” Perl
  • 8.
    Ruby Ruby on Rails “opensource MVC web app framework” Perl Catalyst (outros)
  • 9.
  • 10.
  • 11.
    Ruby Sinatra pequenas aplicac˜es web ¸o Perl Mojolicious::Lite “micro web framework”
  • 12.
    /Mojo.*/ Mojo Mojolicious Mojolicious::Lite
  • 13.
    /Mojo.*/ Mojo Mojolicious Mojolicious::Lite criados por Sebastian Riedel tamb´m criador de Catalyst e developer RoR
  • 14.
    /Mojo.*/ .---------------------------------------------------------------. | Fun! | ’---------------------------------------------------------------’ .---------------------------------------------------------------. | | | .----------------------------------------------’ | | .--------------------------------------------. | Application | | Mojolicious::Lite | | | ’--------------------------------------------’ | | .--------------------------------------------. | | | Mojolicious | ’----------------’ ’--------------------------------------------’ .---------------------------------------------------------------. | Mojo | ’---------------------------------------------------------------’ .-------. .-----------. .--------. .------------. .-------------. | CGI | | FastCGI | | PSGI | | HTTP 1.1 | | WebSocket | ’-------’ ’-----------’ ’--------’ ’------------’ ’-------------’
  • 15.
    /Mojo.*/ Mojo “Thebox!” Encapsula as complexidades de CGI, FastCGI, PSGI, HTTP e WebSocket num s´ m´todo o e D´ suporte a frameworks web como a Mojolicious e Mojolicious::Lite
  • 16.
    /Mojo.*/ Mojolicious “The web in a box!” framework MVC com API OO Perl depende apenas de Perl 5.8.1
  • 17.
    Mojolicious::Lite Single file microweb apps framework ˜ NAO! N˜o apropriado para produc˜o, software a ¸a comercial, ...
  • 18.
    Mojolicious::Lite Single file microweb apps framework ˜ NAO! N˜o apropriado para produc˜o, software a ¸a comercial, ... N˜o apropriado para aplicac˜es complexas a ¸o
  • 19.
    Mojolicious::Lite Single file microweb apps framework ˜ NAO! N˜o apropriado para produc˜o, software a ¸a comercial, ... N˜o apropriado para aplicac˜es complexas a ¸o N˜o est´ bem documentado (ainda) a a
  • 20.
    $ mojolicious generatelite_app [exist] ~/folder [write] ~/folder/myapp.pl [chmod] myapp.pl 744 $ myapp.pl ... generate Generate files and directories from templates. routes Show available routes. inflate Inflate embedded files to real files. version Show versions of installed modules. daemon_prefork Start application w/prefork HTTP 1.1 backend. fastcgi Start application with FastCGI backend. daemon Start application with HTTP 1.1 backend. cgi Start application with CGI backend. get Get file from URL. psgi Start application with PSGI backend. test Run unit tests.
  • 21.
    $ cat myapp.pl #!/usr/bin/envperl use Mojolicious::Lite; get ’/’ => ’index’; get ’/:groovy’ => sub { my $self = shift; $self->render_text($self->param(’groovy’), layout => ’funky’); }; shagadelic; __DATA__ @@ index.html.ep % layout ’funky’; Yea baby! @@ layouts/funky.html.ep <!doctype html><html> <head><title>Funky!</title></head> <body><%== content %></body> </html>
  • 22.
    $ cat myapp.pl #!/usr/bin/envperl use Mojolicious::Lite; get ’/’ => ’index’; get ’/:groovy’ => sub { my $self = shift; $self->render_text($self->param(’groovy’), layout => ’funky’); }; shagadelic; __DATA__ @@ index.html.ep % layout ’funky’; Yea baby! @@ layouts/funky.html.ep <!doctype html><html> <head><title>Funky!</title></head> <body><%== content %></body> </html>
  • 23.
    $ cat myapp.pl #!/usr/bin/envperl use Mojolicious::Lite; get ’/’ => ’index’; get ’/:groovy’ => sub { my $self = shift; $self->render_text($self->param(’groovy’), layout => ’funky’); }; app->start; __DATA__ @@ index.html.ep % layout ’funky’; Yea baby! @@ layouts/funky.html.ep <!doctype html><html> <head><title>Funky!</title></head> <body><%== content %></body> </html>
  • 24.
    Routes get ’/’ => ’index’ # GET ’/’ get ’/:groovy’ => sub # GET ’/anyword’
  • 25.
    Routes get ’/’ => ’index’ # GET ’/’ get ’/:groovy’ => sub # GET ’/anyword’ get ’/perl’ # GET ’/perl’ any [qw/post delete/] ’/bar/:foo’ # ’ POST or DELETE ’/bar/*’ any ’/:foo’ => [foo => qr/+/] . # ’/2010’
  • 26.
    Routes get ’/’ => ’index’ # GET ’/’ get ’/:groovy’ => sub # GET ’/anyword’ get ’/perl’ # GET ’/perl’ any [qw/post delete/] ’/bar/:foo’ # ’ POST or DELETE ’/bar/*’ any ’/:foo’ => [foo => qr/+/] . # ’/2010’ get ’/number/:num’ => {num => 42} get ’/path/(*everything)
  • 27.
    Templates Sistema de templates pr´prio; ou o Template Toolkit, ...
  • 28.
    Exemplos http://d.hatena.ne.jp/yukikimoto/20100220/1266588242 Short message BSS (150 loc) Image BSS (156 loc) Simple search (106 loc) Simple real time clock (133 loc) Simple real time chat (207 loc)
  • 29.
    $ myapp.pl inflate [mkdir] ~/folder/templates/layouts [write] ~/folder/templates/layouts/funky.html. [exist] ~/folder/templates [write] ~/folder/templates/index.html.ep
  • 30.
    Trabalho semelhante Sinatra on Perl, github.com/jtarchie/ sinatra-on-perl Schenker, github.com/spiritloose/Schenker Dancer, perldancer.org
  • 31.
    Trabalho semelhante Sinatra on Perl, github.com/jtarchie/ sinatra-on-perl Schenker, github.com/spiritloose/Schenker Dancer, perldancer.org Mojolicious::Lite vs Dancer http://use.perl.org/~Alias/journal/
  • 32.