Skip to content

Instantly share code, notes, and snippets.

View TristinDavis's full-sized avatar

Tristin Davis TristinDavis

  • Thrivent Financial
  • Arkansas, USA
  • 09:45 (UTC -05:00)
View GitHub Profile
@TristinDavis
TristinDavis / Perl: Mojolicious
Created November 17, 2012 18:52
Flash/Stash example
use Mojo::ByteStream;
app->helper(alerts => sub {
my $c = shift;
my $html = '';
for my $message (qw|success info error|) {
my $css = "alert alert-$message";
if($c->flash($message)) {
$html .= $c->tag('div', class => $css, $c->flash($message));
@TristinDavis
TristinDavis / Perl: Redis
Created November 17, 2012 18:55
Example of using Redis for job queueing/workers
=pod
=head1 Redis jobqueue client
Redis has a feature called lists. The lists can be treated as a stack where
elements can be pushed onto or popped from. There are several calls which help
you to manage such lists. Once nice feature is the blpop command which does a
blocking call on the queue. Therefore it's a nice and elegant way to implement a
queue systems for tasks.
@TristinDavis
TristinDavis / perl: Mojolicious
Created November 17, 2012 18:56
RPM spec file for Mojolicious
Name: perl-Mojolicious
Version: 3.56
Release: 1%{?dist}
Summary: A next generation web framework for Perl
License: Artistic 2.0
Group: Development/Libraries
URL: http://mojolicious.org/
Source0: http://search.cpan.org/CPAN/modules/by-module/Mojolicous/Mojolicious-%{version}.tar.gz
BuildArch: noarch
BuildRequires: perl >= 0:5.010, perl(ExtUtils::MakeMaker)
@TristinDavis
TristinDavis / Perl: Mojolicious
Created November 17, 2012 18:57
Example of using logging in a Mojolicious::Lite/Mojolicious application.
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/works' => sub {
my $self = shift;
my $title = $self->ua->get('http://www.google.de')->res->dom->at('title')->text;
$self->render_text('Google title: ' . $title);
};
get '/ok' => sub {
@TristinDavis
TristinDavis / Mojo::IOLoop-coro-bench.pl
Created November 18, 2012 05:07 — forked from kraih/coro-bench.pl
An example showing how to use a Mojo::IOLoop.
#!/usr/bin/env perl
use Mojolicious::Lite;
use Coro;
use Mojo::IOLoop;
Mojo::IOLoop->recurring(0 => sub {cede});
hook around_dispatch => sub {
my $next = shift;
async { $next->() };
@TristinDavis
TristinDavis / CSSCompressor.pm
Created November 19, 2012 00:30 — forked from kraih/CSSCompressor.pm
Asset Pipeline Example
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 });
@TristinDavis
TristinDavis / Perl: Mojolicious
Created November 19, 2012 00:33 — forked from sshaw/gist:1984738
Mojolicious::Controller using CGI::Expand
use Mojo::Base 'Mojolicious';
use CGI::Expand;
sub startup
{
my $self = shift;
$self->hook(before_dispatch => sub {
my $c = shift;
my $hash = CGI::Expand->expand_hash($c->req->params->to_hash);
$c->param($_ => $hash->{$_}) for keys %$hash;
@TristinDavis
TristinDavis / MojoLite-hook_and_test.pl
Created November 19, 2012 00:35 — forked from bduggan/gist:1355071
A nice short example of using Test::Mojo and how to grab events.
#!/usr/bin/env perl
# put.t
use Test::More tests => 3;
use Mojolicious::Lite;
use Test::Mojo;
$ENV{MOJO_MAX_MEMORY_SIZE} = 2; # Force temp files.
$ENV{MOJO_TMPDIR} = "/nosuchdir"; # test setting tempdir dynamically
package Mojolicious::Plugin::MicroPipeline::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 });
@TristinDavis
TristinDavis / CSSCompressor.pm
Created November 19, 2012 00:41 — forked from kevinold/CSSCompressor.pm
Shows how to register filters and how to composite templates, css, and js files.
package Mojolicious::Plugin::MicroPipeline::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 });