Mojo as a HTTP
  A new beginning
I am Marcus
 I work here
In the beginning
 there was LWP
First Changelog Entry:

Thu 18 May 1995   Martijn Koster
<m.koster@nexor.co.uk>

 o Mentioned on libwww-perl that I had
changed the classes around lots.
Became wildly popular,
  default choice for
    HTTP in Perl.
Starting to
show it’s age
Clunky API
  No Async
No Web Socket
     ...
The time is ripe for
 a new beginning.
perl -Mojo -e'
g("vg.no/")
->dom(".article-content h3
a")
->each(sub {
b(shift->text)
->decode->say })'
Actually, it didn’t
happen just like that.
Sebastian Riedel
    @kraih
Took over Maypole.
 Wanted to make
  radical changes
Got
Booted
Created Catalyst.
Wanted to make
 radical changes
Got
Booted
Made Mojolicious.
  Wanted to target
PHP developers & Perl
         6
Ease of install/porting
          ➜
   Only Perl5 core
    dependencies
Bootstrapping:
 curl -L cpanmin.us |
sudo perl - Mojolicious
Wrote an async
   HTTP 1.1
compliant stack
Mojo::Transaction
Mojo::Message::Response
      Mojo::Cookie
       Mojo::URL
         +++
Test Driven
Development
RFC Driven
Development
PRAGMATIC
Next logical step:
Add a client on top of
     this stack.
Mojo == Lego
Built from reusable
       bricks.
Parallel
requests
my $callback = sub {

 print shift−>res−>body
};
$client−>get('http://mojolicious.org' =>
$callback);
$client−>get('http://search.cpan.org' =>
$callback);
$client−>start;
Form Handling
# Form post with exception handling
my $cpan = 'http://search.cpan.org/search';
my $search = {q => 'mojo'};
my $tx = $client−>post_form($cpan => $search);
if (my $res = $tx−>success) {
   print $res−>body
} else {
   my ($message, $code) = $tx−>error;
   print "Error: $message";
}
Web Sockets
$client−>websocket(
'ws://websockets.org:8787' =>
sub {
  my $client = shift;
  $client−>on_message( sub {
    my ($client, $message) = @_;
    print "$messagen";
    $client−>finish;
  });
  $client−>send_message('hiya!');
})−>start;
Oneliners
Collection of single
 letter commands.
g ➜ get
  d ➜ delete
f ➜ form post
   p ➜ post
    u ➜ put
w ➜ websocket
Special Cases
b ➜ byte stream,
  a ➜ Lite app
#ojo module
my $res = g( 'http://mojolicio.us',
         {'Content−Type' => 'text/plain'},
         'Hello!' );

perl -Mojo -e 'b(g("mojolicio.us")->dom->at("title")->text)->say'

perl -Mojo -E'g("bloomberg.com")->dom("a.story_link")->each(sub { say
shift->text; })'

perl -Mojo -E'g("digg.com")->dom("a.story-title")->each(sub { say pop, ". ",
shift->text })'

# Undocumented o(fun) Mojolicious cloud function:
perl -Mojo -e 'oO("http://www.reddit.com")->dom->find("a.title")-
>each(sub { Oo(pop . ". " . shift->text)->say })'
Mojo::DOM
Liberal XML Parser
Supports all CSS3
  selectors that
   make sense
*
 E[foo=”bar”]
   E:checked
    E:empty
  E:nth-child
E:first-of-type
    E:not(s)
       EF
      E>F
     ++++
o(fun)
If you need performance
       use libXML
SOME RANDOM
    BITS
Supports IPV6,
TLS,proxies, epoll,
     kqueue
DEBUG MODE
    ENV VARIABLE
MOJO_CLIENT_DEBUG=1 
   perl -Mojo -E’...’
# Streaming response
my $tx = $client−>build_tx(GET =>
       'http://mojolicious.org');
$tx−>res−>body(sub { print $_[1] });
$client−>start($tx);

# Custom socket
my $tx = $client−>build_tx(GET =>
       'http://mojolicious.org');
$tx−>connection($socket);
$client−>start($tx);
Big Body
  Just Works
(temp storage on
   filesystem)
$res->json
Automatic JSON
deserialization
o(fun)
Every file in the distro
  has a Simpsons or
   Futurama quote.
Now also handles
  disconnected keep-alive
         gracefully.
(this was not the case when
     we started using it).
Test Driven
Development
You provide
failing test. kraih
   provides fix.
Learn More Mojo
           mojolicious.org
      github.com/kraih/mojo
         #mojo on irc.perl.org

groups.google.com/group/mojolicious

Mojo as a_client

Editor's Notes

  • #5 Much of this development was driven by Gisle Aas++ (Oslonett)
  • #8 Hard to update, because so much depend on the current broken behavior
  • #12 at Twitter
  • #13 Maypole was originally developed by Simon Cozens, but he fucked off to become a missionary in Japan.
  • #15 Catalyst had gathered steam. Core team of developers
  • #19 Runs in 30 seconds (time to download, run test suite and install files).
  • #21 All
  • #24 Tell about nginx and status phrases.