Skip to content

Instantly share code, notes, and snippets.

View kraih's full-sized avatar

Sebastian Riedel kraih

View GitHub Profile
% mkdir hellomojo
% cd hellomojo
% touch myapp.pl
% touch Makefile.PL
% touch app.psgi
% dotcloud create hellomojo
...
% dotcloud deploy --type perl hellomojo.www
...
% dotcloud push hellomojo.www .
...
% dotcloud run hellomojo.www
...
# main.pl
use Mojolicious::Lite;
get '/' => sub { shift->render_text('ROOT!') };
under '/foo';
require 'foo.pl';
app->start;
use 5.14.0;
use Mojo::DOM;
# Let's start with something simple
my $dom = Mojo::DOM->new('<div id="a">A</div>');
# All child elements now have accessors you can use instead of CSS3 selectors
say $dom->div->text;
# You also get direct hash access to element attributes
% ./myapp.pl eval -v 'join "\n", sort keys %{app->renderer->helpers}'
app
base_tag
check_box
content
content_for
dumper
extends
file_field
flash
use 5.14.0;
use Mojo::Server;
# Load application with mock server
my $server = Mojo::Server->new;
my $app = $server->load_app('./myapp.pl');
# Access fully initialized application
say $app->static->paths->[0];
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
# Write two chunks right away
$self->write("first\n");
$self->write("second\n");
# Wait 3 seconds and write last chunk
set background=dark
hi clear
if exists("syntax_on")
syntax reset
endif
let colors_name = "kraihlight"
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
$ua->websocket('ws://localhost:3000/echo' => sub {
my $tx = pop;
$tx->on(finish => sub { Mojo::IOLoop->stop });
$tx->on(message => sub {
my ($tx, $message) = @_;
print "$message\n";
});
#!/usr/bin/env perl
use Mojo::Base 'Mojolicious';
sub handler {
my $tx = pop;
my $res = $tx->res;
$res->code(200);
$res->body('Hello World!');
$tx->resume;
};