[AC::PM] Mojolicious::UserAgent Slides

Michael Greb m at mikegrb.pm
Thu Jul 19 16:47:17 PDT 2012


Hey guys, I didn't forget to send my slides this time!

# This is a sample Vroom input file. It should help you get started.
#
# Edit this file with your content. Then run `vroom --vroom` to start
# the show!
#
# See `perldoc Vroom` for complete details.
#
---- config
# Basic config options.
title: mojolicious the client strikes back
indent: 5
#height: 18
#width: 69
auto_size: 1
skip: 0

# The following options are for Gvim usage.
# vim: mvim
# gvimrc: |
#   set fuopt=maxhorz,maxvert
#   set guioptions=egmLtT
#   set guifont=Bitstream_Vera_Sans_Mono:h18
#   set guicursor=a:blinkon0-ver25-Cursor
#   colorscheme default

---- center
mojolicious
the client strikes back

by @mikegrb

----
== mojolicious

* A next generation web framework for the Perl programming language.
* The web, in a box
  * zero dependencies
  * JSON encoder/decoder
  * HTTP Client

----
== Mojo::UserAgent
* HTTP Client with mojolicious's everything included approach
* DOM parser w/ CSS Selectors
* JSON decoder

----
== A quick diversion

CSS Selectors

In CSS, pattern matching rules determine which style
rules apply to elements in the document tree. These
patterns, called selectors, may range from simple
element names to rich contextual patterns.

----
== Groking CSS Selectors

* http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/
  * http://j.mp/LyrUhB
* http://www.selectorgadget.com/

----
== Select by ID

* '#X'
* <div id="x">

----
== Select by Class

* '.x'
* <div class="x">

----
== Select by Tag
* 'x'
* <x>urmom</x>

----
== Descendant
* x y
* <x>
    <y></y>
  </x>

---- perl
use 5.010;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new(max_redirects => 5);
say $ua->get("metacpan.org/module/Vroom")
    ->res()
    ->dom("#index a")
    ->pluck("text")
    ->map(sub{ return ucfirst(lc(shift)) } )
    ->join("\n");

----
== JSON

* pretty popular for webservice APIs these days
* mojo has your back

---- perl
use 5.010;
use Mojo::UserAgent;
binmode STDOUT, ':utf8';

my $ua = Mojo::UserAgent->new();
my $t = $ua->get("search.twitter.com/search.json?q=perl")
    ->res()->json->{results};
say join "\n\n",
    map { "\@$_->{from_user} $_->{text}" }
    @$t[0 .. 4];

----
== Debugging Mojo::UserAgent

Really easy, set MOJO_USERAGENT_DEBUG=1:

$ MOJO_USERAGENT_DEBUG=1 ./myscript.pl

----
== CLI?

This stuff is sweet, I could use it from CLI
all the time!
----
== mojo's got your back
ojo.pm
Let's you do:

$ perl -Mojo -E ...

---- shell
perl -Mojo -E 'say g("metacpan.org/search?q=mojo")->dom("big
a")->pluck("text")->join("\n")'

----
== it get's better!
* if you act now, mojo(1) will be included
  at no extra charge

---- shell
mojo get -r  'metacpan.org/search?q=mojo' 'big a' text

---- shell
mojo get www.linode.com 'head > title' text

---- shell
mojo get -r metacpan.org/module/Vroom '#index a' text


More information about the AtlanticCity-PM mailing list