Skip to content

Instantly share code, notes, and snippets.

View memememomo's full-sized avatar
🏠
Working from home

Uchiko memememomo

🏠
Working from home
  • Aichi, Japan
View GitHub Profile
@memememomo
memememomo / Mojolicious.pm
Created April 11, 2014 15:56
Mojoliciousアプリ全体のエラー処理をする ref: http://qiita.com/uchiko/items/350bfd4ac1ed38279415
sub _exception {
my ($next, $c) = @_;
local $SIG{__DIE__}
= sub { ref $_[0] ? CORE::die($_[0]) : Mojo::Exception->throw(@_) };
$c->render_exception($@) unless eval { $next->(); 1 };
}
use Mojolicious::Lite;
use Mojo::JSON::XS;
...
app->renderer->add_handler(json => sub {
my ($self, $c, $output, $options) = @_;
$$output = Mojo::JSON::XS->new->encode($options->{json});
});
@memememomo
memememomo / parse_param.js
Last active August 29, 2015 13:58
inputのnameで配列表記されているものをjavascriptのデータ構造に変換する
var is_array = function(value) {
return value &&
typeof value === 'object' &&
typeof value.length === 'number' &&
typeof value.splice === 'function' &&
!(value.propertyIsEnumerable('length'));
}
function parse_param(params) {
var build_hash;
@memememomo
memememomo / sample.pl
Created April 9, 2014 10:54
Perlでタイムアウトを設定して外部コマンドを実行する ref: http://qiita.com/memememomo/items/72a10e1bbe04b57f5666
use strict;
use warnings;
use POSIX qw( SIGKILL );
my $TIMEOUT = 10;
my $cmd = "sleep 15";
my $pid = fork();
if (!defined($pid)) { die "failed to fork"; }
if ($pid == 0) {
@memememomo
memememomo / basic.t
Created April 9, 2014 10:30
Test::MojoでビルドされたアプリにPhantomJSでアクセスする ref: http://qiita.com/memememomo/items/cc0ae8bb1d82be75315e
use strict;
use warnings;
use File::Basename;
use Mojo::Server::Daemon;
use Test::TCP;
use Test::More;
use Test::Mojo;
my $t = Test::Mojo->new('SampleApp');
@memememomo
memememomo / Poll.pm
Created April 5, 2014 19:18
Mojo::Reactor::Pollのソースを読んだときに調べたこと ref: http://qiita.com/memememomo/items/bee6630be8453a891fe1
# タイムアウト設定
$poll->poll($timeout);
# Read OKとなったハンドルに対してコールバックを実行
++$i and $self->_sandbox('Read', $self->{io}{fileno $_}{cb}, 0)
for $poll->handles(POLLIN | POLLPRI | POLLHUP | POLLERR);
# Write OKとなったハンドルに対してコールバックを実行
++$i and $self->_sandbox('Write', $self->{io}{fileno $_}{cb}, 1)
for $poll->handles(POLLOUT);
#include <stdio.h>
int main()
{
char buf[80];
int a, b;
fgets(buf, 80, stdin);
sscanf(buf, "%d %d", &a, &b);
JSON文字列に変換する + utf8フラグを外す
@memememomo
memememomo / base.t
Created March 13, 2014 10:59
Test::Mojoで、JSONを返すAPIをテストする。 ref: http://qiita.com/uchiko/items/70c132bab5e66783e474
use strict;
use warnings;
use utf8;
use Test::More;
use Test::Mojo;
use File::Basename;
$ENV{MOJO_HOME} = dirname(__FILE__) . '/../';
require "$ENV{MOJO_HOME}/myapp.pl";
$ morbo script/myapp # Mojoliciousアプリの場合
$ morbo myapp.pl # Mojolicious::Liteアプリの場合