<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:thr="http://purl.org/syndication/thread/1.0">
    <title>ziguzagu.org</title>
    
    
    <link rel="alternate" type="text/html" href="http://ziguzagu.org/" />
    <id>tag:typepad.com,2003:weblog-81249650132424459</id>
    <updated>2010-09-04T00:38:01+09:00</updated>
    <subtitle>ziguzagu's programming life with perl, javascript, emacs and etc.</subtitle>
    <generator uri="http://www.typepad.com/">TypePad</generator>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/ziguzagu-org" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="ziguzagu-org" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://hubbub.api.typepad.com/" /><entry>
        <title>Writing Plack::Middleware modifying the response body</title>
        <link rel="alternate" type="text/html" href="http://ziguzagu.org/2010/09/writing-plack-middleware-modifying-the-response-body.html" />
        <link rel="replies" type="text/html" href="http://ziguzagu.org/2010/09/writing-plack-middleware-modifying-the-response-body.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a012877233d43970c013486a92852970c</id>
        <published>2010-09-04T00:38:01+09:00</published>
        <updated>2010-09-04T01:31:59+09:00</updated>
        <summary>response body をいじる Plack::Middleware を作る場合、call で渡すコールバック関数の戻り値はサブルーチンにするのがよい（というかそうすべき？）。 sub call ...</summary>
        <author>
            <name>ziguzagu</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Perl" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Plack" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://ziguzagu.org/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>response body をいじる Plack::Middleware を作る場合、call で渡すコールバック関数の戻り値はサブルーチンにするのがよい（というかそうすべき？）。</p>

<pre><code>sub call {
    my ($self, $env) = @_;
    my $res = $self-&gt;app-&gt;($env);
    $self-&gt;response_cb($res, sub {
        my $res = shift;
        ## こゆこと
        return sub {
            my $chunk = shift;
            $chunk .= 'test';
            return $chunk;
        };
    }
}
</code></pre>

<p>ただ $res をそのまんま書き換えてもちゃんと動いている風。</p>

<pre><code>sub call {
    my ($self, $env) = @_;
    my $res = $self-&gt;app-&gt;($env);
    $self-&gt;response_cb($res, sub {
        my $res = shift;
        ## なまなましい感じで
        push @{ $res-&gt;[2] }, 'test';
    }
}
</code></pre>

<p>でも、後者をやってしまうと、Content-Length ヘッダがすでに設定されてしまっている場合、response body が変更されているにもかかわらず値が変更されない、という問題があったりする。</p>

<p>response_cb メソッドの実態は（いまのところ）Plack::Util::response_cb で、その中では、戻り値がサブルーチンだった場合にのみ設定済みの Content-Length を削除してくれるようになっている（ので、ContentLength Middleware も enable しておく必要がある）。以下、Plack::Util::response_cb (v0.9946) より抜粋。</p>

<pre><code>my $body_filter = sub {
    my($cb, $res) = @_;
    my $filter_cb = $cb-&gt;($res);
    # If response_cb returns a callback, treat it as a $body filter
    if (defined $filter_cb &amp;&amp; ref $filter_cb eq 'CODE') {
        Plack::Util::header_remove($res-&gt;[1], 'Content-Length');
        if (defined $res-&gt;[2]) {
            if (ref $res-&gt;[2] eq 'ARRAY') {
                for my $line (@{$res-&gt;[2]}) {
                    $line = $filter_cb-&gt;($line);
                }
                # Send EOF.
                my $eof = $filter_cb-&gt;( undef );
                push @{ $res-&gt;[2] }, $eof if defined $eof;
            } else {
                my $body    = $res-&gt;[2];
                my $getline = sub { $body-&gt;getline };
                $res-&gt;[2] = Plack::Util::inline_object
                    getline =&gt; sub { $filter_cb-&gt;($getline-&gt;()) },
                    close =&gt; sub { $body-&gt;close };
            }
        } else {
            return $filter_cb;
        }
    }
};
</code></pre>

<p>なんでサブルーチン返したときのみこうするようにしてるのかはよくわかっていないんだけど、、、そうなってるのでそうしましょう。。。（くぅ…、弱い）</p>

<p># そして、<a href="http://github.com/miyagawa/Plack/issues/#issue/121">P::U::response_cb の doc を書くという issue が github にある</a>ことに今気づいた</p>
</div>
</content>



    </entry>
    <entry>
        <title>Plack::Middleware::Watermark</title>
        <link rel="alternate" type="text/html" href="http://ziguzagu.org/2010/08/plack-middleware-watermark.html" />
        <link rel="replies" type="text/html" href="http://ziguzagu.org/2010/08/plack-middleware-watermark.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a012877233d43970c0134868f2845970c</id>
        <published>2010-08-31T01:16:21+09:00</published>
        <updated>2010-08-31T01:16:21+09:00</updated>
        <summary>HTML や XML なんかのケツにコメントとしてテキストを埋め込む Plack::Middleware を作ってみた。 http://github.com/ziguzagu/Plack-Middle...</summary>
        <author>
            <name>ziguzagu</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Perl" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Plack" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://ziguzagu.org/">
<div xmlns="http://www.w3.org/1999/xhtml"><p>HTML や XML なんかのケツにコメントとしてテキストを埋め込む Plack::Middleware を作ってみた。</p>

<p><a href="http://github.com/ziguzagu/Plack-Middleware-Watermark">http://github.com/ziguzagu/Plack-Middleware-Watermark</a></p>

<p>やってることといえば、content type を見て適当なコメントシンタックスを選んで、指定された文字列を追加するだけのお仕事。</p>

<pre><code>use Plack::Builder;
my $app = sub {
    [ 200, [ 'Content-Type' =&gt; 'text/html' ], [ "Hello World\n" ] ]
};
builder {
    enable 'Watermark', comment =&gt; 'HELLO HELLO!!';
    $app;
}
</code></pre>

<p>こうすると、</p>

<pre><code>Hello World
&lt;!-- HELLO HELLO!! --&gt;
</code></pre>

<p>こういう出力になる。サブルーチンも渡せたりするので、</p>

<pre><code>builder {
    enable 'Watermark', comment =&gt; sub { 'Generated by ' . Sys::Hostname::hostname };
    $app;
}
</code></pre>

<p>とかってのもできたり。HTML/CSS/XML/JS な Content-Type にとりあえず対応してる感じ。</p>

<p>でもまぁ、あんまり使い道はないですね :)
ただ Plack &amp; Plack::Middleware の勉強にはもろもろちょうど良かったです。まる。</p>
</div>
</content>



    </entry>
    <entry>
        <title>WEB+DB PRESS Vol.55</title>
        <link rel="alternate" type="text/html" href="http://ziguzagu.org/2010/05/webdb-press-vol55.html" />
        <link rel="replies" type="text/html" href="http://ziguzagu.org/2010/05/webdb-press-vol55.html" thr:count="0" />
        <id>tag:typepad.com,2003:post-6a012877233d43970c0133ed184764970b</id>
        <published>2010-05-01T12:13:52+09:00</published>
        <updated>2010-05-01T12:13:52+09:00</updated>
        <summary>またしても周回遅れ。Vol.56 を買ってしまったのであわてて読んだ Vol.55。 WEB+DB PRESS Vol.55 WEB+DB PRESS編集部 技術評論社 2010-02-24評価 by...</summary>
        <author>
            <name>ziguzagu</name>
        </author>
        
        
<content type="html" xml:lang="en-US" xml:base="http://ziguzagu.org/">
&lt;div xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;p&gt;またしても周回遅れ。Vol.56 を買ってしまったのであわてて読んだ Vol.55。&lt;/p&gt;

&lt;div class="hreview" &gt;&lt;a class="item url" href="http://www.amazon.co.jp/WEB-DB-PRESS-Vol-55-PRESS%E7%B7%A8%E9%9B%86%E9%83%A8/dp/4774141593%3FSubscriptionId%3D15SMZCTB9V8NGR2TW082%26tag%3Dziguzagu-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774141593"&gt;&lt;img src="http://ecx.images-amazon.com/images/I/61NVAApNjAL._SL160_.jpg" alt="photo" class="photo" style="float:left; margin: 0 15px 10px 10px; padding: 0;border:none;" /&gt;&lt;/a&gt;&lt;dl style="margin-bottom:0.5em; text-align:left; min-height: 168px;font-size:12px;line-height:16px;"&gt;&lt;dt class="fn"&gt;&lt;a class="item url" href="http://www.amazon.co.jp/WEB-DB-PRESS-Vol-55-PRESS%E7%B7%A8%E9%9B%86%E9%83%A8/dp/4774141593%3FSubscriptionId%3D15SMZCTB9V8NGR2TW082%26tag%3Dziguzagu-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774141593"&gt;WEB+DB PRESS Vol.55&lt;/a&gt;&lt;img src="http://www.assoc-amazon.jp/e/ir?t=ziguzagu-22&amp;l=ur2&amp;o=9" width="1" height="1" style="border: none;" alt="" /&gt;&lt;/dt&gt;&lt;dd&gt;WEB+DB PRESS編集部 &lt;/dd&gt;&lt;dd&gt;技術評論社 2010-02-24&lt;/dd&gt;&lt;dd&gt;評価&lt;abbr class="rating" title="5"&gt;&lt;img src="http://g-images.amazon.com/images/G/01/detail/stars-5-0.gif" alt="" /&gt;&lt;/abbr&gt; &lt;/dd&gt;&lt;/dl&gt;&lt;p class="gtools" style="font-size:10px;"&gt;by &lt;a href="http://www.goodpic.com/mt/aws/index.html" &gt;G-Tools&lt;/a&gt; ,  &lt;abbr class="dtreviewed" title="2010/05/01"&gt;2010/05/01&lt;/abbr&gt;&lt;/p&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;Perl Hackers Hub: PSGI/Plack
&lt;ul&gt;
&lt;li&gt;毎号回替わりで、Perl ハカーが執筆。これは楽しみな連載。&lt;/li&gt;
&lt;li&gt;で、3回読んだ。Plack 周りはいろいろスピードがはやいので、連載で定期的にまとまってよめるとうれしいなぁ。&lt;a href="http://blog.plackperl.org"&gt;Plack Blog&lt;/a&gt; みてればだいたいは分かるんだけれども。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;特集: HTML5 [実践]入門
&lt;ul&gt;
&lt;li&gt;まったく追いつけていない HTML5 の話。&lt;/li&gt;
&lt;li&gt;HTML5 の背景から始まり、基礎あり、各 API の要約されたサンプルコードありで、とてもよかった。&lt;/li&gt;
&lt;li&gt;サンプルコードあとで写経しよ。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;特集: モダンネットワークプログラミング入門
&lt;ul&gt;
&lt;li&gt;「ふつうの Linux プログラミング」で（C での）ネットワークプログラミングはかじった程度の知識しかなかったけど、わかりやすく読めた。&lt;/li&gt;
&lt;li&gt;特に、3章の「ネットワークプログラムの I/O 戦略」は非常に良かった。&lt;/li&gt;
&lt;li&gt;これもあとで写経しながらもっかい読もう。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Recent Perl World: 検索エンジンを作ろう
&lt;ul&gt;
&lt;li&gt;まったく門外漢の分野だけど、たのしそうなので後で写経しつつもう一度読む。&lt;/li&gt;
&lt;li&gt;そういえば昔つくった検索エンジンぽいのは、当時は名前もしらなかったけど、SuffixArray なものを自分で考えて作ったな、ということを思い出した。流石にもう別の仕組みにかわっているであろう...。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;大規模 Web サービスの裏側: スケーラブルなサービスの監視
&lt;ul&gt;
&lt;li&gt;mixi は、Nagios + 自作ツール（リソース監視）か。そして、memcached, MySQL 監視の拡張などもいっぱい。すごい。&lt;/li&gt;
&lt;li&gt;ちなみに、Six Apart の ops は Ganglia を利用しています（日本では Nagios も）。あと、&lt;a href="http://www.keynote.com/"&gt;Keynote&lt;/a&gt; というサービスつかっての外部稼働監視もやってる。世界中のいろんなところからアクセスしてレスポンスタイムなどもはかってくれたりするサービス。ops チームはかなりこれを信頼しているぽい。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;WebKit Quest: WebKit のコミュニティ
&lt;ul&gt;
&lt;li&gt;毎号毎号さわろうとおもってはいるものの、いまださわっていない...。&lt;/li&gt;
&lt;li&gt;そうこうしている間に WebKit2 がでている...。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;JavaScript 今ドキ活用術: サーバーサイドで JavaScript (CouchDB)
&lt;ul&gt;
&lt;li&gt;CouchDB オモロそうだな。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Ruby の向かう道
&lt;ul&gt;
&lt;li&gt;写真ででてる人がみんなスーツだ。スーツの世界にも Ruby は浸透しているのだな...。&lt;/li&gt;
&lt;li&gt;Perl はまったくスーツなイメージがない。など、どうでもいいことを思った。&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GW は写経して過ごすことにする。&lt;/p&gt;
&lt;/div&gt;
</content>



    </entry>
    <entry>
        <title>"plackup -s Starman" or "starman"</title>
        <link rel="alternate" type="text/html" href="http://ziguzagu.org/2010/03/plackup-s-starman-or-starman.html" />
        <link rel="replies" type="text/html" href="http://ziguzagu.org/2010/03/plackup-s-starman-or-starman.html" thr:count="1" thr:updated="2010-03-30T10:03:59+09:00" />
        <id>tag:typepad.com,2003:post-6a012877233d43970c0133ec4dcf3d970b</id>
        <published>2010-03-29T22:33:23+09:00</published>
        <updated>2010-03-29T22:33:23+09:00</updated>
        <summary>% plackup -s Starman app.psgi と % starman app.psgi は、機能的には全く同じ。なんだけど、plackup 使ったほうは ps コマンドでプロセスみるとコ...</summary>
        <author>
            <name>ziguzagu</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Perl" />
        <category scheme="http://www.sixapart.com/ns/types#category" term="Plack" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://ziguzagu.org/">
<div xmlns="http://www.w3.org/1999/xhtml"><pre><code>% plackup -s Starman app.psgi
</code></pre>

<p>と</p>

<pre><code>% starman app.psgi
</code></pre>

<p>は、機能的には全く同じ。なんだけど、plackup 使ったほうは ps コマンドでプロセスみるとコマンドラインから渡したパラメーターが見えなくなる。たとえば、worker の数調整して --daemonize したとする。</p>

<pre><code>% plackup -s Starman --workers 2 --daemonize app.psgi
% ps x | grep starman
18801   ??  Ss     0:00.01 starman master
18803   ??  S      0:00.00 starman worker
18804   ??  S      0:00.00 starman worker
</code></pre>

<p>こんな表示。一方 starman コマンド使ったほう。</p>

<pre><code>% starman --workers 2 --daemonize app.psgi
% ps x | grep starman
18835   ??  Ss     0:00.01 starman master --workers 2 --daemonize app.psgi
18837   ??  S      0:00.00 starman worker --workers 2 --daemonize app.psgi
18838   ??  S      0:00.00 starman worker --workers 2 --daemonize app.psgi
</code></pre>

<p>引数見えてる。</p>

<p>１つのホストで複数のアプリを plackup 経由で動かしていて、さてアプリ更新しよ、HUP だ、ってときに</p>

<pre><code>% ps x | grep starman
</code></pre>

<p>したら、いっぱい出てきて焦った...。</p>

<p>それぞれのアプリでもちろん listen してる port も違ったけど、--pid で pid ファイル作っていたので、そっちから、kill して難を逃れた...。</p>

<pre><code>% kill -HUP $(cat hello.pid)
</code></pre>

<p>きょうびのイケてる人達は daemontools 使って、plackup なり starman なりなんなり動かしてるようなので、たぶん問題ないんだろうけど（ daemontools まだ使った事ないからよくわからん...＞＜ ）、コマンド用意されている PSGI なサーバーはとりあえずそれを使おうと思った。</p>

<p>ていうかなんで ps がああなるかわかっていないのはダメ男な気がする...。アウチ。</p>
</div>
</content>



    </entry>
    <entry>
        <title>Pass HashRef as first argument to Getopt::Long::GetOptions</title>
        <link rel="alternate" type="text/html" href="http://ziguzagu.org/2010/03/pass-hashref-as-first-argument-to-getoptlonggetoptions.html" />
        <link rel="replies" type="text/html" href="http://ziguzagu.org/2010/03/pass-hashref-as-first-argument-to-getoptlonggetoptions.html" thr:count="2" thr:updated="2010-03-10T10:15:43+09:00" />
        <id>tag:typepad.com,2003:post-6a012877233d43970c01310f80ae1b970c</id>
        <published>2010-03-09T23:20:49+09:00</published>
        <updated>2010-03-09T23:20:49+09:00</updated>
        <summary>[http://gist.github.com/326535](http://gist.github.com/326535) これをみてちょっとビビった（まったく本筋とは関係ないところで）。Getop...</summary>
        <author>
            <name>ziguzagu</name>
        </author>
        <category scheme="http://www.sixapart.com/ns/types#category" term="Perl" />
        
        
<content type="xhtml" xml:lang="en-US" xml:base="http://ziguzagu.org/">
<div xmlns="http://www.w3.org/1999/xhtml"><p><a href="http://gist.github.com/326535">http://gist.github.com/326535</a></p>

<p>これをみてちょっとビビった（まったく本筋とは関係ないところで）。Getopt::Long::GetOptions の第１引数に HashRef 渡して、続いてオプションリストを配列で渡してる...。なんですかその使い方...。</p>

<p>まじで？！と思って試してみた。</p>

<pre><code>#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;

GetOptions(
    \my %opt,
    qw( foo=s bar=i baz ),
);                                                                                                                                                         
$opt{foo} ||= '';
$opt{bar} ||= 0;
$opt{baz} ||= 0;

print $opt{foo}, "\n";
print $opt{bar}, "\n";
print $opt{baz} ? 'ture' : 'false', "\n";
</code></pre>

<p>テスト。</p>

<pre><code>% perl getoptions.pl --foo test --bar 123
test
123
false
</code></pre>

<p>おぉお。こんな指定できたのかぁ、GetOptions。</p>
</div>
</content>



    </entry>
 
</feed><!-- ph=1 --><!-- nhm:dynamic-ssi -->
