Skip to content

Instantly share code, notes, and snippets.

@s1037989
s1037989 / state.pl
Created February 15, 2016 09:31
Demonstration of the use of a state variable in a Mojolicious app
#!/usr/bin/env perl
package A;
use Mojo::Base -base;
use 5.020;
use feature qw(signatures);
has [qw(a b)];
sub new {
--> Working on Exporter::Tiny
Fetching https://cpan.metacpan.org/authors/id/T/TO/TOBYINK/Exporter-Tiny-0.042.tar.gz ... OK
Configuring Exporter-Tiny-0.042 ... OK
Building Exporter-Tiny-0.042 ... OK
Successfully installed Exporter-Tiny-0.042
Building List-MoreUtils-0.413 ... OK
Successfully installed List-MoreUtils-0.413
Building CSS-Sass-v3.3.0 ... OK
Successfully installed CSS-Sass-v3.3.0
4 distributions installed
stefan@deskws:/tmp/convos$ git pull
Updating bbb9f29..ccfa6be
Fast-forward
script/convos | 42 ++++++++++++++++++++++++++++++------------
1 file changed, 30 insertions(+), 12 deletions(-)
stefan@deskws:/tmp/convos$ ./script/convos
---
@s1037989
s1037989 / gist:835469a14fd164cc6161
Created March 8, 2016 16:40
helpers in Mojo::Template
use 5.022;
use lib 'lib';
use B;
say B->new->render('a.txt');
package A;
use Mojo::Base -base;
use Mojo::Loader 'data_section';
@s1037989
s1037989 / Mojo::Sendgrid.pm
Last active March 23, 2016 22:03
Sendgrid API implementation for Mojo framework
package Mojo::Sendgrid;
use Mojo::Base 'Mojo::EventEmitter';
use Mojo::Sendgrid::Mail;
has config => sub { {} };
has apikey => sub { $ENV{SENDGRID_APIKEY} || shift->config->{apikey} or die "config apikey missing" };
has apiurl => sub { $ENV{SENDGRID_APIURL} || shift->config->{apiurl} || 'https://api.sendgrid.com/api/mail.send.json' };
sub mail { Mojo::Sendgrid::Mail->new(sendgrid => shift, @_) }
@s1037989
s1037989 / deskws
Created March 29, 2016 03:22
sortbench
$ for i in $(seq 1 5); do echo "$i - $(date)"; perl sortbench.pl; done
1 - Mon Mar 28 22:18:24 CDT 2016
timethis for 10: 11 wallclock secs (10.42 usr + 0.02 sys = 10.44 CPU) @ 35.63/s (n=372)
timethis for 10: 11 wallclock secs (10.56 usr + 0.00 sys = 10.56 CPU) @ 34.94/s (n=369)
2 - Mon Mar 28 22:18:49 CDT 2016
timethis for 10: 10 wallclock secs (10.33 usr + 0.04 sys = 10.37 CPU) @ 35.58/s (n=369)
timethis for 10: 11 wallclock secs (10.67 usr + 0.01 sys = 10.68 CPU) @ 34.55/s (n=369)
3 - Mon Mar 28 22:19:13 CDT 2016
timethis for 10: 11 wallclock secs (10.58 usr + 0.02 sys = 10.60 CPU) @ 35.47/s (n=376)
timethis for 10: 11 wallclock secs (10.57 usr + 0.01 sys = 10.58 CPU) @ 34.59/s (n=366)
14252 ? S 0:00 /home/ubuntu/workspace/ForkAndGo/eg/minion.pl
14258 ? S 0:00 \_ /home/ubuntu/workspace/ForkAndGo/eg/minion.pl
14261 ? Ss 0:00 | \_ /home/ubuntu/workspace/ForkAndGo/eg/minion.pl
14267 ? S 0:00 | | \_ /usr/bin/perl /home/ubuntu/workspace/ForkAndGo/eg/minion.pl minion worker
14277 ? S 0:00 | | \_ /usr/bin/perl /home/ubuntu/workspace/ForkAndGo/eg/minion.pl minion worker
14263 ? Ss 0:00 | \_ /home/ubuntu/workspace/ForkAndGo/eg/minion.pl
14265 ? S 0:00 | | \_ /usr/bin/perl /home/ubuntu/workspace/ForkAndGo/eg/minion.pl minion worker
14270 ? S 0:00 | | \_ /usr/bin/perl /home/ubuntu/workspace/ForkAndGo/eg/minion.pl minion worker
14264 ? Ss 0:00 | \_ /home/ubuntu/workspace/ForkAndGo/eg/minion.pl
14268 ? S 0:00 | | \_ /usr/bin/perl /home/ubuntu/workspace/ForkAndGo/eg/minion.pl minion worker
use Mojolicious::Lite;
use lib '../Mojolicious-Plugin-OAuth2-Fetch/lib';
use lib 'lib';
use Mojo::Pg;
use TimeClock::Model::Pg::Users;
use TimeClock::Model::Pg::Timeclock;
my $config = plugin 'Config';
package TimeClock::Model::OAuth2;
use Mojo::Base -base;
use UUID::Tiny ':std';
has 'pg';
sub store {
my $self = shift;
if ( $#_ == 1 ) {
@s1037989
s1037989 / ws
Created August 9, 2016 04:14
[Mojolicious] websocket route that polls a database
use Mojolicious::Lite;
use 5.20.0;
use experimental 'signatures';
use Mojo::IOLoop;
Mojo::IOLoop->recurring(1 => sub { warn scalar localtime });
# Render template "index.html.ep" from the DATA section
get '/' => {template => 'index'};