This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use 5.16.1; | |
| use Mojo::UserAgent; | |
| # Fetch web site | |
| my $ua = Mojo::UserAgent->new; | |
| my $tx = $ua->get('mojolicio.us/perldoc'); | |
| # Extract title | |
| say 'Title: ', $tx->res->dom->at('head > title')->text; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Mojolicious::Plugin::Pipeline::CSSCompressor; | |
| use Mojo::Base 'Mojolicious::Plugin'; | |
| use CSS::Compressor 'css_compress'; | |
| sub register { | |
| my ($self, $app) = @_; | |
| # Register "css_compressor" filter | |
| $app->filter(css_compressor => sub { css_compress shift }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use warnings; | |
| use strict; | |
| use feature ':5.10'; | |
| use B; | |
| # Type and flags for reference | |
| my $obj = B::svref_2object(\(my $dummy = !!1)); | |
| my $TYPE = $obj->SvTYPE; | |
| my $FLAGS = $obj->FLAGS; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Mojolicious::Plugin::GZip; | |
| use Mojo::Base 'Mojolicious::Plugin'; | |
| use IO::Compress::Gzip 'gzip'; | |
| # Just set "gzip => 1" in the stash and it will try to compress your content | |
| sub register { | |
| my ($self, $app) = @_; | |
| $app->hook( | |
| after_dispatch => sub { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Mojolicious::Lite::GetPost; | |
| use Mojo::Base -base; | |
| use Mojo::Util 'monkey_patch'; | |
| sub import { | |
| my $caller = caller; | |
| monkey_patch $caller, 'get_post', sub { | |
| return $caller->app->routes->any([qw(GET POST)] => @_); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Mojolicious::Lite::MyKeywords; | |
| use Mojo::Base -base; | |
| use Mojo::Util 'monkey_patch'; | |
| sub import { | |
| my $caller = caller; | |
| # Export "get_post" keyword (reuse "any" keyword to make "under" work) | |
| monkey_patch $caller, 'get_post', sub { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use Mojolicious::Lite; | |
| use Mango; | |
| use Mango::BSON ':bson'; | |
| my $uri = 'mongodb://<user>:<pass>@<server>/<database>'; | |
| helper mango => sub { state $mango = Mango->new($uri) }; | |
| # Store and retrieve information non-blocking | |
| get '/' => sub { | |
| my $self = shift; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Open 10k concurrent WebSocket connections with the client and server running | |
| # in the same process and perform 10k WebSocket message roundtrips | |
| # | |
| # Tested on OS X 10.9.2 (MacBook Air) with Perl 5.18.2 and EV 4.17 (kqueue) | |
| # | |
| # 560.9MB memory usage, 0% CPU usage once roundtrips are finished after 36s | |
| # (with all 20k sockets still open) | |
| # | |
| # Get the latest version of Mojolicious from http://github.com/kraih/mojo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use Mojolicious::Lite; | |
| use Coro; | |
| use Mojo::IOLoop; | |
| # Magical class for calling a method non-blocking (with a closure) and | |
| # rescheduling the current coroutine until it is done | |
| package with::coro { | |
| use Coro; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Mojolicious::Plugin::Coro; | |
| use Mojo::Base 'Mojolicious::Plugin'; | |
| use Coro; | |
| use Mojo::IOLoop; | |
| # Wrap application in coroutine and reschedule main coroutine in event loop | |
| sub register { | |
| my ($self, $app) = @_; | |
| my $subscribers = $app->plugins->subscribers('around_dispatch'); |