<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>おいぬま日報 &#187; Perl</title>
	<atom:link href="http://tech.lampetty.net/tech/index.php/archives/category/perl/feed" rel="self" type="application/rss+xml" />
	<link>http://tech.lampetty.net/tech</link>
	<description>Apache, MySQL, Perl, Python あたり</description>
	<lastBuildDate>Sun, 05 Feb 2012 12:36:09 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/category/perl/feed" />
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Tomcatのスレッド数をグラフ化するMunin plugin</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/395?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tomcat%25e3%2581%25ae%25e3%2582%25b9%25e3%2583%25ac%25e3%2583%2583%25e3%2583%2589%25e6%2595%25b0%25e3%2582%2592%25e3%2582%25b0%25e3%2583%25a9%25e3%2583%2595%25e5%258c%2596%25e3%2581%2599%25e3%2582%258bmunin-plugin</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/395#comments</comments>
		<pubDate>Sun, 19 Dec 2010 04:25:05 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Munin]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=395</guid>
		<description><![CDATA[会社で使おうと思っていたTomcatのスレッド数をグラフ化するMunin pluginがあるのですが、XML::Simpleに依存していて「Munin pluginのためにわざわざPerlモジュールインストールするのもな [...]]]></description>
			<content:encoded><![CDATA[<p>会社で使おうと思っていたTomcatのスレッド数をグラフ化するMunin pluginがあるのですが、XML::Simpleに依存していて「Munin pluginのためにわざわざPerlモジュールインストールするのもなぁ」と思ったので、ついかっとなってXMLをパースするところを正規表現で書き直してみました。ついでにmaxThreadsも表示するようにしたり、微調整もしています。プラグインは<a href="https://github.com/oinume/munin-plugins/blob/master/tomcat_threads">github</a>に上げてあります。</p>
<p>なんでXML::Simpleを使っていたかというとTomcatのstatus画面がXMLで出力されているからなのですが、頑張ればこのぐらいのXMLは正規表現でパースできるということが実践できて良かったです。</p>
<p>↓は実際のスレッド数のグラフ。</p>
<p><a class="" rel="lightbox" title="munin_tomcat_threads" href="http://www.flickr.com/photos/8250493@N05/5273139214/"><img src="http://farm6.static.flickr.com/5042/5273139214_0903e10d9a.jpg" alt="munin_tomcat_threads"></a></p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Tomcat%E3%83%8F%E3%83%B3%E3%83%89%E3%83%96%E3%83%83%E3%82%AF-%E7%AC%AC2%E7%89%88-Jason-Brittain/dp/4873113881%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873113881"><img src="http://ecx.images-amazon.com/images/I/51hnxTab2NL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Tomcat%E3%83%8F%E3%83%B3%E3%83%89%E3%83%96%E3%83%83%E3%82%AF-%E7%AC%AC2%E7%89%88-Jason-Brittain/dp/4873113881%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873113881">Tomcatハンドブック 第2版</a></p>
	<p><em>著者／訳者：</em>Jason Brittain Ian F. Darwin </p>
	<p><em>出版社：</em>オライリージャパン( 2008-11-21 )</p>
	<p>大型本 ( 560 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/395/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/395" />
	</item>
		<item>
		<title>Perl開発環境としてのEmacsの設定</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/384?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl%25e9%2596%258b%25e7%2599%25ba%25e7%2592%25b0%25e5%25a2%2583%25e3%2581%25a8%25e3%2581%2597%25e3%2581%25a6%25e3%2581%25aeemacs%25e3%2581%25ae%25e8%25a8%25ad%25e5%25ae%259a</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/384#comments</comments>
		<pubDate>Sat, 21 Aug 2010 16:28:13 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=384</guid>
		<description><![CDATA[最近社内でvim勉強会があったりして「vimもいいなぁ」と思っていたのですが、やっぱりキーバインドに慣れなくってEmacsに戻ってきました。最近EmacsでPerl関連の設定を見なおしたので、メモがてら書いておきます。自 [...]]]></description>
			<content:encoded><![CDATA[<p>最近社内でvim勉強会があったりして「vimもいいなぁ」と思っていたのですが、やっぱりキーバインドに慣れなくってEmacsに戻ってきました。最近EmacsでPerl関連の設定を見なおしたので、メモがてら書いておきます。自分がPerlのコードを書くときに使っているelispは以下の5つです。</p>
<ul>
<li>cperl-mode</li>
<li>flymake + set-perl5lib</li>
<li>anything + auto-complete + perl-completion</li>
<li>yasnippet</li>
</ul>
<p>これらを一つ一つ紹介していきます。</p>
<h3>cperl-mode</h3>
<p><a href="http://www.emacswiki.org/emacs/CPerlMode">cperl-mode</a> はこんな感じで設定しています。とりあえず色付けと適切なインデントがなされればいいかなという感じ。</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>autoload 'cperl-mode <span style="color: #ff0000;">&quot;cperl-mode&quot;</span> <span style="color: #ff0000;">&quot;alternate mode for editing Perl programs&quot;</span> t<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'auto-mode-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.<span style="color: #000099; font-weight: bold;">\\</span>([pP][Llm]<span style="color: #000099; font-weight: bold;">\\</span>|al<span style="color: #000099; font-weight: bold;">\\</span>|t<span style="color: #000099; font-weight: bold;">\\</span>|cgi<span style="color: #000099; font-weight: bold;">\\</span>)<span style="color: #000099; font-weight: bold;">\\</span>'&quot;</span> <span style="color: #66cc66;">.</span> cperl-mode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'interpreter-mode-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;perl&quot;</span> <span style="color: #66cc66;">.</span> cperl-mode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'interpreter-mode-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;perl5&quot;</span> <span style="color: #66cc66;">.</span> cperl-mode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'interpreter-mode-alist '<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;miniperl&quot;</span> <span style="color: #66cc66;">.</span> cperl-mode<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;;; cperl-mode is preferred to perl-mode</span>
<span style="color: #808080; font-style: italic;">;;; &quot;Brevity is the soul of wit&quot; &lt;foo at acm.org&gt;</span>
<span style="color: #66cc66;">&#40;</span>defalias 'perl-mode 'cperl-mode<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> cperl-indent-level <span style="color: #cc66cc;">4</span>
      cperl-continued-statement-offset <span style="color: #cc66cc;">4</span>
      cperl-close-paren-offset -<span style="color: #cc66cc;">4</span>
      cperl-label-offset -<span style="color: #cc66cc;">4</span>
      cperl-comment-column <span style="color: #cc66cc;">40</span>
      cperl-highlight-variables-indiscriminately t
      cperl-indent-parens-as-block t
      cperl-tab-always-indent <span style="color: #b1b100;">nil</span>
      cperl-font-lock t<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'cperl-mode-hook
          '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">progn</span>
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> indent-tabs-mode <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> tab-width <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
               <span style="color: #808080; font-style: italic;">; perl-completion</span>
               <span style="color: #66cc66;">&#40;</span>require 'auto-complete<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>require 'perl-completion<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>add-to-<span style="color: #b1b100;">list</span> 'ac-sources 'ac-source-perl-completion<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>perl-completion-mode t<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">; perl tidy</span>
<span style="color: #808080; font-style: italic;">; sudo aptitude install perltidy</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> perltidy-region <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Run perltidy on the current region.&quot;</span>
  <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>save-excursion
    <span style="color: #66cc66;">&#40;</span>shell-command-on-region <span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>mark<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;perltidy -q&quot;</span> <span style="color: #b1b100;">nil</span> t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> perltidy-<span style="color: #b1b100;">defun</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #ff0000;">&quot;Run perltidy on the current defun.&quot;</span>
  <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>save-excursion <span style="color: #66cc66;">&#40;</span>mark-<span style="color: #b1b100;">defun</span><span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span>perltidy-region<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>global-set-key <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\C</span>-ct&quot;</span> 'perltidy-region<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>global-set-key <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\C</span>-c<span style="color: #000099; font-weight: bold;">\C</span>-t&quot;</span> 'perltidy-<span style="color: #b1b100;">defun</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>flymake + set-perl5lib</h3>
<p><a href="http://www.emacswiki.org/emacs/FlyMake">flymake</a> はEmacs自体に含まれているソースコードチェッカーです。set-perl5lib は<a href="http://coderepos.org/share/browser/lang/elisp/set-perl5lib/set-perl5lib.el">ここ</a>からダウンロードできます。この設定を入れるとコードを書いているときに裏でsyntax checkが走って、エラーになっている箇所を赤くしてくれます。これのおかげでしょうもないtypoに簡単にきづけるので、だいぶコードを書くのが速くなりました。かなりおすすめの設定です。</p>
<p style="text-align: center;"><a class="" rel="lightbox" title="flymake_perl" href="http://www.flickr.com/photos/8250493@N05/4911450687/"><img class="alignnone" src="http://farm5.static.flickr.com/4115/4911450687_2b8af1bd20.jpg" alt="flymake_perl"></a></p>
<p>なお、自分は C-c e でエラーが発生している箇所にジャンプしてエラーメッセージをミニバッファに表示するように設定しています。</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; flymake for perl</span>
<span style="color: #66cc66;">&#40;</span>defvar flymake-perl-err-line-patterns '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>(.*<span style="color: #000099; font-weight: bold;">\\</span>) at <span style="color: #000099; font-weight: bold;">\\</span>([^ <span style="color: #000099; font-weight: bold;">\n</span>]+<span style="color: #000099; font-weight: bold;">\\</span>) line <span style="color: #000099; font-weight: bold;">\\</span>([0-9]+<span style="color: #000099; font-weight: bold;">\\</span>)[,.<span style="color: #000099; font-weight: bold;">\n</span>]&quot;</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defconst flymake-allowed-perl-file-name-masks '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.pl$&quot;</span> flymake-perl-init<span style="color: #66cc66;">&#41;</span>
                                               <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.pm$&quot;</span> flymake-perl-init<span style="color: #66cc66;">&#41;</span>
                                               <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>.t$&quot;</span> flymake-perl-init<span style="color: #66cc66;">&#41;</span>
                                               <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> flymake-perl-init <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>temp-file <span style="color: #66cc66;">&#40;</span>flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>local-file <span style="color: #66cc66;">&#40;</span>file-relative-<span style="color: #b1b100;">name</span>
                      temp-file
                      <span style="color: #66cc66;">&#40;</span>file-name-directory buffer-file-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;perl&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #ff0000;">&quot;-wc&quot;</span> local-file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> flymake-perl-load <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>set-perl5lib<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>defadvice flymake-post-syntax-check <span style="color: #66cc66;">&#40;</span>before flymake-force-check-was-interrupted<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-check-was-interrupted t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>ad-activate 'flymake-post-syntax-check<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-allowed-file-name-masks <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> flymake-allowed-file-name-masks flymake-allowed-perl-file-name-masks<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-err-line-patterns flymake-perl-err-line-patterns<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flymake-mode t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>add-hook 'cperl-mode-hook '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>flymake-perl-load<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> next-flymake-<span style="color: #b1b100;">error</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flymake-goto-next-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>err <span style="color: #66cc66;">&#40;</span>get-char-property <span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span> 'help-echo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> err
      <span style="color: #66cc66;">&#40;</span>message err<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>global-set-key <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\C</span>-ce&quot;</span> 'next-flymake-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>auto-complete + perl-completion</h3>
<p><a href="http://cx4a.org/software/auto-complete/">auto-complete</a> と <a href="http://www.emacswiki.org/emacs/PerlCompletion">perl-completion</a> を組み合わせてPerlのモジュールや関数、変数を補完する設定をしています。設定する elisp は先ほどの cperl-mode のところに含まれているので割愛します。下記のような感じで補完候補が出てきます。</p>
<p style="text-align: center;"><a class="" rel="lightbox" title="perl_completion" href="http://www.flickr.com/photos/8250493@N05/4912733953/"><img class="alignnone" src="http://farm5.static.flickr.com/4121/4912733953_52f9cc97bf.jpg" alt="perl_completion"></a></p>
<p>また、perl-completion にはデフォルトで C-c s するとカーソルのあるモジュールのドキュメントをanything インタフェースで開くことができます。</p>
<p style="text-align: center;"><a class="" rel="lightbox" title="perl_completion_pod" href="http://www.flickr.com/photos/8250493@N05/4913485564/"><img class="alignnone" src="http://farm5.static.flickr.com/4120/4913485564_c432b63746.jpg" alt="perl_completion_pod"></a></p>
<h3>yasnippet</h3>
<p><a href="http://code.google.com/p/yasnippet/">yasnippet</a> は任意のキーワードを打ってTABを押すと、そのキーワードを登録しておいたテンプレート(snippet)で置換してくれるものです。うまく使えばかなりのタイプ量が減らせるのでコードを書くのが早くなります。自分は例えば</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span></pre></div></div>

<p>というsnippetを &#8220;usestwa&#8221; というキーワードで登録しています。</p>
<p>yasnippetには予め用意されているものがあるのですが、自分はそれとは別にディレクトリを作成して(~/.emacs.d/mysnippets)、そこに自分で作ったsnippetを置いています。以下がyasnippetの設定です。</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>require 'yasnippet<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>require 'dropdown-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> yas/text-popup-<span style="color: #b1b100;">function</span> #'yas/dropdown-list-popup-for-template<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; コメントやリテラルではスニペットを展開しない</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> yas/buffer-local-condition
      '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;font-lock-comment-face&quot;</span>
                             <span style="color: #66cc66;">&#40;</span>get-char-property <span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span> 'face<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;font-lock-string-face&quot;</span>
                             <span style="color: #66cc66;">&#40;</span>get-char-property <span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span> 'face<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
           '<span style="color: #66cc66;">&#40;</span>require-snippet-condition <span style="color: #66cc66;">.</span> force-in-comment<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; yasnippet 公式提供のものと、</span>
<span style="color: #808080; font-style: italic;">;; 自分用カスタマイズスニペットをロード同名のスニペットが複数ある場合、</span>
<span style="color: #808080; font-style: italic;">;; あとから読みこんだ自分用のものが優先される。</span>
<span style="color: #808080; font-style: italic;">;; また、スニペットを変更、追加した場合、</span>
<span style="color: #808080; font-style: italic;">;; このコマンドを実行することで、変更・追加が反映される。</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> yas/load-all-directories <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>interactive<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>yas/reload-all<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapc</span> 'yas/load-directory-<span style="color: #cc66cc;">1</span> my-snippet-directories<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;;; yasnippet展開中はflymakeを無効にする</span>
<span style="color: #66cc66;">&#40;</span>defvar flymake-is-active-flag <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defadvice yas/expand-snippet
  <span style="color: #66cc66;">&#40;</span>before inhibit-flymake-syntax-checking-while-expanding-snippet activate<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-is-active-flag
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> flymake-is-active-flag
            <span style="color: #66cc66;">&#40;</span>assoc-default 'flymake-mode <span style="color: #66cc66;">&#40;</span>buffer-local-variables<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> flymake-is-active-flag
    <span style="color: #66cc66;">&#40;</span>flymake-mode-off<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>add-hook 'yas/after-exit-snippet-hook
          '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> flymake-is-active-flag
               <span style="color: #66cc66;">&#40;</span>flymake-mode-on<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> flymake-is-active-flag <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> yas/root-directory <span style="color: #66cc66;">&#40;</span>expand-file-<span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;~/.emacs.d/snippets&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; 自分用スニペットディレクトリ(リストで複数指定可)</span>
<span style="color: #66cc66;">&#40;</span>defvar my-snippet-directories
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>expand-file-<span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;~/.emacs.d/mysnippets&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>yas/initialize<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>yas/load-directory <span style="color: #ff0000;">&quot;~/.emacs.d/snippets&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>yas/load-all-directories<span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>まとめ</h3>
<p>以上の設定をすると</p>
<ul>
<li>ソースの色付け</li>
<li>シンタックスチェック</li>
<li>シンボルの補完</li>
<li>モジュールのドキュメントを引く</li>
<li>snippetによるタイプ量の削減</li>
</ul>
<p>ができるようになります。EclipseのようなIDEにはまだ及びませんが、これでだいぶコードを書くのが速くなるかと思います。</p>
<h3>情報源</h3>
<ul>
<li><a href="http://typester.stfuawsc.com/slides/perlcasual2/start.html">Perl Hacks on Emacs</a></li>
<li><a href="http://d.hatena.ne.jp/antipop/20080701/1214838633">EmacsでPerl開発する上で必須な設定 #2 &#8211; flymake</a></li>
</ul>
<p>を参考にさせてもらいました。ありがとうございます。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Emacs%E3%83%86%E3%82%AF%E3%83%8B%E3%83%83%E3%82%AF%E3%83%90%E3%82%A4%E3%83%96%E3%83%AB-%EF%BD%9E%E4%BD%9C%E6%A5%AD%E5%8A%B9%E7%8E%87%E3%82%92%E3%82%AB%E3%82%A4%E3%82%BC%E3%83%B3%E3%81%99%E3%82%8B200%E3%81%AE%E6%8A%80%EF%BD%9E-%E3%82%8B%E3%81%B3%E3%81%8D%E3%81%A1/dp/4774143278%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774143278"><img src="http://ecx.images-amazon.com/images/I/517LicIhabL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Emacs%E3%83%86%E3%82%AF%E3%83%8B%E3%83%83%E3%82%AF%E3%83%90%E3%82%A4%E3%83%96%E3%83%AB-%EF%BD%9E%E4%BD%9C%E6%A5%AD%E5%8A%B9%E7%8E%87%E3%82%92%E3%82%AB%E3%82%A4%E3%82%BC%E3%83%B3%E3%81%99%E3%82%8B200%E3%81%AE%E6%8A%80%EF%BD%9E-%E3%82%8B%E3%81%B3%E3%81%8D%E3%81%A1/dp/4774143278%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4774143278">Emacsテクニックバイブル　～作業効率をカイゼンする200の技～</a></p>
	<p><em>著者／訳者：</em>るびきち</p>
	<p><em>出版社：</em>技術評論社( 2010-08-03 )</p>
	<p>単行本（ソフトカバー） ( 384 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/384/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/384" />
	</item>
		<item>
		<title>コマンドラインオプションを解析するPerlモジュールGetopt::Compact</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/374?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=%25e3%2582%25b3%25e3%2583%259e%25e3%2583%25b3%25e3%2583%2589%25e3%2583%25a9%25e3%2582%25a4%25e3%2583%25b3%25e3%2582%25aa%25e3%2583%2597%25e3%2582%25b7%25e3%2583%25a7%25e3%2583%25b3%25e3%2582%2592%25e8%25a7%25a3%25e6%259e%2590%25e3%2581%2599%25e3%2582%258bperl%25e3%2583%25a2%25e3%2582%25b8%25e3%2583%25a5%25e3%2583%25bc</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/374#comments</comments>
		<pubDate>Sat, 10 Apr 2010 12:37:12 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=374</guid>
		<description><![CDATA[Perlでコマンドラインオプションを解析する場合、大体は標準添付されているGetopt::Longを使うと思いますが、自分は3年前ぐらいから Getopt::Compact というモジュールに出会い、それ以降大抵の場合は [...]]]></description>
			<content:encoded><![CDATA[<p>Perlでコマンドラインオプションを解析する場合、大体は標準添付されているGetopt::Longを使うと思いますが、自分は3年前ぐらいから <a href="http://search.cpan.org/dist/Getopt-Compact/lib/Getopt/Compact.pm">Getopt::Compact</a> というモジュールに出会い、それ以降大抵の場合はこれを使っています。このモジュールの良いところは</p>
<ul>
<li>オプションの定義(-c, &#8211;config などの定義)とそのヘルプメッセージが一箇所で定義できる</li>
<li>-h, &#8211;helpオプションを最初から定義してくれてる(&#8211;manオプションも)</li>
<li>定義してないオプションが指定されると自動的にエラーにしてくれる</li>
<li>Getopt::Compact#usage でヘルプメッセージが簡単に取得できる</li>
</ul>
<p>があると思っています。などと抽象的な話をしてもしょうがないので、具体的なコードを書いてみます。</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Compact</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">Dumper</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$getopt</span> <span style="color: #339933;">=</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Compact</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
    name <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'getopt_compact.pl'</span><span style="color: #339933;">,</span>
    modes <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    args <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'FILE'</span><span style="color: #339933;">,</span>
    version <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'0.1'</span><span style="color: #339933;">,</span>
    struct <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span>
        <span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#91;</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>f force<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'force overwrite of output file and compress links'</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#91;</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>l level<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'compress level'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'=i'</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#91;</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>S suffix<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'use suffix on compressed files'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'=s'</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$options</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$getopt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">opts</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #0000ff;">$getopt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">usage</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;options -----<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> Dumper <span style="color: #0000ff;">$options</span><span style="color: #339933;">;</span></pre></div></div>

<p>上の例では gzip のオプションのモノマネで</p>
<ul>
<li>-f, &#8211;forceというフラグオプション</li>
<li>-l, &#8211;levelという数値のオプション</li>
<li>-S, &#8211;suffixという文字列のオプション</li>
<li>オプション以外に引数FILEを受け取る</li>
</ul>
<p>という仕様としています。</p>
<p>でで、例えば</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> getopt_compact.pl <span style="color: #660033;">--help</span></pre></div></div>

<p>と実行すると</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">getopt_compact.pl v0.1
usage: getopt_compact.pl <span style="color: #7a0874; font-weight: bold;">&#91;</span>options<span style="color: #7a0874; font-weight: bold;">&#93;</span> FILE
options                                                          
-h, <span style="color: #660033;">--help</span>      This <span style="color: #7a0874; font-weight: bold;">help</span> message                                
-v, <span style="color: #660033;">--verbose</span>   Verbose mode                                     
-f, <span style="color: #660033;">--force</span>     Force overwrite of output <span style="color: #c20cb9; font-weight: bold;">file</span> and compress links
-l, <span style="color: #660033;">--level</span>     Compress level                                   
-S, <span style="color: #660033;">--suffix</span>    Use suffix on compressed files                   
    <span style="color: #660033;">--man</span>       Display documentation</pre></div></div>

<p>のようにヘルプが表示されます。また、&#8211;hoge のような存在しないオプションを指定すると</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> getopt_compact.pl <span style="color: #660033;">--hoge</span>
Unknown option: hoge
getopt_compact.pl v0.1
usage: getopt_compact.pl <span style="color: #7a0874; font-weight: bold;">&#91;</span>options<span style="color: #7a0874; font-weight: bold;">&#93;</span> FILE
options                                                          
-h, <span style="color: #660033;">--help</span>      This <span style="color: #7a0874; font-weight: bold;">help</span> message                                
-v, <span style="color: #660033;">--verbose</span>   Verbose mode                                     
-f, <span style="color: #660033;">--force</span>     Force overwrite of output <span style="color: #c20cb9; font-weight: bold;">file</span> and compress links
-l, <span style="color: #660033;">--level</span>     Compress level                                   
-S, <span style="color: #660033;">--suffix</span>    Use suffix on compressed files                   
    <span style="color: #660033;">--man</span>       Display documentation</pre></div></div>

<p>のようにエラーになりヘルプが表示されます。</p>
<p>という感じで、Getopt::Longより依存モジュールがあるものの便利に使わせてもらってます。ただ、PerlのGetopt系のモジュールは他にも色々あるのでもっと便利なのがありそうですが、こういうのもあるよという紹介でした。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/%E3%83%A2%E3%83%80%E3%83%B3Perl%E5%85%A5%E9%96%80-CodeZine-BOOKS-%E7%89%A7-%E5%A4%A7%E8%BC%94/dp/4798119172%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798119172"><img src="http://ecx.images-amazon.com/images/I/41W6wTHDSzL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/%E3%83%A2%E3%83%80%E3%83%B3Perl%E5%85%A5%E9%96%80-CodeZine-BOOKS-%E7%89%A7-%E5%A4%A7%E8%BC%94/dp/4798119172%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798119172">モダンPerl入門 (CodeZine BOOKS)</a></p>
	<p><em>著者／訳者：</em>牧 大輔</p>
	<p><em>出版社：</em>翔泳社( 2009-02-10 )</p>
	<p>大型本 ( 344 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/374/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/374" />
	</item>
		<item>
		<title>DBI-&gt;connectのHandleError</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/366?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dbi-connect%25e3%2581%25aehandleerror</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/366#comments</comments>
		<pubDate>Wed, 10 Feb 2010 15:48:28 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=366</guid>
		<description><![CDATA[DBIのちょっとしたTIPSです。DBIには connect する時に DBI-&#62;connect&#40;'dbi:mysql:database=hoge', 'root', 'whatever', &#123;  [...]]]></description>
			<content:encoded><![CDATA[<p>DBIのちょっとしたTIPSです。DBIには connect する時に</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'dbi:mysql:database=hoge'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'root'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'whatever'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">...</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>とオプションを渡すことができます。(たとえば RaiseError) 。このオプションのひとつに HandleError というものがあってデバッグにはなかなか便利なので紹介してみます。端的にいうと HandleError でサブルーチンを登録しておくと、エラーが発生したときにこのサブルーチンを使ってエラーを投げてくれるようになります。以下は具体例。</p>
<p># MyDB.pm</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> MyDB<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Carp <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DBI<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">bless</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">connect</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>dbh<span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> DBI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span>
        <span style="color: #ff0000;">'dbi:mysql:database=dbix_thin_test'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'root'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'root'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span> RaiseError <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> HandleError <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;Carp</span><span style="color: #339933;">::</span><span style="color: #006600;">confess</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;"># here</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">select</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$self</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@_</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sth</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>dbh<span style="color: #009900;">&#125;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># error!</span>
    <span style="color: #0000ff;">$sth</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p># dbi_handle_error.pl</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> FindBin <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$Bin</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> lib <span style="color: #ff0000;">&quot;$Bin&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> MyDB<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$db</span> <span style="color: #339933;">=</span> MyDB<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">select</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;select * from not_exist&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>と書いてわざと MyDB#select でエラーになるようにしておき、このスクリプトを実行します。<br />
すると</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">$ perl <span style="color: #339933;">~/</span>script<span style="color: #339933;">/</span>perl<span style="color: #339933;">/</span>dbi_handle_error<span style="color: #339933;">.</span>pl
DBD<span style="color: #339933;">::</span><span style="color: #006600;">mysql</span><span style="color: #339933;">::</span><span style="color: #006600;">st</span> execute failed<span style="color: #339933;">:</span> Table <span style="color: #ff0000;">'dbix_thin_test.not_exist'</span> doesn<span style="color: #ff0000;">'t existDBI::st=HASH(0x248a900) at /home/kazuhiro/script/perl/MyDB.pm line 23
        MyDB::select('</span>MyDB<span style="color: #339933;">=</span>HASH<span style="color: #009900;">&#40;</span>0x21b8df0<span style="color: #009900;">&#41;</span><span style="color: #ff0000;">') called at /home/kazuhiro/script/perl/dbi_handle_error.pl line 11</span></pre></div></div>

<p>のように DBI->connectの HandleError で渡した Carp::confess が呼ばれ、エラーになった時にスタックトレースが表示されます。HandleError を指定しない場合は</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">$ perl <span style="color: #339933;">~/</span>script<span style="color: #339933;">/</span>perl<span style="color: #339933;">/</span>dbi_handle_error<span style="color: #339933;">.</span>pl
DBD<span style="color: #339933;">::</span><span style="color: #006600;">mysql</span><span style="color: #339933;">::</span><span style="color: #006600;">st</span> execute failed<span style="color: #339933;">:</span> Table <span style="color: #ff0000;">'dbix_thin_test.not_exist'</span> doesn<span style="color: #ff0000;">'t exist at /home/kazuhiro/script/perl/MyDB.pm line 23.
DBD::mysql::st execute failed: Table '</span>dbix_thin_test<span style="color: #339933;">.</span>not_exist<span style="color: #ff0000;">' doesn'</span>t exist at <span style="color: #339933;">/</span>home<span style="color: #339933;">/</span>kazuhiro<span style="color: #339933;">/</span>script<span style="color: #339933;">/</span>perl<span style="color: #339933;">/</span>MyDB<span style="color: #339933;">.</span>pm line <span style="color: #cc66cc;">23</span><span style="color: #339933;">.</span></pre></div></div>

<p>のようになってしまい、実際にエラーが発生している箇所と問題のクエリの関連性がわかりにくいですが、HandleErrorで Carp::confess を指定しておくとファイル名と行番号がわかるのでどこでエラーになっているかあたりがつけやすいと思います。(この例だとメソッドのネストが少ないのでそのありがたみがわからないですが&#8230;)</p>
<p>以上、生DBIでもこんなマニアックなオプションがあるんだよという紹介でした。<a href="http://d.hatena.ne.jp/ZIGOROu/20090731/1249050735">ZIGOROuさんの生 DBI ユーザーのための DBI Cookbook (1)</a>を見ててよく思うのですが、DBIってなかなか奥が深いです。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/%E3%83%A2%E3%83%80%E3%83%B3Perl%E5%85%A5%E9%96%80-CodeZine-BOOKS-%E7%89%A7-%E5%A4%A7%E8%BC%94/dp/4798119172%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798119172"><img src="http://ecx.images-amazon.com/images/I/41W6wTHDSzL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/%E3%83%A2%E3%83%80%E3%83%B3Perl%E5%85%A5%E9%96%80-CodeZine-BOOKS-%E7%89%A7-%E5%A4%A7%E8%BC%94/dp/4798119172%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798119172">モダンPerl入門 (CodeZine BOOKS)</a></p>
	<p><em>著者／訳者：</em>牧 大輔</p>
	<p><em>出版社：</em>翔泳社( 2009-02-10 )</p>
	<p>大型本 ( 344 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/366/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/366" />
	</item>
		<item>
		<title>DBIx::ThinをCPANから削除</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/364?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dbixthin%25e3%2582%2592cpan%25e3%2581%258b%25e3%2582%2589%25e5%2589%258a%25e9%2599%25a4</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/364#comments</comments>
		<pubDate>Tue, 12 Jan 2010 17:18:02 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[DBIx::Thin]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=364</guid>
		<description><![CDATA[表題の通りですが、思うところがあってDBIx::Thinを削除しました。きっかけは、CPAN Ratingsのレビューです(以下引用)。 DBIx-Thin (0.05) ** It strikes me as odd  [...]]]></description>
			<content:encoded><![CDATA[<p>表題の通りですが、思うところがあって<a href="http://github.com/oinume/p5-dbix-thin">DBIx::Thin</a>を削除しました。きっかけは、<a href="http://cpanratings.perl.org/dist/DBIx-Thin">CPAN Ratingsのレビュー</a>です(以下引用)。</p>
<blockquote><p>
DBIx-Thin (0.05) **</p>
<p> It strikes me as odd how almost most of Japanese authors (of course, except honourable and admirable authors) don&#8217;t write why they have written their modules and the intention of the modules.<br />
 So is the author of this module.<br />
 The module is yet another ORM module but unfocused one. What the heck is different from DBIx::Skinny? The explanation is nowhere to be found, but there is just an enumeration of methods. And yet could anyone else use the module?
</p></blockquote>
<p>言われてみればその通りで、PODにコンセプトも書いてなければ<a href="http://search.cpan.org/dist/DBIx-Skinny/">DBIx::Skinny</a>と違うところも良くわからないと。その辺に関しては<a href="http://github.com/oinume/p5-dbix-thin/blob/master/lib/DBIx/Thin.pm">モジュールのPOD</a>に書いておきましたが、それでもDBIx::Skinnyとあんまり変わらないし、現時点で自分以外の誰かが使っているとも思えないので、変な混乱を招かないようにCPANからは削除しました。んで<a href="http://github.com/oinume/p5-dbix-thin">github</a>では開発を継続していきます。</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/364/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/364" />
	</item>
		<item>
		<title>DBIx::ThinというORMapperをリリースしました</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/357?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dbixthin%25e3%2581%25a8%25e3%2581%2584%25e3%2581%2586ormapper%25e3%2582%2592%25e3%2583%25aa%25e3%2583%25aa%25e3%2583%25bc%25e3%2582%25b9%25e3%2581%2597%25e3%2581%25be%25e3%2581%2597%25e3%2581%259f</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/357#comments</comments>
		<pubDate>Tue, 29 Dec 2009 02:21:14 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[DBIx::Thin]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=357</guid>
		<description><![CDATA[今年のYAPC(9月)からすき間時間を使って作っていたDBIx::Thinという、DBIx::Skinnyインスパイアなモジュールを昨日やっとリリースしました。作った動機は単に自己満足の追求と車輪の再発明による個人のスキ [...]]]></description>
			<content:encoded><![CDATA[<p>今年のYAPC(9月)からすき間時間を使って作っていた<a href="http://search.cpan.org/dist/DBIx-Thin/lib/DBIx/Thin.pm">DBIx::Thin</a>という、<a href="http://search.cpan.org/dist/DBIx-Skinny/lib/DBIx/Skinny.pm">DBIx::Skinny</a>インスパイアなモジュールを昨日やっとリリースしました。作った動機は単に自己満足の追求と車輪の再発明による個人のスキルアップですが、前職で3年前ぐらいに自作したORMのコードを忘れないうちに改良して世に出したいなぁとずっと思っていたのです。SkinnyのAdvent Calendarが日々更新されているのを読み、途中で何度も「これSkinny使えばいいんじゃね？」って思って挫折しかけましたが、Skinnyも細かいところでは自分のポリシーと合わない部分があったりしたのと、とりあえず作ってしまったので世に出してみます。</p>
<p>特徴としては</p>
<ul>
<li>生SQL派にやさしい</li>
<li>Skinnyより書き方は冗長だと思うけど、コードを見て何をやっているかわかるようにした</li>
<li>依存モジュールが少ない(標準以外のものは3つ)</li>
<li>Schemaクラスを生成するジェネレータが標準添付(ただしまだMySQL以外のDBに対応していない)</li>
</ul>
<p>かなと思います。SQLはなんだかんだ言ってもまだWebエンジニアの共通言語だと思うので、ORMでゴリゴリJOINのコードを書くより生SQLの方がわかりやすい、と思っているので、生SQLなモジュールを作りました。</p>
<p>「Skinnyより書き方は冗長」というのは、例えばSkinnyだと</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$iterator</span> <span style="color: #339933;">=</span> Your<span style="color: #339933;">::</span><span style="color: #006600;">Model</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search</span><span style="color: #009900;">&#40;</span>
    <span style="color: #ff0000;">'user'</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>id <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>order_by <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'id'</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>と書きますが、2つ目の引数が何を表しているのかぱっと見わからないので、Thinの場合は</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$iterator</span> <span style="color: #339933;">=</span> Your<span style="color: #339933;">::</span><span style="color: #006600;">Model</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">search</span><span style="color: #009900;">&#40;</span>
    <span style="color: #ff0000;">'user'</span><span style="color: #339933;">,</span>
    where <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>id <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;"># これ</span>
    order_by <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'id'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>と書くようにしています。もちろんSkinnyも学習すれば全然わかると思うので、細かいどうでもいいことかもしれませんが&#8230;</p>
<p>あとSkinnyと違う点は、</p>
<ul>
<li>SchemaクラスとレコードのRowクラスは分離されていますが、シンプルさを重視してあえて一緒のクラスにまとめているところ(Your::Model::UserみたいなクラスがSchema定義をしてさらにこれのインスタンスがRowオブジェクト)</li>
<li>Schemaクラスの書き方がSkinnyはinflateやutf8がルールベースで全テーブルに適用されますが、こっちは基本的にテーブルの各カラムごとに書くようにさせてます</li>
</ul>
<p>ところぐらいでしょうか。</p>
<p>最後に、Skinnyのコードはすごく綺麗で色々な部分を参考にさせてもらいました。作者の<a href="http://d.hatena.ne.jp/nekokak/">nekokakさん</a>、どうもありがとうございました。</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/357/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/357" />
	</item>
		<item>
		<title>PerlのDateTimeとTime::Pieceモジュールのベンチマーク</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/352?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl%25e3%2581%25aedatetime%25e3%2581%25a8timepiece%25e3%2583%25a2%25e3%2582%25b8%25e3%2583%25a5%25e3%2583%25bc%25e3%2583%25ab%25e3%2581%25ae%25e3%2583%2599%25e3%2583%25b3%25e3%2583%2581%25e3%2583%259e%25e3%2583%25bc%25e3%2582%25af</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/352#comments</comments>
		<pubDate>Sat, 05 Dec 2009 15:40:24 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=352</guid>
		<description><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book13309692', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6224958%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311427.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6224958%2F">【送料無料】初めてのPerl第5版</a></p><p class="rakuten_caption">ランダル・L．シュウォーツ トム・フェニックス オーム社 オーム社発行年月：2009年10月 ページ数：393p サイズ：単行本 ISBN：9784873114……</p><p><em>販売価格：</em> 3780 円 <font class="rakuten_time">(12/02/10 02:53 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		Perlで日付関連の処理をする代表的なモジュールにDateTimeというものがありますが、メモリ消費量が激しいのがずっと気になっていました。でで、Time::Pieceが5.10.1からPerlに標準添付になったという話 [...]]]></description>
			<content:encoded><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book13309692', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6224958%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311427.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F6224958%2F">【送料無料】初めてのPerl第5版</a></p><p class="rakuten_caption">ランダル・L．シュウォーツ トム・フェニックス オーム社 オーム社発行年月：2009年10月 ページ数：393p サイズ：単行本 ISBN：9784873114……</p><p><em>販売価格：</em> 3780 円 <font class="rakuten_time">(12/02/10 02:53 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		<p>Perlで日付関連の処理をする代表的なモジュールに<a href="http://search.cpan.org/dist/DateTime/lib/DateTime.pm">DateTime</a>というものがありますが、メモリ消費量が激しいのがずっと気になっていました。でで、<a href="http://search.cpan.org/dist/Time-Piece/Piece.pm">Time::Piece</a>が5.10.1からPerlに標準添付になったという話を聞いて、乗り換えようかどうか検討しています。<a title="Perlメモ/Time::Pieceモジュール - Walrus, Digit." href="http://digit.que.ne.jp/work/wiki.cgi?Perl%E3%83%A1%E3%83%A2%2FTime%3A%3APiece%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB">Perlメモ/Time::Pieceモジュール &#8211; Walrus, Digit.</a>を見ると、DateTimeでできることは大体できるので、以下のユースケースでの速度面を測ってみます。</p>
<ul>
<li><a href="#use_memory">use 時のメモリ使用量</a></li>
<li><a href="#now">現在日時でオブジェクトを生成する</a></li>
<li><a href="#add">日付オブジェクトに対する日付の加算</a></li>
<li><a href="#subtract">日付オブジェクト同士の減算(A &#8211; B して何日経ったかみたいな)</a></li>
<li><a href="#compare">日付オブジェクト同士の比較</a></li>
<li><a href="#stringify">日付オブジェクトを文字列にする</a></li>
</ul>
<p>環境は以下で、ベンチマークのスクリプトは最後に載せてあります。</p>
<ul>
<li>OS: Ubuntu 9.04 amd64</li>
<li>Perl: 5.10.0 x86_64-linux-gnu-thread-multi</li>
<li>DateTime 0.51</li>
<li>Time::Piece 1.15</li>
</ul>
<p><a id="use_memory"></a></p>
<h3>use 時のメモリ使用量</h3>
<p><a href="http://tech.lampetty.net/tech/index.php/archives/344#gtop">こちらのgtop.pl</a>を使って測ります。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ gtop.pl <span style="color: #ff0000;">'use DateTime'</span>
10.2M : use DateTime</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ gtop.pl <span style="color: #ff0000;">'use Time::Piece'</span>
2.5M : use Time::Piece</pre></div></div>

<p>おおお、なんと4分の1！</p>
<p><a id="now"></a></p>
<h3>現在日時でオブジェクトを生成する</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>benchmark_datetime.pl now
Benchmark: running now_datetime, now_time_piece <span style="color: #000000; font-weight: bold;">for</span> at least <span style="color: #000000;">3</span> CPU seconds...
now_datetime:  <span style="color: #000000;">4</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.03</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">3.03</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">3149.83</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">9544</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
now_time_piece:  <span style="color: #000000;">2</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.58</span> usr +  <span style="color: #000000;">0.53</span> sys =  <span style="color: #000000;">3.11</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">52694.86</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">163881</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                  Rate   now_datetime now_time_piece
now_datetime    <span style="color: #000000;">3150</span><span style="color: #000000; font-weight: bold;">/</span>s             <span style="color: #660033;">--</span>           -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>
now_time_piece <span style="color: #000000;">52695</span><span style="color: #000000; font-weight: bold;">/</span>s          <span style="color: #000000;">1573</span><span style="color: #000000; font-weight: bold;">%</span>             <span style="color: #660033;">--</span></pre></div></div>

<p>15倍！</p>
<p><a id="add"></a></p>
<h3>日付オブジェクトに対する日付の加算</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>benchmark_datetime.pl add
Benchmark: running add_datetime, add_time_piece <span style="color: #000000; font-weight: bold;">for</span> at least <span style="color: #000000;">3</span> CPU seconds...
add_datetime:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.05</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">3.05</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1853.44</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">5653</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
add_time_piece:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.76</span> usr +  <span style="color: #000000;">0.42</span> sys =  <span style="color: #000000;">3.18</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">54969.81</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">174804</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                  Rate   add_datetime add_time_piece
add_datetime    <span style="color: #000000;">1853</span><span style="color: #000000; font-weight: bold;">/</span>s             <span style="color: #660033;">--</span>           -<span style="color: #000000;">97</span><span style="color: #000000; font-weight: bold;">%</span>
add_time_piece <span style="color: #000000;">54970</span><span style="color: #000000; font-weight: bold;">/</span>s          <span style="color: #000000;">2866</span><span style="color: #000000; font-weight: bold;">%</span>             <span style="color: #660033;">--</span></pre></div></div>

<p>Time::Pieceの圧倒的勝利。28倍！</p>
<p><a id="subtract"></a></p>
<h3>日付オブジェクト同士の減算</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>benchmark_datetime.pl subtract
Benchmark: running subtract_datetime, subtract_time_piece <span style="color: #000000; font-weight: bold;">for</span> at least <span style="color: #000000;">3</span> CPU seconds...
subtract_datetime:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.00</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">3.00</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">4065.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">12195</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
subtract_time_piece:  <span style="color: #000000;">4</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.15</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">3.15</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">132329.84</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">416839</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                        Rate   subtract_datetime subtract_time_piece
subtract_datetime     <span style="color: #000000;">4065</span><span style="color: #000000; font-weight: bold;">/</span>s                  <span style="color: #660033;">--</span>                -<span style="color: #000000;">97</span><span style="color: #000000; font-weight: bold;">%</span>
subtract_time_piece <span style="color: #000000;">132330</span><span style="color: #000000; font-weight: bold;">/</span>s               <span style="color: #000000;">3155</span><span style="color: #000000; font-weight: bold;">%</span>                  <span style="color: #660033;">--</span></pre></div></div>

<p>引き算もTime::Pieceの方が圧倒的に速いですね。</p>
<p><a id="compare"></a></p>
<h3>日付オブジェクト同士の比較</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>benchmark_datetime.pl compare
Benchmark: running compare_datetime, compare_time_piece <span style="color: #000000; font-weight: bold;">for</span> at least <span style="color: #000000;">3</span> CPU seconds...
compare_datetime:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.04</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">3.04</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">47709.21</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">145036</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
compare_time_piece:  <span style="color: #000000;">4</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.09</span> usr + -<span style="color: #000000;">0.01</span> sys =  <span style="color: #000000;">3.08</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">173623.70</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">534761</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                       Rate   compare_datetime compare_time_piece
compare_datetime    <span style="color: #000000;">47709</span><span style="color: #000000; font-weight: bold;">/</span>s                 <span style="color: #660033;">--</span>               -<span style="color: #000000;">73</span><span style="color: #000000; font-weight: bold;">%</span>
compare_time_piece <span style="color: #000000;">173624</span><span style="color: #000000; font-weight: bold;">/</span>s               <span style="color: #000000;">264</span><span style="color: #000000; font-weight: bold;">%</span>                 <span style="color: #660033;">--</span></pre></div></div>

<p><a id="stringify"></a></p>
<h3>日付オブジェクトを文字列にする</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>benchmark_datetime.pl stringify
Benchmark: running stringify_datetime, stringify_time_piece <span style="color: #000000; font-weight: bold;">for</span> at least <span style="color: #000000;">3</span> CPU seconds...
stringify_datetime:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.00</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">3.00</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">44470.33</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">133411</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
stringify_time_piece:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.45</span> usr +  <span style="color: #000000;">0.66</span> sys =  <span style="color: #000000;">3.11</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">116681.35</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">362879</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                         Rate   stringify_datetime stringify_time_piece
stringify_datetime    <span style="color: #000000;">44470</span><span style="color: #000000; font-weight: bold;">/</span>s                   <span style="color: #660033;">--</span>                 -<span style="color: #000000;">62</span><span style="color: #000000; font-weight: bold;">%</span>
stringify_time_piece <span style="color: #000000;">116681</span><span style="color: #000000; font-weight: bold;">/</span>s                 <span style="color: #000000;">162</span><span style="color: #000000; font-weight: bold;">%</span>                   <span style="color: #660033;">--</span></pre></div></div>

<h3>まとめ</h3>
<p>総じてDateTimeよりTime::Pieceの方が高い性能を叩き出しました。速度にシビアな状況ではTime::Pieceを使った方が良いと感じました。<a href="http://blog.nomadscafe.jp/archives/000514.html">こういう罠</a>もあるみたいなので気をつけなくてはいけないところもありますが&#8230; インタフェースはどちらも綺麗に出来ているので使い勝手としては同じぐらいかなと思います。それにしてももっと速くTime::Pieceを検証しておけばよかったと思う今日この頃です。</p>
<h3>ベンチマークスクリプト(benchmark_datetime.pl)</h3>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Benchmark <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>cmpthese timethese<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DateTime<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Piece</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Seconds</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$timezone</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">TimeZone</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'local'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #666666; font-style: italic;"># now</span>
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #000000; font-weight: bold;">sub</span> now_datetime <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$timezone</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> now_time_piece <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now</span> <span style="color: #339933;">=</span> <span style="color: #000066;">localtime</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #666666; font-style: italic;"># add</span>
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$add_dt</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$timezone</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> add_datetime <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$add_dt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">add</span><span style="color: #009900;">&#40;</span>days <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$add_tp</span> <span style="color: #339933;">=</span> <span style="color: #000066;">localtime</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> add_time_piece <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$add_tp</span> <span style="color: #339933;">+=</span> ONE_DAY<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #666666; font-style: italic;"># subtract</span>
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sub_dt1</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$timezone</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sub_dt2</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
    year <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2008</span><span style="color: #339933;">,</span>
    month <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12</span><span style="color: #339933;">,</span>
    day <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> subtract_datetime <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dur</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sub_dt1</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">subtract_datetime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sub_dt2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sub_tp1</span> <span style="color: #339933;">=</span> <span style="color: #000066;">localtime</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sub_tp2</span> <span style="color: #339933;">=</span> Time<span style="color: #339933;">::</span><span style="color: #006600;">Piece</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strptime</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'2008-12-01'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'%Y-%m-%d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> subtract_time_piece <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sec</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$sub_tp1</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$sub_tp2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #666666; font-style: italic;"># compare</span>
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$compare_dt1</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$timezone</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$compare_dt2</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$timezone</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> compare_datetime <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">compare</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$compare_dt1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$compare_dt2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$compare_tp1</span> <span style="color: #339933;">=</span> <span style="color: #000066;">localtime</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$compare_tp2</span> <span style="color: #339933;">=</span> <span style="color: #000066;">localtime</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> compare_time_piece <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$compare_tp1</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000ff;">$compare_tp2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #666666; font-style: italic;"># stringify</span>
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_dt</span> <span style="color: #339933;">=</span> DateTime<span style="color: #339933;">-&gt;</span><span style="color: #006600;">now</span><span style="color: #009900;">&#40;</span>time_zone <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$timezone</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> stringify_datetime <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$now_dt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%Y-%m-%d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$now_tp</span> <span style="color: #339933;">=</span> <span style="color: #000066;">localtime</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> stringify_time_piece <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$now_tp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%Y-%m-%d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #666666; font-style: italic;"># main</span>
<span style="color: #666666; font-style: italic;">#----------------------#</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mode</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">||</span> <span style="color: #ff0000;">'now'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">||</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mode</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'now'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cmpthese timethese <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'now_datetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;now_datetime</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'now_time_piece'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;now_time_piece</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mode</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'add'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cmpthese timethese <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'add_datetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;add_datetime</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'add_time_piece'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;add_time_piece</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mode</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'subtract'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cmpthese timethese <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'subtract_datetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;subtract_datetime</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'subtract_time_piece'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;subtract_time_piece</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mode</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'compare'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cmpthese timethese <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'compare_datetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;compare_datetime</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'compare_time_piece'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;compare_time_piece</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elsif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mode</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'stringify'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cmpthese timethese <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'stringify_datetime'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;stringify_datetime</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'stringify_time_piece'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;stringify_time_piece</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><div id="book13309692" class="rakuten_details">Now Loading...</div></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/352/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/352" />
	</item>
		<item>
		<title>Text::MicroTemplate 0.10で速度が速くなっている件</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/350?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=textmicrotemplate-0-10%25e3%2581%25a7%25e9%2580%259f%25e5%25ba%25a6%25e3%2581%258c%25e9%2580%259f%25e3%2581%258f%25e3%2581%25aa%25e3%2581%25a3%25e3%2581%25a6%25e3%2581%2584%25e3%2582%258b%25e4%25bb%25b6</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/350#comments</comments>
		<pubDate>Sat, 21 Nov 2009 02:37:11 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=350</guid>
		<description><![CDATA[Now Text::MicroTemplate is even faster than HTML::Template::Pro &#8211; use GFx::WebLog; Text::MicroTemplate 0 [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Now Text::MicroTemplate is even faster than HTML::Template::Pro - use GFx::WebLog;" href="http://d.hatena.ne.jp/gfx/20091120/1258695709">Now Text::MicroTemplate is even faster than HTML::Template::Pro &#8211; use GFx::WebLog;</a></p>
<p>Text::MicroTemplate 0.10で動作速度が改善されたとのことなので、自分のところでもベンチマークしてみました。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> benchmark_templates.pl <span style="color: #000000;">1</span>
Perl<span style="color: #000000; font-weight: bold;">/</span>5.10.0 <span style="color: #7a0874; font-weight: bold;">&#40;</span>x86_64-linux-gnu-thread-multi<span style="color: #7a0874; font-weight: bold;">&#41;</span>
HTML::Template<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.9</span>
HTML::Template::Compiled<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.94</span>
HTML::Template::Pro<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.92</span>
Template<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2.20</span>
Text::MicroTemplate<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0.10</span>
Benchmark: running HT, HT::C, HT::Pro, MT, TT <span style="color: #000000; font-weight: bold;">for</span> at least <span style="color: #000000;">1</span> CPU seconds...
        HT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">1.03</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">1.03</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1094.17</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">1127</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
     HT::C:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">1.03</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">1.03</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">11598.06</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">11946</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">2</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.82</span> usr +  <span style="color: #000000;">0.29</span> sys =  <span style="color: #000000;">1.11</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">17611.71</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">19549</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.96</span> usr +  <span style="color: #000000;">0.08</span> sys =  <span style="color: #000000;">1.04</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">15904.81</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">16541</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">1.08</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">1.08</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">7110.19</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">7679</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
           Rate      HT      TT   HT::C      MT HT::Pro
HT       <span style="color: #000000;">1094</span><span style="color: #000000; font-weight: bold;">/</span>s      <span style="color: #660033;">--</span>    -<span style="color: #000000;">85</span><span style="color: #000000; font-weight: bold;">%</span>    -<span style="color: #000000;">91</span><span style="color: #000000; font-weight: bold;">%</span>    -<span style="color: #000000;">93</span><span style="color: #000000; font-weight: bold;">%</span>    -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>
TT       <span style="color: #000000;">7110</span><span style="color: #000000; font-weight: bold;">/</span>s    <span style="color: #000000;">550</span><span style="color: #000000; font-weight: bold;">%</span>      <span style="color: #660033;">--</span>    -<span style="color: #000000;">39</span><span style="color: #000000; font-weight: bold;">%</span>    -<span style="color: #000000;">55</span><span style="color: #000000; font-weight: bold;">%</span>    -<span style="color: #000000;">60</span><span style="color: #000000; font-weight: bold;">%</span>
HT::C   <span style="color: #000000;">11598</span><span style="color: #000000; font-weight: bold;">/</span>s    <span style="color: #000000;">960</span><span style="color: #000000; font-weight: bold;">%</span>     <span style="color: #000000;">63</span><span style="color: #000000; font-weight: bold;">%</span>      <span style="color: #660033;">--</span>    -<span style="color: #000000;">27</span><span style="color: #000000; font-weight: bold;">%</span>    -<span style="color: #000000;">34</span><span style="color: #000000; font-weight: bold;">%</span>
MT      <span style="color: #000000;">15905</span><span style="color: #000000; font-weight: bold;">/</span>s   <span style="color: #000000;">1354</span><span style="color: #000000; font-weight: bold;">%</span>    <span style="color: #000000;">124</span><span style="color: #000000; font-weight: bold;">%</span>     <span style="color: #000000;">37</span><span style="color: #000000; font-weight: bold;">%</span>      <span style="color: #660033;">--</span>    -<span style="color: #000000;">10</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro <span style="color: #000000;">17612</span><span style="color: #000000; font-weight: bold;">/</span>s   <span style="color: #000000;">1510</span><span style="color: #000000; font-weight: bold;">%</span>    <span style="color: #000000;">148</span><span style="color: #000000; font-weight: bold;">%</span>     <span style="color: #000000;">52</span><span style="color: #000000; font-weight: bold;">%</span>     <span style="color: #000000;">11</span><span style="color: #000000; font-weight: bold;">%</span>      <span style="color: #660033;">--</span></pre></div></div>

<p>何回かやったのですが、自分の環境ではまだHTML::Template::Proが速いみたい。でも僅差なのでまったく気にならないレベルですね。とにかくgfxさん++すぎる。</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/350/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/350" />
	</item>
		<item>
		<title>Text::MicroTemplateとHTML::Template::Proの比較</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/348?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=textmicrotemplate%25e3%2581%25a8htmltemplatepro%25e3%2581%25ae%25e6%25af%2594%25e8%25bc%2583</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/348#comments</comments>
		<pubDate>Sat, 21 Nov 2009 01:56:04 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=348</guid>
		<description><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book11294491', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F1705851%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311202.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F1705851%2F">【送料無料】Perlクックブック（1（volume　1））第2版</a></p><p class="rakuten_caption">トム・クリスチャンセン ネイザン・トーキントン オーム社 オーム社発行年月：2004年09月 ページ数：687， サイズ：単行本 ISBN：9784873112……</p><p><em>販売価格：</em> 5145 円 <font class="rakuten_time">(12/02/10 06:20 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		PerlでWebアプリを作る時のテンプレートエンジンをどれにしようか相変わらず模索中なのですが、以下の2つのエントリーで動作速度とメモリについて調べた結果、どうやらText::MicroTemplateとHTML::Te [...]]]></description>
			<content:encoded><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book11294491', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F1705851%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311202.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F1705851%2F">【送料無料】Perlクックブック（1（volume　1））第2版</a></p><p class="rakuten_caption">トム・クリスチャンセン ネイザン・トーキントン オーム社 オーム社発行年月：2004年09月 ページ数：687， サイズ：単行本 ISBN：9784873112……</p><p><em>販売価格：</em> 5145 円 <font class="rakuten_time">(12/02/10 06:20 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		<p>PerlでWebアプリを作る時のテンプレートエンジンをどれにしようか相変わらず模索中なのですが、以下の2つのエントリーで動作速度とメモリについて調べた結果、どうやらText::MicroTemplateとHTML::Template::Proのどちらかを選択するのが良いだろうと思っています。</p>
<ul>
<li><a href="http://tech.lampetty.net/tech/index.php/archives/342">Text::MicroTemplateの速度を簡単にベンチマーク</a></li>
<li><a href="http://tech.lampetty.net/tech/index.php/archives/344">Text::MicroTemplateのメモリ使用量をベンチマーク</a></li>
</ul>
<h3>Text::MicroTemplateの良い点</h3>
<ul>
<li>Perlのコードが書けるので自由度が高い</li>
<li>そこそこ速い</li>
<li>省メモリ</li>
<li>デフォルトでHTMLエスケープしてくれるのでセキュリティ的にもグッド</li>
<li>コードがシンプルなので拡張しやすい</li>
<li>loopやifで改行の制御が出来る(<? 〜 ?>と ? )</li>
</ul>
<h3>HTML::Template::Proの良い点</h3>
<ul>
<li>文法がシンプル</li>
<li>爆速</li>
<li>そこそこ省メモリ</li>
<li>register_functionでテンプレート内から呼び出せる関数が登録できるので、拡張性はそれなりにある</li>
</ul>
<h3>Text::MicroTemplateのイマイチな点</h3>
<ul>
<li>自由度が高い分、色々できてしまう(Viewにロジックをガリガリ書いたりとか)</li>
<li>Includeがデフォルトでは出来ない(Text::MicroTemplate::Extended使えば似たようなことはできる)</li>
</ul>
<h3>HTML::Template::Proのイマイチな点</h3>
<ul>
<li>&lt;TMPL_IF&gt;、&lt;TMPL_LOOP&gt;などの制御を書く時の改行のコントロールが難しい</li>
<li>デフォルトではHTMLエスケープがかからないので、自前で自動エスケープのロジックを書くかテンプレート側で毎度エスケープしなくてはいけない</li>
</ul>
<p>「&lt;TMPL_IF&gt;、&lt;TMPL_LOOP&gt;で改行のコントロールが難しい」というのは、例えば以下のテンプレートファイルがあって、それを実際に出力した時のHTMLに改行が入ってしまう、ということです。</p>
<p><strong>テンプレートファイル</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;TMPL_IF <span style="color: #000066;">NAME</span><span style="color: #66cc66;">=</span>hoge&gt;</span>
hoge
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>TMPL_IF&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><strong>出力されるHTML</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
#ここに改行
hoge
#ここにも改行
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>出力するのがHTMLであればそんなに問題はないのですが、メールの文面をテンプレートエンジンで動的に生成したい場合、改行が実直に反映されてしまうので、いつもどうしようか悩んでしまいます。この改行のためにHTMLの生成とメールの生成で別々のテンプレートエンジン使うのも微妙ですし。</p>
<p>もしかするとHTML::Template::Proの filter 機能でなんとかできるのかもしれませんが、まだそこまでは考えられてないです。というわけで相変わらずテンプレートエンジンどうしようか悩み中&#8230;</p>
<p><div id="book11294491" class="rakuten_details">Now Loading...</div></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/348/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/348" />
	</item>
		<item>
		<title>Text::MicroTemplateのメモリ使用量をベンチマーク</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/344?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=textmicrotemplate%25e3%2581%25ae%25e3%2583%25a1%25e3%2583%25a2%25e3%2583%25aa%25e4%25bd%25bf%25e7%2594%25a8%25e9%2587%258f%25e3%2582%2592%25e3%2583%2599%25e3%2583%25b3%25e3%2583%2581%25e3%2583%259e%25e3%2583%25bc%25e3%2582%25af</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/344#comments</comments>
		<pubDate>Sun, 15 Nov 2009 13:58:31 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=344</guid>
		<description><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book11995194', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311314.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F">【送料無料】PERL　HACKS</a></p><p class="rakuten_caption">プロが教えるテクニック＆ツール101選 クロマチック ダミアン・コンウェイ オーム社 オーム社発行年月：2007年01月 ページ数：327p サイズ：単行本 I……</p><p><em>販売価格：</em> 3150 円 <font class="rakuten_time">(12/02/10 09:24 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		Text::MicroTemplateの速度を簡単にベンチマークでText::MicroTemplateの動作速度を簡単にベンチマークしましたが、今回はメモリ使用量を他のテンプレートエンジンと比較してみました。環境はUb [...]]]></description>
			<content:encoded><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book11995194', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311314.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F">【送料無料】PERL　HACKS</a></p><p class="rakuten_caption">プロが教えるテクニック＆ツール101選 クロマチック ダミアン・コンウェイ オーム社 オーム社発行年月：2007年01月 ページ数：327p サイズ：単行本 I……</p><p><em>販売価格：</em> 3150 円 <font class="rakuten_time">(12/02/10 09:24 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		<p><a href="http://tech.lampetty.net/tech/index.php/archives/342">Text::MicroTemplateの速度を簡単にベンチマーク</a>でText::MicroTemplateの動作速度を簡単にベンチマークしましたが、今回はメモリ使用量を他のテンプレートエンジンと比較してみました。環境はUbuntu 9.04 amd64, Perl 5.10.0 で、<a href="http://search.cpan.org/dist/GTop/">GTop</a>を使って測っています。</p>
<h3>useした時のメモリ使用量</h3>
<p>独自に作ったスクリプト(use_memory_size.pl)でuse時のメモリ使用量を測ってみます。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>use_memory_size.pl
 3.2M : use HTML::Template
11.7M : use HTML::Template::Compiled
 2.5M : use HTML::Template::Pro
 264k : use Text::MicroTemplate
 3.6M : use Template</pre></div></div>

<p>HTML::Template::Compiledは多くのモジュールで構成されているので、メモリ使用量が多そうな感じです。一方、ソースコードがとてもコンパクトな Text::MicroTemplate は他のテンプレートエンジンと比べてメモリ使用量が一桁少ない。これはすごい。</p>
<p><strong>use_memory_size.pl</strong></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$module</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span>
                   HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Compiled</span>
                   HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Pro</span>
                   Text<span style="color: #339933;">::</span><span style="color: #006600;">MicroTemplate</span>
                   Template<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066;">system</span> <span style="color: #ff0000;">&quot;gtop.pl 'use $module'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a id="gtop"></a><br />
(プロセスのメモリ使用量を調べるために必要なGTopモジュール(およびlibgtop2)をインストールして、下記スクリプトをgtop.plとしてどこかパスの通ってるところに保存しておいてください)<br />
<strong>gtop.pl</strong></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> GTop<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #ff0000;">&quot;Argument required.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$gtop</span> <span style="color: #339933;">=</span> GTop<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$before</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$gtop</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">proc_mem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$$</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">size</span><span style="color: #339933;">;</span>
<span style="color: #000066;">eval</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">die</span> <span style="color: #0000ff;">$@</span> <span style="color: #b1b100;">if</span> <span style="color: #0000ff;">$@</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$after</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$gtop</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">proc_mem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$$</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">size</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$diff</span> <span style="color: #339933;">=</span> GTop<span style="color: #339933;">::</span><span style="color: #006600;">size_string</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$after</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">$before</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;$diff : $ARGV[0]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>newしてレンダリングさせた後のメモリ使用量</h3>
<p>モジュールをuseした時のメモリ使用量だけでなく、newしてoutputした後のメモリ使用量も調べてみました。結果は以下になります。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>new_memory_size.pl 
 3.2M : use HTML::Template;my <span style="color: #007800;">$t</span> = HTML::Template-<span style="color: #000000; font-weight: bold;">&gt;</span>new<span style="color: #7a0874; font-weight: bold;">&#40;</span>filename =<span style="color: #000000; font-weight: bold;">&gt;</span> q<span style="color: #7a0874; font-weight: bold;">&#123;</span>template_speed_file.ht<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;my <span style="color: #007800;">$s</span> = <span style="color: #007800;">$t</span>-<span style="color: #000000; font-weight: bold;">&gt;</span>output;
11.7M : use HTML::Template::Compiled;my <span style="color: #007800;">$t</span> = HTML::Template::Compiled-<span style="color: #000000; font-weight: bold;">&gt;</span>new<span style="color: #7a0874; font-weight: bold;">&#40;</span>filename =<span style="color: #000000; font-weight: bold;">&gt;</span> q<span style="color: #7a0874; font-weight: bold;">&#123;</span>template_speed_file.ht<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;my <span style="color: #007800;">$s</span> = <span style="color: #007800;">$t</span>-<span style="color: #000000; font-weight: bold;">&gt;</span>output;
 2.5M : use HTML::Template::Pro;my <span style="color: #007800;">$t</span> = HTML::Template::Pro-<span style="color: #000000; font-weight: bold;">&gt;</span>new<span style="color: #7a0874; font-weight: bold;">&#40;</span>filename =<span style="color: #000000; font-weight: bold;">&gt;</span> q<span style="color: #7a0874; font-weight: bold;">&#123;</span>template_speed_file.ht<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;my <span style="color: #007800;">$s</span> = <span style="color: #007800;">$t</span>-<span style="color: #000000; font-weight: bold;">&gt;</span>output;
 796k : use Text::MicroTemplate::File;my <span style="color: #007800;">$t</span> = Text::MicroTemplate::File-<span style="color: #000000; font-weight: bold;">&gt;</span>new;my <span style="color: #007800;">$s</span> = <span style="color: #007800;">$t</span>-<span style="color: #000000; font-weight: bold;">&gt;</span>render_file<span style="color: #7a0874; font-weight: bold;">&#40;</span>q<span style="color: #7a0874; font-weight: bold;">&#123;</span>template_speed_file.mt<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>-<span style="color: #000000; font-weight: bold;">&gt;</span>as_string<span style="color: #000000; font-weight: bold;">;;</span>
11.4M : use Template;my <span style="color: #007800;">$t</span> = Template-<span style="color: #000000; font-weight: bold;">&gt;</span>new;<span style="color: #007800;">$t</span>-<span style="color: #000000; font-weight: bold;">&gt;</span>process<span style="color: #7a0874; font-weight: bold;">&#40;</span>q<span style="color: #7a0874; font-weight: bold;">&#123;</span>template_speed_file.tt<span style="color: #7a0874; font-weight: bold;">&#125;</span>, <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span>, \my <span style="color: #007800;">$s</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>;</pre></div></div>

<p>
相変わらずText::MicroTemplateは良好な成績ですね。あまりメモリを積んでいないマシンでもこれなら安心して使えそうな感じです。HTML::Template::Proも2.5MBと省メモリですね。スピードも爆速なので、テンプレートにゴリゴリPerlのコードを書く必要がない場合はこれが一番よいかもしれません。
</p>
<p><strong>new_memory_size.pl</strong></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># HTML::Template</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ht</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">'...'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>filename <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">q</span><span style="color: #009900;">&#123;</span>template_speed_file<span style="color: #339933;">.</span>ht<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># HTML::Template::Compiled</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$htc</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">'...'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Compiled</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Compiled</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>filename <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">q</span><span style="color: #009900;">&#123;</span>template_speed_file<span style="color: #339933;">.</span>ht<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># HTML::Template::Pro</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$htp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">'...'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Pro</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Pro</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>filename <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">q</span><span style="color: #009900;">&#123;</span>template_speed_file<span style="color: #339933;">.</span>ht<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Text::MicroTemplate</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mt</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">'...'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Text<span style="color: #339933;">::</span><span style="color: #006600;">MicroTemplate</span><span style="color: #339933;">::</span><span style="color: #006600;">File</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> Text<span style="color: #339933;">::</span><span style="color: #006600;">MicroTemplate</span><span style="color: #339933;">::</span><span style="color: #006600;">File</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">render_file</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">q</span><span style="color: #009900;">&#123;</span>template_speed_file<span style="color: #339933;">.</span>mt<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">as_string</span><span style="color: #339933;">;;</span>
<span style="color: #339933;">...</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Template</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tt</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">'...'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Template<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$t</span> <span style="color: #339933;">=</span> Template<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$t</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">process</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">q</span><span style="color: #009900;">&#123;</span>template_speed_file<span style="color: #339933;">.</span>tt<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> \<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">...</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$code</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$ht</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$htc</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$htp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$mt</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$tt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$code</span> <span style="color: #339933;">=~</span> <span style="color: #009966; font-style: italic;">s/\n//g</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">system</span> <span style="color: #000066;">qq</span><span style="color: #009900;">&#123;</span>gtop<span style="color: #339933;">.</span>pl <span style="color: #ff0000;">'$code'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><div id="book11995194" class="rakuten_details">Now Loading...</div></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/344/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/344" />
	</item>
		<item>
		<title>Text::MicroTemplateの速度を簡単にベンチマーク</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/342?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=textmicrotemplate%25e3%2581%25ae%25e9%2580%259f%25e5%25ba%25a6%25e3%2582%2592%25e7%25b0%25a1%25e5%258d%2598%25e3%2581%25ab%25e3%2583%2599%25e3%2583%25b3%25e3%2583%2581%25e3%2583%259e%25e3%2583%25bc%25e3%2582%25af</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/342#comments</comments>
		<pubDate>Sat, 14 Nov 2009 16:21:34 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=342</guid>
		<description><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book11995194', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311314.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F">【送料無料】PERL　HACKS</a></p><p class="rakuten_caption">プロが教えるテクニック＆ツール101選 クロマチック ダミアン・コンウェイ オーム社 オーム社発行年月：2007年01月 ページ数：327p サイズ：単行本 I……</p><p><em>販売価格：</em> 3150 円 <font class="rakuten_time">(12/02/10 09:24 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		2009/11/15 追記：tokuhiromさんのコメントを受けて、Text::MicroTemplate以外のテンプレートファイルにHTMLエスケープの処理を追加してベンチマークを取り直しています。 Perlのテンプ [...]]]></description>
			<content:encoded><![CDATA[
		<script type="text/javascript"><!--
			show_rakuten('#book11995194', '<!-- cache --><div class="rakuten_image"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F"><img src="http://thumbnail.image.rakuten.co.jp/@0_mall/book/cabinet/8731/87311314.jpg?_ex=128x128"><br /><p class="rakuten_logo"><!-- Rakuten Web Services Attribution Snippet FROM HERE --><a href="http://webservice.rakuten.co.jp/" target="_blank"><img src="http://webservice.rakuten.co.jp/img/credit/200709/credit_4936.gif" border="0" alt="楽天ウェブサービスセンター" title="楽天ウェブサービスセンター" width="49" height="36"/></a><!-- Rakuten Web Services Attribution Snippet TO HERE --></p><font class="rakuten_warn"><a href="#" class="tooltip">[ご利用にあたって]<span>このサイトで掲載されている情報は、「おいぬま日報」の作成者により運営されています。価格、販売可能情報は、変更される場合があります。購入時に楽天市場店舗（www.rakuten.co.jp）に表示されている価格が、その商品の販売に適用されます。</span></a></font></div><div class="rakuten_info"><p class="rakuten_itemname"><a href="http://hb.afl.rakuten.co.jp/hgc/0e871b08.72e65fa8.0e871b0a.296b5a1b/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fbook%2F4277413%2F">【送料無料】PERL　HACKS</a></p><p class="rakuten_caption">プロが教えるテクニック＆ツール101選 クロマチック ダミアン・コンウェイ オーム社 オーム社発行年月：2007年01月 ページ数：327p サイズ：単行本 I……</p><p><em>販売価格：</em> 3150 円 <font class="rakuten_time">(12/02/10 09:24 更新)</font></p><p><em>販売店舗：</em> <a href="http://www.rakuten.co.jp/book/">楽天ブックス</a></p></div><!-- cache -->');
		// -->
		</script>
		<p><strong>2009/11/15 追記</strong>：<a href="#comment-152">tokuhiromさんのコメント</a>を受けて、Text::MicroTemplate以外のテンプレートファイルにHTMLエスケープの処理を追加してベンチマークを取り直しています。</p>
<p>Perlのテンプレートエンジンでどれを使おうか悩んでいるので、Text::MicroTemplateをはじめとする以下のテンプレートエンジンの速度を簡単にベンチマークしてみました。</p>
<ul>
<li><a href="http://search.cpan.org/dist/Text-MicroTemplate/lib/Text/MicroTemplate.pm">Text::MicroTemplate</a></li>
<li><a href="http://search.cpan.org/dist/HTML-Template/Template.pm">HTML::Template</a></li>
<li><a href="http://search.cpan.org/dist/HTML-Template-Pro/lib/HTML/Template/Pro.pm">HTML::Template::Pro</a></li>
<li><a href="http://search.cpan.org/dist/HTML-Template-Compiled/lib/HTML/Template/Compiled.pm">HTML::Template::Compiled</a> (PODにはHTML::Templateの3-7倍速いと書かれている)</li>
<li><a href="http://search.cpan.org/dist/Template-Toolkit/lib/Template.pm">Template-Toolkit</a></li>
</ul>
<p>下記の要因で単純に比較できない部分もあるので、これを鵜呑みにしないでなるべく自身のユースケースの延長でもベンチマークを取ることをお奨めします。あくまで参考値ということで。</p>
<ul>
<li>そもそも持っている機能が全然違うので単純に比較できない</li>
<li>実際のテンプレートファイルはもっと複雑なはず</li>
<li>utf8 flagの処理入れる場合もある</li>
</ul>
<p>ではとりあえず結果から。</p>
<h3>キャッシュなし</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> template_speed_file.pl <span style="color: #000000;">3000</span> <span style="color: #000000;">0</span>
Benchmark: timing <span style="color: #000000;">3000</span> iterations of HT, HT::Compiled, HT::Pro, MT, TT...
        HT:  <span style="color: #000000;">5</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">4.60</span> usr +  <span style="color: #000000;">0.09</span> sys =  <span style="color: #000000;">4.69</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">639.66</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
HT::Compiled: <span style="color: #000000;">10</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">10.56</span> usr +  <span style="color: #000000;">0.10</span> sys = <span style="color: #000000;">10.66</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">281.43</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.20</span> usr +  <span style="color: #000000;">0.04</span> sys =  <span style="color: #000000;">0.24</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">12500.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">2</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.39</span> usr +  <span style="color: #000000;">0.07</span> sys =  <span style="color: #000000;">2.46</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1219.51</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT: <span style="color: #000000;">10</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">9.65</span> usr +  <span style="color: #000000;">0.17</span> sys =  <span style="color: #000000;">9.82</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">305.50</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                Rate HT::Compiled          TT          HT          MT    HT::Pro
HT::Compiled   <span style="color: #000000;">281</span><span style="color: #000000; font-weight: bold;">/</span>s           <span style="color: #660033;">--</span>         -<span style="color: #000000;">8</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">56</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">77</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>
TT             <span style="color: #000000;">305</span><span style="color: #000000; font-weight: bold;">/</span>s           <span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">52</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">75</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>
HT             <span style="color: #000000;">640</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">127</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">109</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">48</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">95</span><span style="color: #000000; font-weight: bold;">%</span>
MT            <span style="color: #000000;">1220</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">333</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">299</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">91</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>       -<span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro      <span style="color: #000000;">12500</span><span style="color: #000000; font-weight: bold;">/</span>s        <span style="color: #000000;">4342</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">3992</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">1854</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">925</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #660033;">--</span></pre></div></div>

<h3>キャッシュあり</h3>
<p>コードを見てもらえばわかるのですが、「キャッシュあり」とは具体的には</p>
<ul>
<li>テンプレートのインスタンスを作るのは最初の1回だけ(キャッシュなしの場合は毎回作っている)</li>
<li>テンプレートエンジンに対して、「キャッシュあり」とコンストラクタで指令</li>
</ul>
<p>ということになります。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> template_speed_file.pl <span style="color: #000000;">3000</span> <span style="color: #000000;">1</span>
Benchmark: timing <span style="color: #000000;">3000</span> iterations of HT, HT::Compiled, HT::Pro, MT, TT...
        HT:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.69</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">2.69</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1115.24</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
HT::Compiled:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.28</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">0.28</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">10714.29</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.13</span> usr +  <span style="color: #000000;">0.02</span> sys =  <span style="color: #000000;">0.15</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">20000.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.22</span> usr +  <span style="color: #000000;">0.01</span> sys =  <span style="color: #000000;">0.23</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">13043.48</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">1.84</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">1.84</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1630.43</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                Rate          HT          TT HT::Compiled          MT    HT::Pro
HT            <span style="color: #000000;">1115</span><span style="color: #000000; font-weight: bold;">/</span>s          <span style="color: #660033;">--</span>        -<span style="color: #000000;">32</span><span style="color: #000000; font-weight: bold;">%</span>         -<span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">91</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>
TT            <span style="color: #000000;">1630</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">46</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>         -<span style="color: #000000;">85</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">87</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">92</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Compiled <span style="color: #000000;">10714</span><span style="color: #000000; font-weight: bold;">/</span>s        <span style="color: #000000;">861</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">557</span><span style="color: #000000; font-weight: bold;">%</span>           <span style="color: #660033;">--</span>        -<span style="color: #000000;">18</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">46</span><span style="color: #000000; font-weight: bold;">%</span>
MT           <span style="color: #000000;">13043</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1070</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">700</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #000000;">22</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>       -<span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro      <span style="color: #000000;">20000</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1693</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">1127</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #000000;">87</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">53</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #660033;">--</span></pre></div></div>

<ul>
<li>HTML::Template::Proが爆速！</li>
<li>Text::MicroTemplateは、キャッシュなしだとそこそこ、<s>キャッシュありはかなり速い部類に入るようです。</s>キャッシュありだとHTML::Template::Proには及ばないものの、かなり速いです。</li>
<li>HTML::Template::Compiledはキャッシュを無効にするとTTより遅いという結果ですが、キャッシュを有効にすると猛烈に速くなるようです。ソースが追えてないのですが、中でガチンコにキャッシュしてるのでしょうか。
<li>たぶん一番使われているTTはよく「遅い」と言われていますが、キャッシュを有効にすると意外にもHTML::Templateより速くなりました。</li>
</ul>
<p>というのがポイントだと思います。この結果だけで判断すると、HTML::Template::Proは神がかり的な速さなのですが、そもそもPerlのコードをテンプレートに記述できず自由度は低いので(*1)、使う場合にはそれなりに不便さを覚悟した方がよいと思います。「便利さ」という点だとText::MicroTemplateはテンプレート内にPerlのコードが書ける分かなり自由度が高い、かつ速度もいい感じでバランスが取れていると言えます。</p>
<p>*1) Perlのコードをテンプレートに書けてしまっていいのかどうかは使う人次第だと思いますが</p>
<p>以下にソースを載せておきます。テストに使ったマシンはUbuntu 9.04, perl 5.10.0, CPUが Athlon Dual Core Processor 5050e です。</p>
<p><strong>template_speed_file.pl</strong></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Compiled</span> speed <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Pro</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Template<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Text<span style="color: #339933;">::</span><span style="color: #006600;">MicroTemplate</span><span style="color: #339933;">::</span><span style="color: #006600;">File</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> Benchmark <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>timethese cmpthese<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@LANGUAGES</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Perl'</span><span style="color: #339933;">,</span> ll <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Ruby'</span><span style="color: #339933;">,</span> ll <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Python'</span><span style="color: #339933;">,</span> ll <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'PHP'</span><span style="color: #339933;">,</span> ll <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Java'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'C'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'C++'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'C#'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'VB'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'VB.NET'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ASP.NET'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Delphi'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Erlang'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Scala'</span><span style="color: #339933;">,</span> ll <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> language <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Go'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$count</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">||</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$cache</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span> <span style="color: #339933;">||</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$mt</span> <span style="color: #339933;">=</span> <span style="color: #000066;">undef</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> mt <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%args</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>use_cache <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$mt</span> <span style="color: #339933;">||=</span> Text<span style="color: #339933;">::</span><span style="color: #006600;">MicroTemplate</span><span style="color: #339933;">::</span><span style="color: #006600;">File</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$mt</span> <span style="color: #339933;">=</span> Text<span style="color: #339933;">::</span><span style="color: #006600;">MicroTemplate</span><span style="color: #339933;">::</span><span style="color: #006600;">File</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$mt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">render_file</span><span style="color: #009900;">&#40;</span>
        <span style="color: #ff0000;">'template_speed_file.mt'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span>
            page_title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'LL Programming languages'</span><span style="color: #339933;">,</span>
            languages <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@LANGUAGES</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">as_string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ht</span> <span style="color: #339933;">=</span> <span style="color: #000066;">undef</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> ht <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%args</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
        filename <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'template_speed_file.ht'</span><span style="color: #339933;">,</span>
        case_sensitive <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        die_on_bad_params <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
        cache <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$cache</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$ht</span> <span style="color: #339933;">||=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$ht</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #0000ff;">$ht</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span>
        page_title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'LL Programming languages'</span><span style="color: #339933;">,</span>
        languages <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@LANGUAGES</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ht</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$htc</span> <span style="color: #339933;">=</span> <span style="color: #000066;">undef</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> htc <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%args</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
        filename <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'template_speed_file.ht'</span><span style="color: #339933;">,</span>
        case_sensitive <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        die_on_bad_params <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
        cache <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$cache</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$htc</span> <span style="color: #339933;">||=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Compiled</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$htc</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Compiled</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #0000ff;">$htc</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span>
        page_title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'LL Programming languages'</span><span style="color: #339933;">,</span>
        languages <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@LANGUAGES</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$htc</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$htp</span> <span style="color: #339933;">=</span> <span style="color: #000066;">undef</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> htp <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%args</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
        filename <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'template_speed_file.ht'</span><span style="color: #339933;">,</span>
        case_sensitive <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        die_on_bad_params <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
        cache <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$cache</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$htp</span> <span style="color: #339933;">||=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Pro</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$htp</span> <span style="color: #339933;">=</span> HTML<span style="color: #339933;">::</span><span style="color: #006600;">Template</span><span style="color: #339933;">::</span><span style="color: #006600;">Pro</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">%args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #0000ff;">$htp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">param</span><span style="color: #009900;">&#40;</span>
        page_title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'LL Programming languages'</span><span style="color: #339933;">,</span>
        languages <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@LANGUAGES</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$htp</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$tt</span> <span style="color: #339933;">=</span> <span style="color: #000066;">undef</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">sub</span> tt <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$cache</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$tt</span> <span style="color: #339933;">||=</span> Template<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span>
            CACHE_SIZE <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$cache</span> <span style="color: #339933;">?</span> <span style="color: #000066;">undef</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$tt</span> <span style="color: #339933;">=</span> Template<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #0000ff;">$tt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">process</span><span style="color: #009900;">&#40;</span>
        <span style="color: #ff0000;">'template_speed_file.tt'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#123;</span>
            page_title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'LL Programming languages'</span><span style="color: #339933;">,</span>
            languages <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\@LANGUAGES</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        \<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$out</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #0000ff;">$tt</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># main</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$comp</span> <span style="color: #339933;">=</span> timethese<span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'MT'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;mt</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'HT'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;ht</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'HT::Pro'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;htp</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'HT::Compiled'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;htc</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'TT'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\&amp;tt</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cmpthese <span style="color: #0000ff;">$comp</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>template_speed_file.mt &#8211; Text::MicroTemplate用のファイル</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">? my $p = $_[0];
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Programming languages<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;&lt;?<span style="color: #66cc66;">=</span> $p-&gt;</span>{page_title} ?&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
? for my $language (@{ $p-&gt;{languages} }) {
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;?<span style="color: #66cc66;">=</span> $language-&gt;</span>{language} ?&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
? }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><strong>template_speed_file.ht &#8211; HTML::Template::*用のファイル</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Programming languages<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;&lt;TMPL_VAR <span style="color: #000066;">NAME</span><span style="color: #66cc66;">=</span>page_title ESCAPE<span style="color: #66cc66;">=</span>HTML&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;TMPL_LOOP <span style="color: #000066;">NAME</span><span style="color: #66cc66;">=</span>languages&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;TMPL_IF <span style="color: #000066;">NAME</span><span style="color: #66cc66;">=</span>ll&gt;</span>*<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>TMPL_IF&gt;&lt;TMPL_VAR <span style="color: #000066;">NAME</span><span style="color: #66cc66;">=</span><span style="color: #000066;">language</span> ESCAPE<span style="color: #66cc66;">=</span>HTML&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>TMPL_LOOP&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p><strong>template_speed_file.tt &#8211; Template::Toolkit用のファイル</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Programming languages<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>[% page_title | html %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
[% FOREACH language IN languages %]
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>[% language.language | html %]<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
[% END %]
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<h3>HTMLエスケープあり/なしのスピードについて</h3>
<p><a href="#comment-152">tokuhiromさんのコメント</a>にあるように、Text::MicroTemplateは自動でHTMLエスケープがかかる一方、他のテンプレートエンジンでは手動でやる必要があるので、HTMLエスケープを入れて測り直してみました。ちなみに上に載せているベンチの結果は、HTMLエスケープを入れたものになっています。</p>
<p>特筆すべきなのは、HTML::Template::CompiledがキャッシュありかつHTMLエスケープありの場合、HTMLエスケープなしに比べてパフォーマンスがかなり劣化している点です。(その他はちょっとずつパフォーマンスが劣化していますね)。</p>
<p><strong>キャッシュなしかつHTMLエスケープなし</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> template_speed_file.pl <span style="color: #000000;">3000</span> <span style="color: #000000;">0</span>
Benchmark: timing <span style="color: #000000;">3000</span> iterations of HT, HT::Compiled, HT::Pro, MT, TT...
        HT:  <span style="color: #000000;">4</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">3.85</span> usr +  <span style="color: #000000;">0.10</span> sys =  <span style="color: #000000;">3.95</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">759.49</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
HT::Compiled: <span style="color: #000000;">10</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">9.99</span> usr +  <span style="color: #000000;">0.09</span> sys = <span style="color: #000000;">10.08</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">297.62</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.21</span> usr +  <span style="color: #000000;">0.02</span> sys =  <span style="color: #000000;">0.23</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">13043.48</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">2</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.35</span> usr +  <span style="color: #000000;">0.05</span> sys =  <span style="color: #000000;">2.40</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1250.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT:  <span style="color: #000000;">8</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">7.83</span> usr +  <span style="color: #000000;">0.15</span> sys =  <span style="color: #000000;">7.98</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">375.94</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                Rate HT::Compiled          TT          HT          MT    HT::Pro
HT::Compiled   <span style="color: #000000;">298</span><span style="color: #000000; font-weight: bold;">/</span>s           <span style="color: #660033;">--</span>        -<span style="color: #000000;">21</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">61</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">76</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>
TT             <span style="color: #000000;">376</span><span style="color: #000000; font-weight: bold;">/</span>s          <span style="color: #000000;">26</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">51</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">70</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">97</span><span style="color: #000000; font-weight: bold;">%</span>
HT             <span style="color: #000000;">759</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">155</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">102</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">39</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>
MT            <span style="color: #000000;">1250</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">320</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">233</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">65</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>       -<span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro      <span style="color: #000000;">13043</span><span style="color: #000000; font-weight: bold;">/</span>s        <span style="color: #000000;">4283</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">3370</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">1617</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">943</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #660033;">--</span></pre></div></div>

<p><strong>キャッシュなしかつHTMLエスケープあり</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Benchmark: timing <span style="color: #000000;">3000</span> iterations of HT, HT::Compiled, HT::Pro, MT, TT...
        HT:  <span style="color: #000000;">5</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">4.60</span> usr +  <span style="color: #000000;">0.09</span> sys =  <span style="color: #000000;">4.69</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">639.66</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
HT::Compiled: <span style="color: #000000;">10</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">10.56</span> usr +  <span style="color: #000000;">0.10</span> sys = <span style="color: #000000;">10.66</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">281.43</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.20</span> usr +  <span style="color: #000000;">0.04</span> sys =  <span style="color: #000000;">0.24</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">12500.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">2</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.39</span> usr +  <span style="color: #000000;">0.07</span> sys =  <span style="color: #000000;">2.46</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1219.51</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT: <span style="color: #000000;">10</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">9.65</span> usr +  <span style="color: #000000;">0.17</span> sys =  <span style="color: #000000;">9.82</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">305.50</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                Rate HT::Compiled          TT          HT          MT    HT::Pro
HT::Compiled   <span style="color: #000000;">281</span><span style="color: #000000; font-weight: bold;">/</span>s           <span style="color: #660033;">--</span>         -<span style="color: #000000;">8</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">56</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">77</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>
TT             <span style="color: #000000;">305</span><span style="color: #000000; font-weight: bold;">/</span>s           <span style="color: #000000;">9</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">52</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">75</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">98</span><span style="color: #000000; font-weight: bold;">%</span>
HT             <span style="color: #000000;">640</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">127</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">109</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">48</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">95</span><span style="color: #000000; font-weight: bold;">%</span>
MT            <span style="color: #000000;">1220</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">333</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">299</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">91</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>       -<span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro      <span style="color: #000000;">12500</span><span style="color: #000000; font-weight: bold;">/</span>s        <span style="color: #000000;">4342</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">3992</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">1854</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">925</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #660033;">--</span></pre></div></div>

<p><strong>キャッシュありかつHTMLエスケープなし</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> template_speed_file.pl <span style="color: #000000;">3000</span> <span style="color: #000000;">1</span>
Benchmark: timing <span style="color: #000000;">3000</span> iterations of HT, HT::Compiled, HT::Pro, MT, TT...
        HT:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.50</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">2.50</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1200.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
HT::Compiled:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.15</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">0.15</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">20000.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.10</span> usr +  <span style="color: #000000;">0.04</span> sys =  <span style="color: #000000;">0.14</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">21428.57</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.21</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">0.21</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">14285.71</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">1.19</span> usr +  <span style="color: #000000;">0.01</span> sys =  <span style="color: #000000;">1.20</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">2500.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                Rate          HT          TT          MT HT::Compiled    HT::Pro
HT            <span style="color: #000000;">1200</span><span style="color: #000000; font-weight: bold;">/</span>s          <span style="color: #660033;">--</span>        -<span style="color: #000000;">52</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">92</span><span style="color: #000000; font-weight: bold;">%</span>         -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>
TT            <span style="color: #000000;">2500</span><span style="color: #000000; font-weight: bold;">/</span>s        <span style="color: #000000;">108</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>        -<span style="color: #000000;">82</span><span style="color: #000000; font-weight: bold;">%</span>         -<span style="color: #000000;">87</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">88</span><span style="color: #000000; font-weight: bold;">%</span>
MT           <span style="color: #000000;">14286</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1090</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">471</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>         -<span style="color: #000000;">29</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">33</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Compiled <span style="color: #000000;">20000</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1567</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">700</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">40</span><span style="color: #000000; font-weight: bold;">%</span>           <span style="color: #660033;">--</span>        -<span style="color: #000000;">7</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro      <span style="color: #000000;">21429</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1686</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">757</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">50</span><span style="color: #000000; font-weight: bold;">%</span>           <span style="color: #000000;">7</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #660033;">--</span></pre></div></div>

<p><strong>キャッシュありかつHTMLエスケープあり</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Benchmark: timing <span style="color: #000000;">3000</span> iterations of HT, HT::Compiled, HT::Pro, MT, TT...
        HT:  <span style="color: #000000;">3</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">2.69</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">2.69</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1115.24</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
HT::Compiled:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.28</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">0.28</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">10714.29</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
   HT::Pro:  <span style="color: #000000;">0</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.13</span> usr +  <span style="color: #000000;">0.02</span> sys =  <span style="color: #000000;">0.15</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">20000.00</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        MT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">0.22</span> usr +  <span style="color: #000000;">0.01</span> sys =  <span style="color: #000000;">0.23</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">13043.48</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>warning: too few iterations <span style="color: #000000; font-weight: bold;">for</span> a reliable count<span style="color: #7a0874; font-weight: bold;">&#41;</span>
        TT:  <span style="color: #000000;">1</span> wallclock secs <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000;">1.84</span> usr +  <span style="color: #000000;">0.00</span> sys =  <span style="color: #000000;">1.84</span> CPU<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000;">1630.43</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">n</span>=<span style="color: #000000;">3000</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                Rate          HT          TT HT::Compiled          MT    HT::Pro
HT            <span style="color: #000000;">1115</span><span style="color: #000000; font-weight: bold;">/</span>s          <span style="color: #660033;">--</span>        -<span style="color: #000000;">32</span><span style="color: #000000; font-weight: bold;">%</span>         -<span style="color: #000000;">90</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">91</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">94</span><span style="color: #000000; font-weight: bold;">%</span>
TT            <span style="color: #000000;">1630</span><span style="color: #000000; font-weight: bold;">/</span>s         <span style="color: #000000;">46</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>         -<span style="color: #000000;">85</span><span style="color: #000000; font-weight: bold;">%</span>        -<span style="color: #000000;">87</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">92</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Compiled <span style="color: #000000;">10714</span><span style="color: #000000; font-weight: bold;">/</span>s        <span style="color: #000000;">861</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">557</span><span style="color: #000000; font-weight: bold;">%</span>           <span style="color: #660033;">--</span>        -<span style="color: #000000;">18</span><span style="color: #000000; font-weight: bold;">%</span>       -<span style="color: #000000;">46</span><span style="color: #000000; font-weight: bold;">%</span>
MT           <span style="color: #000000;">13043</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1070</span><span style="color: #000000; font-weight: bold;">%</span>        <span style="color: #000000;">700</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #000000;">22</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #660033;">--</span>       -<span style="color: #000000;">35</span><span style="color: #000000; font-weight: bold;">%</span>
HT::Pro      <span style="color: #000000;">20000</span><span style="color: #000000; font-weight: bold;">/</span>s       <span style="color: #000000;">1693</span><span style="color: #000000; font-weight: bold;">%</span>       <span style="color: #000000;">1127</span><span style="color: #000000; font-weight: bold;">%</span>          <span style="color: #000000;">87</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #000000;">53</span><span style="color: #000000; font-weight: bold;">%</span>         <span style="color: #660033;">--</span></pre></div></div>

<p><div id="book11995194" class="rakuten_details">Now Loading...</div></p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/342/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/342" />
	</item>
		<item>
		<title>Perlで楽天ブックスジャンル検索APIを使う</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/339?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl%25e3%2581%25a7%25e6%25a5%25bd%25e5%25a4%25a9%25e3%2583%2596%25e3%2583%2583%25e3%2582%25af%25e3%2582%25b9%25e3%2582%25b8%25e3%2583%25a3%25e3%2583%25b3%25e3%2583%25ab%25e6%25a4%259c%25e7%25b4%25a2api%25e3%2582%2592%25e4%25bd%25bf%25e3%2581%2586</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/339#comments</comments>
		<pubDate>Wed, 23 Sep 2009 14:27:14 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=339</guid>
		<description><![CDATA[PerlでAmazon Product Advertising APIのBrowseNodeLookupを行なうの楽天版です。Amazon Product Advertising APIはRESTしかないのですが、楽天ウ [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://tech.lampetty.net/tech/index.php/archives/338">PerlでAmazon Product Advertising APIのBrowseNodeLookupを行なう</a>の楽天版です。Amazon Product Advertising APIはRESTしかないのですが、楽天ウェブサービスはRESTとJSONに対応しているので、今回はJSONを使ってみます。</p>
<p>(ファイル名はsample_books_genre_search.pl)</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">Dumper</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>Dumper<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> JSON<span style="color: #339933;">::</span><span style="color: #006600;">Syck</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>Load<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> URI<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uri</span> <span style="color: #339933;">=</span> URI<span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'http://api.rakuten.co.jp/rws/2.0/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$uri</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">query_form</span><span style="color: #009900;">&#40;</span>
    developerId <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Your Developer ID'</span><span style="color: #339933;">,</span>
    operation <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'BooksGenreSearch'</span><span style="color: #339933;">,</span>
    version <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'2009-03-26'</span><span style="color: #339933;">,</span>
    booksGenreId <span style="color: #339933;">=&gt;</span> <span style="color: #000066;">shift</span> <span style="color: #339933;">||</span> <span style="color: #ff0000;">'000'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ua</span> <span style="color: #339933;">=</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$Data</span><span style="color: #339933;">::</span><span style="color: #006600;">Dumper</span><span style="color: #339933;">::</span><span style="color: #006600;">Indent</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">is_success</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> Dumper<span style="color: #009900;">&#40;</span>Load<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Body<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">status_line</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">as_string</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> sample_books_genre_search.pl 000</pre></div></div>

<p>と実行すると、以下のような結果が得られます。引数の&#8221;000&#8243;はルートのジャンルIDです。これを&#8221;001&#8243;のようにすると、&#8221;本&#8221;のジャンルの子ジャンルが取れます。</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #0000ff;">$VAR1</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #ff0000;">'BooksGenreSearch'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #ff0000;">'parent'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #ff0000;">'current'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #ff0000;">'child'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'genreName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'本'</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'genreId'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'001'</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'genreLevel'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'genreName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'CD'</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'genreId'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'002'</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'genreLevel'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #ff0000;">'genreName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'DVD'</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'genreId'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'003'</span><span style="color: #339933;">,</span>
        <span style="color: #ff0000;">'genreLevel'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#40;</span>以下省略<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Amazonと楽天の両方のAPIを見ていて思ったのですが、Amazonのカテゴリを表すBrowseNodeは、例えば&#8221;本&#8221;とかそういうカテゴリだけじゃなくて、&#8221;岩波書店&#8221;みたいな出版社にも適用されています。例えば、&#8221;岩波書店&#8221;にはBrowseNodeIdとして&#8221;516508&#8243;が割り振られていて、子ノードには&#8221;岩波文庫&#8221;や&#8221;岩波現代文庫&#8221;があり、「出版社別」みたいな切り口でも商品を分類することが可能です。</p>
<p>一方で楽天の場合は、商品一つに対してジャンルという純粋な一つのカテゴリしか設定されないようなデータ構造になっています。サイトを作る上では、様々な切り口で商品を分類できた方が集客がしやすいので、個人的には楽天ウェブサービスにもBrowseNodeみたいな概念を取り入れてもらいたいなぁと思いました。(リクエストはしました)</p>
<p>というわけであまりまとまってないですが、こうやってECサイトのAPIが利用できると個人でもECサイトが持てるので夢が広がっていいなぁと感じています。といってもアフィリエイトだったら利益はたかが知れているので、よっぽど頑張らないといけないと思いますが&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/339/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/339" />
	</item>
		<item>
		<title>PerlでAmazon Product Advertising APIのBrowseNodeLookupを行なう</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/338?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl%25e3%2581%25a7amazon-product-advertising-api%25e3%2581%25aebrowsenodelookup%25e3%2582%2592%25e8%25a1%258c%25e3%2581%25aa%25e3%2581%2586</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/338#comments</comments>
		<pubDate>Tue, 22 Sep 2009 18:24:10 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Amazon]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=338</guid>
		<description><![CDATA[AmazonのProduct Advertising APIで、あるカテゴリの下にあるカテゴリ(BrowseNode)を取得するPerlスクリプトを書いてみました。最初はNet::Amazonを使っていたのですが、オブジ [...]]]></description>
			<content:encoded><![CDATA[<p>AmazonのProduct Advertising APIで、あるカテゴリの下にあるカテゴリ(BrowseNode)を取得するPerlスクリプトを書いてみました。最初は<a href="http://search.cpan.org/dist/Net-Amazon/lib/Net/Amazon.pm">Net::Amazon</a>を使っていたのですが、オブジェクトを大量に生成するためかとてつもなく重いのと、返ってくるオブジェクトの構成がよくわからなかったので、自前で処理してみるとさくっとできました。</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/env perl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Encode <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>encode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> URI<span style="color: #339933;">::</span><span style="color: #006600;">Amazon</span><span style="color: #339933;">::</span><span style="color: #006600;">APA</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> XML<span style="color: #339933;">::</span><span style="color: #006600;">Simple</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> YAML <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>Dump<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> <span style="color: #000000; font-weight: bold;">STDERR</span> <span style="color: #ff0000;">&quot;usage: $0 browse_node_id<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">255</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$uri</span> <span style="color: #339933;">=</span> URI<span style="color: #339933;">::</span><span style="color: #006600;">Amazon</span><span style="color: #339933;">::</span><span style="color: #006600;">APA</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'http://ecs.amazonaws.jp/onca/xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$browse_node_id</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$uri</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">query_form</span><span style="color: #009900;">&#40;</span>
    Service     <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'AWSECommerceService'</span><span style="color: #339933;">,</span>
    Operation   <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'BrowseNodeLookup'</span><span style="color: #339933;">,</span>
    SearchIndex <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Books'</span><span style="color: #339933;">,</span>
    BrowseNodeId <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$browse_node_id</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$uri</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">sign</span><span style="color: #009900;">&#40;</span>
    key    <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'key'</span><span style="color: #339933;">,</span>
    secret <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'secret_key'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ua</span> <span style="color: #339933;">=</span> LWP<span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$res</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ua</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$content</span> <span style="color: #339933;">=</span> XMLin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$children</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$content</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>BrowseNodes<span style="color: #009900;">&#125;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>BrowseNode<span style="color: #009900;">&#125;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>Children<span style="color: #009900;">&#125;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>BrowseNode<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">is_success</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> encode<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'utf-8'</span><span style="color: #339933;">,</span> Dump<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$children</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">print</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">status_line</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">as_string</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">perl</span> sample_browse_node.pl <span style="color: #000000;">465610</span></pre></div></div>

<p>とすると下みたいな感じで下の階層のカテゴリを表示します。ちなみに465610は本のBrowseNodeIdです。Net::Amazon&#8211;、URI::Amazon::APA++ということで。</p>
<pre>
---
- BrowseNodeId: 466284
  Name: 文学・評論
- BrowseNodeId: 466288
  Name: 思想・社会・ノンフィクション
- BrowseNodeId: 571582
  Name: 人文・思想
- BrowseNodeId: 571584
  Name: 社会・政治
- BrowseNodeId: 492152
  Name: ノンフィクション
- BrowseNodeId: 466286
  Name: 歴史・地理
- BrowseNodeId: 466282
</pre>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/338/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/338" />
	</item>
		<item>
		<title>YAPC::Asia 2009に行ってきました(9.10)</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/335?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yapcasia-2009%25e3%2581%25ab%25e8%25a1%258c%25e3%2581%25a3%25e3%2581%25a6%25e3%2581%258d%25e3%2581%25be%25e3%2581%2597%25e3%2581%259f9-10</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/335#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:58:41 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[YAPC::Asia]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=335</guid>
		<description><![CDATA[2009/9/10,11に開催されたYAPC::Asia 2009に参加しました。今年はフレームワークやORM系のセッションがとても豪華で全部聞きたいなと思っていたので、迷うことなく有給を取って参加。というわけで殴り書き [...]]]></description>
			<content:encoded><![CDATA[<p>2009/9/10,11に開催されたYAPC::Asia 2009に参加しました。今年はフレームワークやORM系のセッションがとても豪華で全部聞きたいなと思っていたので、迷うことなく有給を取って参加。というわけで殴り書きですがメモと感想を徒然に書いてみます。</p>
<h3>PSGI &#8211; Perl Server Gateway Interface</h3>
<p><a href="http://tokuhirom.github.com/talks/20090910-yapcasia2009-psgi-plack/">スライド</a></p>
<p>tokuhiromさんとmiyagawaさんのコラボレーション。PSGIは仕様でPlackは実装。HTTP::Engineには仕様とAPIと実装が全て入っていたのでこれを分離しましたよ、というお話。YAPCの1週間前ぐらいから開発し始めていてすごい勢いで実装されているらしいです。PlackはいわゆるReference Implementationですが、かなり速いという話でした。Reference Implementationという用語を聞いて、JavaのServlet APIをなんとなく思い出しました。</p>
<h3>シックスアパートフレームワーク</h3>
<p>shigetaさん<br />
<a href="http://www.slideshare.net/comewalk/ss-1994397">スライド</a></p>
<p>Six ApartではMTフレームワーク, ArcheTypeの2種類のフレームワークがある。<br />
MTではMTオブジェクトがData::ObjectDriverを継承している。Data::ObjectDriverはSix ApartのCTOの人が作っているORマッパ。memcachedによるwrite-throughキャッシュが実装されてたり、shardingもサポートされている。たしか2年前ぐらいのYAPCでセッションやってたと思います。</p>
<p>驚きだったのは、TypepadではHTML::Templateを使っているということ。普通はTTとか最近だったらText::MicroTemplateのような高機能なものを使うと思うのですが、ななぜあえてHTML::Templateなんでしょうか。っていうのを質問すれば良かったと今になって後悔。</p>
<h3>API Design</h3>
<p>sartak &#8211; Shawn M Mooreさん。Mooseのコミッター。<br />
<a href="http://sartak.org/talks/yapc-asia-2009/api-design/api-design.pdf">スライド</a></p>
<ul>
<li>Moose</li>
<li>Path::Dispatcher</li>
<li>HTTP::Engine</li>
<li>Dist::Zilla</li>
<li>IM::Engine</li>
</ul>
<p>今日はこれらのプロジェクトが持つクールなAPIの話をします。大事なのはテストをたくさん書くこと！なぜならテストを書けば使いやすいAPIかどうかわかる！あとはMooseなどの話。HTTP::Engineは新しいサーバのInterfaceが出てきても一行書き換えれば対応可能なところが素晴らしい、と言っていました。<br />
英語だったので話の内容の半分も理解できなかったのが無念&#8230;</p>
<h3>Modern Catalyst</h3>
<p>hidekさん。最近DeNAに転職した? らしい。<a href="http://www.slideshare.net/hidek/modern-catalyst">スライド</a></p>
<ul>
<li>Catalyst 5.8について</li>
<li>Mooseベースになった</li>
<li>M,V,Cの名前空間がdepreatedになった</li>
<li>NEXTが&#8230;</li>
<li>Catalyst::PluginなnamespaceのプラグインをCPANにあげるとmstにおこられるw</li>
</ul>
<p>他のセッションでも「Catalyst 5.8使ってます」という話をちらほら聞いたのですが、Mooseベースになったら速度が遅くなったりしてないのかな、というのが気になっています。</p>
<h3>ark &#8211; framework inspired by Catalyst</h3>
<p>KAYACのtypesterさん。<a href="http://typester.stfuawsc.com/slides/yapsasia2009-ark/">スライド</a></p>
<ul>
<li>KAYACでは大量に自社サービスを作っている。</li>
<li>Catalystでもいいんだけど、もうちょっとライトなWAFがよかったのでHTTP::Engineをベースに作った。</li>
<li>CGIモードでも割とサクサク動くようになってる。</li>
<li>CGIの場合はクラスはlazy loadingするように工夫</li>
<li>あとdispatch tableのキャッシュもしている</li>
<li>テンプレートはText::MicroTemplate::Extendedというものを使うのが推奨されている。もともとはMojo::Templateをokuさんが切り出したもので、それを拡張している。</li>
<li>Ark::Models &#8211; CLIスクリプトからでもModelを使える仕組み。あとlazy loadingをやったり。</li>
<li>Ark::Form &#8211; HTML::Shakanベース</li>
<li>KAYACのArkで作られたサービスの一部はなんとソースコードが公開されている！</li>
<li><a href="http://github.com/kayac/inamena.kayac.com">いなめなヶ崎</a></li>
<li><a href="http://github.com/kayac/im.kayac.com">im.kayac.com</a></li>
</ul>
<p>Ark、Catalystとほぼ互換っていうのがいいなぁというのと、日本のモバイル対応版が早くリリースされないかな、って思いました。モバイル対応はもう必須になりつつあるような世の中なので、そのあたりのベストプラクティスを積み上げていってほしいなぁと。</p>
<h3>優しいモダンなWAFの作り方</h3>
<p>dannさん。<a href="http://www.slideshare.net/techmemo/waf">スライド</a></p>
<ul>
<li>モダンなWAFとは？
<ul>
<li>プラガブル &#8211; Plagger</li>
<li>サーバ抽象化 &#8211; PythonのWSGI</li>
<li>フルスタック &#8211; Rails</li>
</ul>
</li>
<li>Angelos::PSGI::Engine</li>
<li>Plack::Requestを使ってる</li>
<li>HTTP::Routerを使ってる</li>
<li>シンプルなWAFなら2,3時間あれば作れちゃう！</li>
<li>次、WAFの拡張部分の作り方(プラグイン)</li>
<li>コアは小さく</li>
<li>拡張可能な箇所を限定(Controller, View, Middleware, Request, Response)</li>
<li>Hookポイントを明確にする</li>
<li>プラグインの種類は大きく分けると2種類</li>
<li>RequestやResponseのライフサイクルへのHook系</li>
<li>メソッドはやす系</li>
<li>適切なデフォルトセットの提供することが重要</li>
<li>そうしないとUnicode化するためのTIPSが乱立したり、Inflate/Deflateするための(ry</li>
<li>HTTP::EngineやPlack、HTTP::RouterなどのモジュールのおかげでWAF作りがかつてないほど簡単になっている！</li>
</ul>
<p>Arkの話も聞いてて思ったのですが、Sinatraみたいなレベルであれば本当に2,3時間で作れそうな気がしました。これからフレームワーク戦国時代の幕開けですね。そして懇親会でも直接dannさんに言ったのですが、Angelosを早くリリースしてほしいです&#8230; (切実)</p>
<h3>Booking.com &#038; Perl</h3>
<p>Cristina Nunesさん(from リスボン,ポルトガル)。<a href="http://www.slideshare.net/cmnunes/bookingcom-perl">スライド</a></p>
<ul>
 Class::DBI(frozen version)<br />
 HTML::Mason<br />
 HTML::Template (forked version)</p>
<li>Perl 5.8.8</li>
<li>Class::DBI</li>
<li>HTML::Mason</li>
<li>HTML::Template(forked version)</li>
<li>なぜPerl? &#8211;> 簡単で速い</li>
<li>なぜHTML::Template? &#8211;> 簡単</li>
<li>なぜClass::DBI? &#8211;> 聞き取れず(一番聞きたかったのに)</li>
<li>Apache + mod_perl</li>
<li>MySQL</li>
<li>memcached</li>
<li>git</li>
<li>Jabber</li>
<li>
将来のゴール</p>
<ul>
<li>Perl upgrade</li>
<li>Modules upgrade</li>
<li>Apache + mod_perl upgrade (to 2.x)</li>
<li>MySQL upgrade (to 5.1 or 5.4)</li>
</ul>
<li>フロントエンドのWeb Server: 100台以上</li>
<li>MySQLサーバ: 100台以上</li>
<li>データセンターにあるサーバは700台以上</li>
<li>
IT at Booking.com</p>
<ul>
<li>Perl developers</li>
<li>Java developers</li>
<li>Network engineers</li>
<li>Sysadmins (社内SEみたいなものかな)</li>
<li>Web designers</li>
</ul>
</li>
<li>世界に25オフィスある</li>
<li>1900人の従業員</li>
<li>71000L hosts over 70 countries</li>
<li>4500+ distribution partners</li>
<li>24 languages available on website</li>
</ul>
<p>Booking.comって初めて知ったのですが、ヨーロッパのホテルはかなり網羅されているという情報がIRCで流れてました。なんでいまだにClass::DBI使ってるんだろう、と思いましたが、mod_perlのバージョンも1.x系なので、やっぱりサーバの台数が多いとアップグレードも大変なのでしょうね。「Booking.comという会社は著名なPerl hackerを何人も抱えている会社なのです」という牧さんの補足がありました。</p>
<h3>Key Value Store with O/R Mapper</h3>
<p>yappoさん。<a href="http://yappo.ficia.com/#album/1E8DF4EE-9DB6-11DE-B1EE-7BD1A805B909">スライド</a></p>
<ul>
<li>Key Value StoreとO/R Mapperの合わせ技 &#8211;> Data::Model</li>
<li>Data::ModelはいわゆるO/R Mapper
<ul>
<li>DBI, memcached protocol, Perl hash, Perl code, Gearman</li>
<li>Q4Mにネイティブ対応</li>
<li>透過キャッシュ</li>
<li>MixinによるSchemaクラスの拡張</li>
<li>ちょっとなういイテレータ</li>
</ul>
</li>
<li>なんで既製品では駄目だったのか？
<ul>
<li>Class::DBI &#8211; DBICに移行する方向になってる。偉大なる先輩に感謝</li>
<li>DBIx::Class &#8211; 過去に使っていたがあまりいい思い出がなかった</li>
<li>Rose::DB, Jifty::DBI &#8211; 見る時間があまり取れず&#8230;</li>
<li>DBIx::Moco &#8211; ドキュメント少ない。実際にサービスで投入されているものと公開されているモジュールの差異が激しい</li>
<li>Data::ObjectDriver &#8211; Six Apart謹製。意外とテスト少ない＆document少ない。ただしコードは大分参考にさせてもらった</li>
<li>DBIx::Skinny &#8211; 日本のPerl ORM専門家のもの。シンプルなスキーマに対して「生SQL書けます」はあまり魅力的じゃなかった。</li>
</ul>
</li>
<li>結局調べているうちにORM作成のポイントがわかってきたので自分で作ることにした</li>
<li>ORMはSchema, Iterator, Row, SQL, DBの5つのものがあればよい</li>
<li>Data::Modelのデモ(詳しくはスライドを参照)</li>
<li>TokyoCabinetをストレージとして使う場合、データを小さくした方がキャッシュにのったり色々嬉しいので、データは圧縮して小さくするべし。</li>
<li>Perl標準のStorableはデータ効率が悪いことで有名 &#8211;> 代わりにMessagePackを使用(これがすごいらしい)</li>
<li>KVSがストレージならALTER TABLEが必要なくなる。FriendFeedの例のようにすれば、スキーマなくても大丈夫</li>
<li>MessagePackがとにかくすごいらしい</li>
</ul>
<p>まずスライドがFiciaだったのが驚きでした。なるほど、そういう使い方できるんですねー。途中のData::Modelのチュートリアルでは、その高機能さ(透過キャッシュやMasterSlave、column_sugar)に驚きました。自分でもオレオレORM作ってるのですが、相当レベル違うなと感激しましたです。その他、MessagePackがすごい使えそう、というのがよくわかりました。圧縮のアルゴリズムがカリカリ過ぎです。</p>
<h3>simple or mapper DBIx::Skinny</h3>
<p>nekokakさん。<a href="http://nekokak.org/presen/yapcasia2009-dbix-skinny/">スライド</a></p>
<ul>
<li>なぜ自分でORMapperを作ったか？</li>
<li>DBIx::Classが微妙&#8230;
<ul>
<li>便利だけど発行されるSQLが微妙</li>
<li>パフォーマンスが出なかったりする</li>
<li>生成されるオブジェクトがでかすぎるため重い</li>
<li>カスタマイズしようにもソースがでかすぎる</li>
<li>DBICで速度でない場合は生SQL書くことがあるが、Inflateとかしてくれない</li>
</ul>
</li>
<li>生SQL書いてinflate/deflateしてくれるライトなものでいいんじゃないか、というところにたどりついた</li>
<li>DBIx::Skinnyのデモ</li>
<li>relationshipは特に何もしない</li>
<li>コアは小さく、がモットーなので、拡張したい人はラッパーを書けばいい</li>
<li>何人かの人に興味持ってるみたいなので、Skinnyみたいな方向性はありなのだなと思った。</li>
</ul>
<p>ちょいと前にSkinnyのバグを見つけたのでメールでパッチ送ったりしていて、ちょうどYAPC当日にnekokakさんと直接お話させてもらう機会があったのですが、いやー非常に嬉しかったです。懇親会でもお話しさせてもらったのですが、やっぱり生SQL全然ありだよね、と思いました。もともと自分がSkinnyに興味を持ったのは、自分が3年前ぐらいに会社で作ったO/R Mapperと非常に設計思想が似ていて、「へー」と思ったのがきっかけです。(もちろんSkinnyの方がAPI的にも非常に洗練されているのですが&#8230;)</p>
<p>というわけで、自分も<a href="http://github.com/oinume/p5-dbix-thin/tree/master">DBIx::Thin</a>というのを作っているのですが、早く安定板をリリースしないと。とにかくエンジニアとしてはとても刺激になったセッションでした。</p>
<h3>『Ficia』インフラとPerlにまつわるエトセトラ</h3>
<p>hirose31さん＠えとらぼ。<a href="http://www.slideshare.net/hirose31/ficiaperl-1981415">スライド</a></p>
<ul>
<li>Linux 2.6, Apache2, mod_perl 2.0</li>
<li>mod_perl 2のメモリ関連のチューニング</li>
<li>推測するな計測するべし(ps, top)</li>
<li>fork(2) &#8211; Copy On Write</li>
</li>
<li>/proc/PID/smaps (kernel >= 2.6.14) &#8211; 共有領域: Shared_Clean + Shared_Dirty</li>
<li>いわゆる親プロセスと子プロセスの共有メモリ領域の話</li>
<li><a href="http://d.hatena.ne.jp/naoya/20080212/1202830671">id:naoyaさんの日記</a>が詳しい</li>
<li>使うモジュールは親プロセスで起動時に(startup.plなどで)ロードする &#8211;> 子プロセス独自のメモリ領域にロードしないので節約になる</li>
<li>Apache起動直後は共有率99%。時間が経つと共有率が40%ぐらいになり、プロセスのメモリ消費量が多くなる</li>
<li>使用しているモジュールの一覧をどうやって取得するか？</li>
<li>Apache2::Status &#8211;> Loaded Modules で一覧を確認</li>
<li>あと親プロセスでモジュールをロードしておくと、コンパイル済みで即戦力になるのでオーバヘッドが少ないらしい</li>
<li>サイズがもりもり太っているモジュールを探す方法 &#8211;  Apache2::Status + B::TerseSize<br />
。Memory Usageで一覧を確認</li>
<li>nagiosで少しでもswapしたらメールを飛ばすようにしている</li>
<li>以降、httpd.confの話</li>
<li>StartServers != {Min,Max}SpareServers</li>
<li>忙しい時はMaxに。ヒマになったらStartServersの数に戻る &#8211;> forkするのがむだ？だったら最初から全プロセス起動しておいた方がいい</li>
<li>/etc/init.d/httpd stop</li>
<li> stopしたのに新規リクエストが来ちゃう(ロードバランサーのヘルスチェック後とか)</li>
<li>start &#8211; 起動時からフルでプロセスを起動するので、start直後にリクエストが来ると負荷が高くなってしまう。一定時間おいてからロードバランサーに入れる必要がある。</li>
<li>ロードバランサーのヘルスチェックのURLを動的なものにしておき、フラグファイルがあったら200を返すようにするとか。</li>
<li>あとはmatrixの話</li>
</ul>
<p>mod_perlのチューニングのような話は、最近はみんなFastCGIに移行しているのかあまり聞かないので、非常にためになりました。あと、StartServersで最初から一気にプロセス立ち上げておく技は昔考えたことあったのですが、LBのヘルスチェックとか面倒だなぁと思って挫折した経験があります。なるほど、ヘルスチェックのURLを動的にしてフラグファイルで管理っていうのは全然ありだなと思いました。というかRubyのpassengerとかはそんな感じの実装になってますね。</p>
<h3>一日を通しての感想</h3>
<p>おそらく初めて丸一日ぶっ通しでセッションを聴いたのですが、最後の方はやはり集中力がもたなくていいセッションでもあまりちゃんと聴けなかったのが残念です。ただ、エンジニアとしてのモチベーションは上がりまくりで、懇親会中も「早く家帰ってコーディングしたい」と実は思ってました。あと酔った勢いでDBIx::Skinnyのポスグレ対応とかしたのですが、バグとかテストを <a href="http://d.hatena.ne.jp/sfujiwara/">sfujiwara さん</a>に面倒見てもらったようで、非常に恐縮でした&#8230;</p>
<p>個人的に非常に参考になったのは、Ark、Data::Model、DBIx::Skinny、Ficiaのインフラでした。WAF, ORMapper, インフラの話が聴けて大満足です。</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/335/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/335" />
	</item>
		<item>
		<title>YAPC::Asia 2009に行ってきました(9.11)</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/337?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yapcasia-2009%25e3%2581%25ab%25e8%25a1%258c%25e3%2581%25a3%25e3%2581%25a6%25e3%2581%258d%25e3%2581%25be%25e3%2581%2597%25e3%2581%259f9-11</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/337#comments</comments>
		<pubDate>Fri, 11 Sep 2009 13:21:17 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[YAPC::Asia]]></category>
		<category><![CDATA[Perl]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=337</guid>
		<description><![CDATA[すごい今更ですが、記憶が残っているうちに9.11のYAPC::Asiaのレポートをば。2日目も色々セッションに参加したのですが、特に記憶に残っている最初の2つだけ。 DeNA loves Perl notoさん。スライド [...]]]></description>
			<content:encoded><![CDATA[<p>すごい今更ですが、記憶が残っているうちに9.11のYAPC::Asiaのレポートをば。2日目も色々セッションに参加したのですが、特に記憶に残っている最初の2つだけ。</p>
<h3>DeNA loves Perl</h3>
<p>notoさん。<a href="http://www.slideshare.net/notolab/dena-loves-perl">スライド</a></p>
<ul>
<li>DeNAはエンジニアは100人ぐらいで半分ぐらいはモバゲー</li>
<li>今回のYAPCでも3人がセッションで発表している。(hidek, zigorou, matsuda)</li>
<li>最初のbiddersはJava+Oracleだった</li>
<li>モバオクを川崎さんが作る時に初めてPerlをつかった</li>
<li>Perlは読みやすい、実行速度も速い</li>
<li>最近はmyDNSのDNS RRの代わりにLVSを使うようになった</li>
<li>memcachedも使っている</li>
<li>MySQLのshardingもしている。モバゲーでは90個ぐらいに分割されている</li>
<li>mobamail</li>
<li>モバイルのメール配信 &#8211; キャリアの都合で接続がdenyされることがある。大量に送り続けるとdenyされる</li>
<li>キャリアのMXレコードは異様に少ない
<ul>
<li>docomo.ne.jp &#8211; 4IP</li>
<li>ezweb.ne.jp &#8211; 1IP</li>
<li>softbank.ne.jp &#8211; 1IP</li>
<li></li>
</ul>
</li>
<li>キャリアのメールサーバが少ないから、送信側で送信先を分散できない</li>
<li>mobamail &#8211; Perlで実装したモバイルメール向けの配信サーバ。配信するだけなのでMTAじゃない(SMTP受ける方は実装してない)</li>
<li>コネクションを貼るところでIO::Selectを使っている</li>
<li>キャリアの制限の範囲内でなんとか最速で配信できるようにする。キャリアごとに細かい設定ができるように。</li>
<li>SIELLA Engine &#8211; より速い。さらに自分たちで設定しなくても楽。最近はこっちを使っている(ズコー)</li>
<li>(話は変わって)モバゲーのオープン化について</li>
<li>OpenSocial + Game API  法人に解放</li>
<li>10/5 にフォーラム。来年1月以降に一般公開</li>
<li>DeNAのエンジニアのポリシー &#8211; ビジネスの成功にコミットすること</li>
<li>お金で解決しない。技術で「安く、早く」</li>
<li> ビジネスの成功を重視しているので、きれいな技術が二の次になっているのは否めない</li>
<li>100人近くPerlエンジニアがいるのにCPANコミットがない
<ul>
<li>他社の特許侵害のリスク(一部上場しているので目をつけられてしまう)</li>
<li>やりたい人がいたら推奨しようとしているが&#8230;</li>
</ul>
</li>
<li>DeNAのPerlでの開発方針
<ul>
<li>「一筆書き文化」：再利用せいや汎用性の設計に時間をかけるくらいならそういうことは考えずに書く方が速い</li>
<li>サービス単位の独自性・機動性重視＝モジュールの共有なし</li>
<li>割り切りの思想、逆転の発想、これでサービスを速くリリースしてきた面も</li>
</ul>
</li>
<li>今後の展望
<ul>
<li>大きな開発チームでのポリシー</li>
<li>more test code</li>
<li>MobaSiFの次のバージョン</li>
</ul>
</li>
<li>DeNAで一緒にコードを書きましょう!
<ul>
<li>as DeNA software engineer / architect</li>
<li>as developer of moba-ga town</li>
<li>as CPAN author
</li>
</ul>
</li>
</ul>
<p>携帯向けのメール配信でSIELLA Engineを使っているのが驚きでした。ああいうモバイル向けのパッケージ製品って他にも<a href="http://www.klab.jp/am/">Klabのアクセルメール</a>とか良さそうなのいっぱいあるし、やっぱり餅は餅屋にって感じなのでしょうか。</p>
<p>モバゲーのAPIがオープン化されるということは、PC向けにFlashでゲームも作れるのかな？というのが気になりました。現状モバゲーのPCサイトってモバイル版のはりぼてみたいなものなので、これを機にPCサイトも盛り上がるといいのかなぁと。</p>
<h3>endeworksでのWeb Appの作り方</h3>
<p> 33rpmさん(船木太郎さん)。<a href="http://www.slideshare.net/33rpm/endeworkswebapp">スライド</a></p>
<ul>
<li>牧さんが代表の会社です。</li>
<li>特別なことはやっていない</li>
<li>開発サーバは用意していない(個人のMac上で開発)</li>
<li>Test::FITesque が最近の流行り（読み方よくわからない）</li>
<li>ディプロイ &#8211;  git pullするだけ。ディプロイツールといったものは使用していない</li>
<li>Apache+FCGI &#8211; daemontoolsでFCGIのプロセスを管理</li>
<li>Catalystを使用</li>
<li>Mooseを使用 &#8211; Catalyst 5.8をプロダクション環境で使用</li>
<li>APIモジュール</li>
<li>CatalystのModelを使用しない</li>
<li>CatalystのModelは当たり前だけどCatalyst依存</li>
<li>ModelはWebという枠組みの外、たとえばCLIやWorkerからも呼び出したいことが多い</li>
<li>MyApp::API::* という名前空間
<ul>
<li>中身はORM+アプリケーションロジック</li>
<li>キャッシングのロジックもここ</li>
</ul>
</li>
<li>DIコンテナは？ &#8211; アプリケーション全体で共有されるインスタンスはRegistryというモジュールに格納</li>
<li>Registry &#8211; メモリ上のKVSに保存するだけのsingleton object</li>
<li>Formの作成はHTML::FormFuを使用
<ul>
<li> 生成されるHTMLに不満</li>
<li> HTML::FormFuのValidatorはとてもよくできているので、レンダリングはさせないでValidatorだけ使うというのが最近の流行</li>
</ul>
</li>
<li>pixis(フレームワーク)の説明(githubで公開されている)
<ul>
<li>WebサービスやSNSでありがちなメッセージ管理などのソーシャル機能をまとめたフレームワーク</li>
<li>Catalyst 5.8依存</li>
<li>依存モジュールが多い</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/337/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/337" />
	</item>
	</channel>
</rss>

