Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Is Mojolicious web only?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Reinventing the wheel

Slide 5

Slide 5 text

Parser for ... JSON, HTML, HTTP

Slide 6

Slide 6 text

Object orientation

Slide 7

Slide 7 text

Moose, Mouse, Mu, Moo, Mo

Slide 8

Slide 8 text

Mojo::Base -base

Slide 9

Slide 9 text

Slide 10

Slide 10 text

#!/usr/bin/env perl use Mojolicious::Lite; push @{ app->plugins->namespaces }, 'PerlServices::Plugin'; plugin 'Ticket' => app->config->{otrs}; app->defaults( bodyclass => 'start', active => 'home', ); get '/' => 'static/index'; get '/imprint' => 'static/imprint'; get '/privacy' => 'static/privacy'; get '/jobs' => 'static/jobs'; under '/otrs' => sub { shift->stash(bodyclass => 'otrs', active => 'otrs'); return 1; }; get '/' => 'static/otrs'; get '/support' => 'static/otrs-support'; get '/development' => 'static/otrs-development'; under '/perl' => sub { shift->stash(bodyclass => 'perl', active => 'perl'); return 1; } get '/' => 'static/perl'; under '/academy' => sub { shift->stash(bodyclass => 'schulung',active => 'academy'); return 1; }; get '/' => 'static/academy'; under '/' => sub { my $c = shift; $c->stash( errors => {}, values => {}, ); return 1; }; get '/kontakt.cgi' => 'contact/form'; post '/kontakt.cgi' => \&_send_contact_request; post '/get-in-touch' => \&_send_contact_request; under '/'; any '/*anything' => sub { my $c = shift; $c->redirect_to( '/' ); }; app->start;

Slide 11

Slide 11 text

Mojo == Web ?

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

$ perl -Mojo -E ' say g("https://act.yapc.eu/gpw2019/talk/7601") ->dom ->at("#talk_abstract") ->text '

Slide 15

Slide 15 text

$ perl -Mojo -E ' say g("https://act.yapc.eu/gpw2019/talk/7601") ->dom ->at("#talk_abstract") ->text '

Slide 16

Slide 16 text

$ perl -Mojo -E ' say g("https://act.yapc.eu/gpw2019/talk/7601") ->dom ->at("#talk_abstract") ->text ' Mojolicious ist ein tolles Webframework f�r Perl. Aber kann es auch mehr? Ein paar Anwendungsf�lle werde ich im Vortrag zeigen...

Slide 17

Slide 17 text

$ perl -Mojo -E ' say p("https://api.hetzner.cloud/v1/servers" => { Authorization => "Bearer " } => json => { name => "Testserver$$", server_type => "cx11", location => "nbg1", image => "ubuntu-18.04" }) ->json("/server/public_net/ipv4/ip")' 116.203.66.1

Slide 18

Slide 18 text

use Mojo::DOM; use Mojo::File qw(path); my $file = path('./Badge-4er-tmpl.pure.svg'); my @user_pages = _get_gpw_users(); my $page_nr = 0; for my $page ( @user_pages ) { my $dom = Mojo::DOM->new( $file->slurp ); for my $i ( 0 .. 3 ) { say sprintf "Set name %s...", $name; $dom->find('#firstname-' . ($i+1) )->first->content( $firstname ); $dom->find('#lastname-' . ($i+1) )->first->content( $page->[$i]->[2] ); } path('./Badges/Badge.page' . ++$page_nr . '.svg')->spurt( "$dom" ); }

Slide 19

Slide 19 text

use Mojo::DOM; use Mojo::File qw(path); my $file = path('./Badge-4er-tmpl.pure.svg'); my @user_pages = _get_gpw_users(); my $page_nr = 0; for my $page ( @user_pages ) { my $dom = Mojo::DOM->new( $file->slurp ); for my $i ( 0 .. 3 ) { say sprintf "Set name %s...", $name; $dom->find('#firstname-' . ($i+1) )->first->content( $firstname ); $dom->find('#lastname-' . ($i+1) )->first->content( $page->[$i]->[2] ); } path('./Badges/Badge.page' . ++$page_nr . '.svg')->spurt( "$dom" ); }

Slide 20

Slide 20 text

use Mojo::DOM; use Mojo::File qw(path); my $file = path('./Badge-4er-tmpl.pure.svg'); my @user_pages = _get_gpw_users(); my $page_nr = 0; for my $page ( @user_pages ) { my $dom = Mojo::DOM->new( $file->slurp ); for my $i ( 0 .. 3 ) { say sprintf "Set name %s...", $name; $dom->find('#firstname-' . ($i+1) )->first->content( $firstname ); $dom->find('#lastname-' . ($i+1) )->first->content( $page->[$i]->[2] ); } path('./Badges/Badge.page' . ++$page_nr . '.svg')->spurt( "$dom" ); }

Slide 21

Slide 21 text

CLI

Slide 22

Slide 22 text

$ perl -Mojo -E ' say g("https://act.yapc.eu/gpw2019/talk/7601") ->dom ->at("#talk_abstract") ->text ' Mojolicious ist ein tolles Webframework f�r Perl. Aber kann es auch mehr? Ein paar Anwendungsf�lle werde ich im Vortrag zeigen...

Slide 23

Slide 23 text

$ mojo get https://act.yapc.eu/gpw2019/talk/7601 \ '#talk_abstract' text Mojolicious ist ein tolles Webframework für Perl. Aber kann es auch mehr? Ein paar Anwendungsfälle werde ich im Vortrag zeigen...

Slide 24

Slide 24 text

$ mojo get https://act.yapc.eu/gpw2019/talks 'td > a[href^="/gpw2019/talk"] > b' text Supercharging Math Modules with Databases WebPerl - Perl im Browser! und goto ist sooo nützlich Eine nützliche Quelle für Perl-Code: MySQL Workbench Mojolicious nur für's Web? Schaun wir mal... Darf der das? Regelbasierte Autorisierung in Catalyst Parsen mit Zuversicht Finding humans to turn into developers Gitlab und Perl – Ein Erfahrungsbericht II Mein Jahr mit der DSGVO Moving Mountains With Perl Emacs für Perl-Sourcen: Kleine Tricks Willkommen in München - Welcome to Munich Auf Wiedersehen - vielleicht beim 22. Deutschen Perl-Workshop? Einführung in Encoding mit Perl Was ich beim Advent Of Code 2018 gelernt habe Vue.js for Perl hackers Deploying Perl Apps using Docker, Gitlab & Kubernetes Hacker haben unsere Login-Daten. Was tun wir? Du kannst den Planeten retten. Wie wäre es mit heute Nachmittag? Fun with Macros The Camel Paradox Was ich über SQL gelernt habe Git Tips & Tricks Perl meetup HOWTO session TPF Update node.js in perl - event driven programming including promises via perl/POE static linking of perl OBS - Streamline the delivery of your perl project Perl 6 one-liners Creating a compiler in Perl 6 Perl und SSL für Fortgeschrittene Gatekeepers Perl erweitern mit Nim Freifunk - was ist das und wie entsteht die Software Escape analysis and related optimizations for Perl 6 Understanding react, supply, and whenever PullRequest Club Neuigkeiten von Coocook.org Perl at the Chaos Communication Congress Join all the things with DBIC Scanley Cybrick - APT für jedermann EPO Recording Kits Debugging ist bunt! PerlCon 2019

Slide 25

Slide 25 text

mojo version mojo generate lite_app ...

Slide 26

Slide 26 text

App::Cmd MooX::Cmd ...

Slide 27

Slide 27 text

Mojolicious::Commands

Slide 28

Slide 28 text

gpw add 2019 gpw update 2019 gpw show 2019 gpw talks 2019 gpw talk 7106

Slide 29

Slide 29 text

WIP

Slide 30

Slide 30 text

#!/usr/bin/perl use Mojolicious::Lite; app->plugin( JSONConfig => { file => app->home->child('..', 'conf', 'gpw.json'), }); app->plugins->namespaces(['App::GPW::Plugin']); app->commands->namespaces(['App::GPW::Cmd']); app->plugin( 'Utils' ); Mojo::IOLoop->start if !Mojo::IOLoop->is_running; my $gpw = $app;

Slide 31

Slide 31 text

package App::GPW; use Mojo::Base 'Mojolicious', -strict; sub startup { my $app = shift; $app->plugin( JSONConfig => { file => $app->home->child('conf', 'gpw.json'), }); $app->plugins->namespaces(['App::GPW::Plugin']); $app->commands->namespaces(['App::GPW::Cmd']); $app->plugin( 'Utils' ); } 1;

Slide 32

Slide 32 text

package App::GPW; use Mojo::Base 'Mojolicious', -strict; sub startup { my $app = shift; $app->plugin( JSONConfig => { file => $app->home->child('conf', 'gpw.json'), }); $app->plugins->namespaces(['App::GPW::Plugin']); $app->commands->namespaces(['App::GPW::Cmd']); $app->plugin( 'Utils' ); } 1;

Slide 33

Slide 33 text

package App::GPW::Plugin::Utils; use Mojo::Base 'Mojolicious::Plugin', -signatures; # … infrastructure code … sub register ( $self, $app, $config ) { for my $attr ( qw(...) ) { next if !$config->{$attr}; $self->$attr( $config->{$attr} ); } $app->helper( 'gpw.directory' => sub { $self->_directory( @_ ); } ); $app->helper( 'gpw.workshop' => sub { $self->_new_workshop( @_ ); } ); } 1;

Slide 34

Slide 34 text

package App::GPW::Plugin::Utils; use Mojo::Base 'Mojolicious::Plugin', -signatures; # … infrastructure code … sub register ( $self, $app, $config ) { for my $attr ( qw(...) ) { next if !$config->{$attr}; $self->$attr( $config->{$attr} ); } $app->helper( 'gpw.directory' => sub { $self->_directory( @_ ); } ); $app->helper( 'gpw.workshop' => sub { $self->_new_workshop( @_ ); } ); } 1;

Slide 35

Slide 35 text

package App::GPW; use Mojo::Base 'Mojolicious', -strict; sub startup { my $app = shift; $app->plugin( JSONConfig => { file => $app->home->child('conf', 'gpw.json'), }); $app->plugins->namespaces(['App::GPW::Plugin']); $app->commands->namespaces(['App::GPW::Cmd']); $app->plugin( 'Utils' ); } 1;

Slide 36

Slide 36 text

package App::GPW::Cmd::add; use Mojo::Base 'Mojolicious::Command', -signatures; use Try::Tiny; sub run ($self, @args) { my $app = $self->app; my $year = shift @args; $app->log->debug( "Add $year workshop" ); my $error; try { $app->gpw->workshop( $year ); } catch { $error = $_; $app->log->warn( "Cannot add gpw: $_" ); }; return 0 if $error; return 1; } 1;

Slide 37

Slide 37 text

$ perl bin/gpw add 2018 [2019-03-04 19:33:25.42306] [10000] [debug] Add 2018 workshop [2019-03-04 19:33:25.42345] [10000] [debug] Get list of talks... [2019-03-04 19:33:25.44091] [10000] [debug] Create directory for 2018 [2019-03-04 19:33:25.44129] [10000] [debug] Selector: /gpw2018 [2019-03-04 19:33:25.44219] [10000] [debug] Get event info from github [2019-03-04 19:33:25.44227] [10000] [debug] URL: https://r... [2019-03-04 19:33:26.16024] [10000] [debug] { "end" => "2018-04-07 17:00:00", "name" => "Deutscher Perl-Workshop 2018", "start" => "2018-04-03 09:00:00" } [2019-03-04 19:33:27.75339] [10000] [debug] Get talk 7271... [2019-03-04 19:33:27.75420] [10000] [debug] Get talk 7272... [2019-03-04 19:33:27.75457] [10000] [debug] Get talk 7280... [2019-03-04 19:33:27.75490] [10000] [debug] Get talk 7277... [2019-03-04 19:33:27.75522] [10000] [debug] Get talk 7299… $

Slide 38

Slide 38 text

$ MOJO_MODE=production perl bin/gpw add 2018 $

Slide 39

Slide 39 text

Mojo „infrastructure“

Slide 40

Slide 40 text

package App::GPW::Plugin::Utils; use Mojo::Base 'Mojolicious::Plugin', -signatures; use Mojo::File qw(path); use Mojo::JSON qw(encode_json); use Mojo::UserAgent; use Config::Tiny; use File::HomeDir; has base_url => sub { 'https://act.yapc.eu/' }; has github_url => sub { 'https://raw.githubusercontent.com/Act-Conferences/' }; has conference => sub { 'gpw' }; has talks_path => sub { '/talks' }; has talk_path => sub { '/talk' };

Slide 41

Slide 41 text

sub _get_act_info_from_github ($self, $c, $year ) { my $url = … ; my $app = $c->app; $app->log->debug( "Get event info from github" ); $app->log->debug( "URL: $url" ); $app->ua->get( $url => {} => sub { my ($ua, $tx) = @_; return if $tx->res->is_error; # ... $self->_directory( $c, $year )->child( 'conference.json' )->spurt( encode_json \%info ); }, ); return 1; }

Slide 42

Slide 42 text

sub _get_act_info_from_github ($self, $c, $year ) { my $url = … ; my $app = $c->app; $app->log->debug( "Get event info from github" ); $app->log->debug( "URL: $url" ); $app->ua->get( $url => {} => sub { my ($ua, $tx) = @_; return if $tx->res->is_error; # ... $self->_directory( $c, $year )->child( 'conference.json' )->spurt( encode_json \%info ); }, ); return 1; }

Slide 43

Slide 43 text

Reusable for web

Slide 44

Slide 44 text

package App::GPW::Controller::Conf; use Mojo::Base 'Mojolicious::Controller', -signatures; use Try::Tiny; sub list ($c) { my $confs = $c->gpw->list; $c->stash( confs => $confs ); return $c->render( 'index' ); } sub add ($c) { my $year = $c->param('year'); my $app = $c->app; my $success = $app->commands->run('add', $year); return $c->reply->exception if !$success; return $c->redirect_to( $c->url_for('conf-list') ); }

Slide 45

Slide 45 text

package App::GPW::Controller::Conf; use Mojo::Base 'Mojolicious::Controller', -signatures; use Try::Tiny; sub list ($c) { my $confs = $c->gpw->list; $c->stash( confs => $confs ); return $c->render( 'index' ); } sub add ($c) { my $year = $c->param('year'); my $app = $c->app; my $success = $app->commands->run('add', $year); return $c->reply->exception if !$success; return $c->redirect_to( $c->url_for('conf-list') ); }

Slide 46

Slide 46 text

Mojolicious is not web only...

Slide 47

Slide 47 text

Questions?

Slide 48

Slide 48 text

https://github.com/reneeb/App-GPW

Slide 49

Slide 49 text

Thank you!