Awaiting Mojolicious

Joel Berger

Purdue Perl Mongers

September 9, 2020

What is Mojolicious?

  • An amazing real-time web framework
  • A powerful web development toolkit
  • Designed from the ground up
  • ... based on years of experience developing Catalyst
  • Portable
  • No non-core dependencies
  • Batteries included
  • Real-time and non-blocking
  • "... perfect for building highly scalable web services"
  • ~8k lines of code in lib
  • ~11k tests (~93% coverage)
  • Easy to install (secure, only takes one minute!)
curl -L https://cpanmin.us | perl - -M https://cpan.metacpan.org -n Mojolicious

Getting Help

Let's begin

Async is a Hero's Journey

Oh and of course ...

Hello World!

ex/hello/world/app.pl

Hello World! Test

ex/hello/world/test.t

Hello World! Test

ex/hello/world/test.t

Hello World! Test

ex/hello/world/test.t

Hello World! Test

ex/hello/world/test.t

What actually happened?

  • text encoded with utf8
  • default content-type is text/html

Hello 🌐!

ex/hello/format/app.pl

Hello 🌐!

ex/hello/format/app.pl

Hello 🌐!

ex/hello/format/app.pl

Hello 🌐! Test

ex/hello/format/test.t

Meeting New Friends

Hello $user!

ex/hello/user/app.pl

Hello $user!

ex/hello/user/app.pl

Hello $user!

ex/hello/user/app.pl

Hello $user!

ex/hello/user/app.pl

Hello $user!

ex/hello/user/app.pl

Hello $user! Test

ex/hello/user/test.t

#HanShotFirst

Other Formats?

JSON

ex/hello/json/app.pl

JSON

ex/hello/json/app.pl

JSON Tests

ex/hello/json/test.t

JSON Tests

ex/hello/json/test.t

JSON Tests

ex/hello/json/test.t

JSON Tests

ex/hello/json/test.t

Templates

ex/hello/template/app.pl

Templates

ex/hello/template/app.pl

Templates

ex/hello/template/app.pl

CSS Selectors in Tests

ex/hello/template/test.t

CSS Selectors in Tests

ex/hello/template/test.t

Because we've come this far ...

Luke, you've turned off your targeting computer!

Content Negotiation

ex/hello/content_negotiation/app.pl

Content Negotiation

ex/hello/content_negotiation/app.pl

Content Negotiation

ex/hello/content_negotiation/app.pl

Testing Content Negotiation

ex/hello/content_negotiation/test.t

Testing Content Negotiation

ex/hello/content_negotiation/test.t

Other Ways to Negotiate

ex/hello/content_negotiation/test.t

Let's Build a Doc Fetching App

Goal:

Display Docs Fetched From MetaCPAN

Simple MetaCPAN App

ex/metacpan/simple/app.pl

Mocking An External Service

Aren't you a little short for a storm trooper?

Mocking An External Service

ex/metacpan/simple/test.t

How do we use this mock?

Mocking An External Service

  • Mojo::UserAgent
    • server for relative urls
    • accepts a Mojolicious app
  • Attach mock app to the main app's ua
  • MetaCPAN url is configurable!

ex/metacpan/simple/test.t

Test Simple MetaCPAN App

ex/metacpan/simple/test.t

Smart URL Manipulation

ex/metacpan/url/app.pl

Smart URL Manipulation

ex/metacpan/url/app.pl

Smart URL Manipulation

ex/metacpan/url/app.pl

Goal:

Fix Link URLs

Mojo::DOM in Action

Transforming HTML

An elegant weapon... for a more civilized age

Transforming HTML

ex/metacpan/munge_links/app.pl

Transforming HTML

ex/metacpan/munge_links/app.pl

Transforming HTML

ex/metacpan/munge_links/app.pl

Transforming HTML

ex/metacpan/munge_links/app.pl

Transforming HTML

ex/metacpan/munge_links/app.pl

Transforming HTML

ex/metacpan/munge_links/app.pl

Transforming HTML

ex/metacpan/munge_links/app.pl

Testing the Transformation

ex/metacpan/munge_links/test.t

Testing the Transformation

ex/metacpan/munge_links/test.t

Testing the Transformation

ex/metacpan/munge_links/test.t

Testing the Transformation

ex/metacpan/munge_links/test.t

Goal:

Add Distribution and ++ to Doc

We Need More Information from MetaCPAN

Its as Simple as ...

ex/metacpan/blocking/app.pl

Get Dist For Module

ex/metacpan/blocking/app.pl

ex/metacpan/blocking/test.t

Get Favorite Count For Dist

ex/metacpan/blocking/app.pl

ex/metacpan/blocking/test.t

Inject New HTML

ex/metacpan/blocking/app.pl

Inject New HTML

ex/metacpan/blocking/app.pl

Inject New HTML

ex/metacpan/blocking/app.pl

Full Application

ex/metacpan/blocking/app.pl

Full Test

ex/metacpan/blocking/test.t

Goal:

Make it Async

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Async/Await

ex/metacpan/async/app.pl

Full Application

ex/metacpan/async/app.pl

And the Tests ...

... are unchanged

Questions?

Command System

Start App

  • ./myapp.pl daemon
  • ./myapp.pl prefork

Inspect App

  • ./myapp.pl routes
  • ./myapp.pl eval

Add your own

  • ./myapp.pl migrate

Or get one from CPAN

  • ./myapp.pl minion worker
  • ./myapp.pl nopaste app.pl
  • ./myapp.pl export