<?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>clicktx::Tech::Memo &#187; perl</title>
	<atom:link href="http://perl.no-tubo.net/category/web%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%9f%e3%83%b3%e3%82%b0/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://perl.no-tubo.net</link>
	<description>perl、 MySQL、オープンソース系、ウェブ系ネタ。なぜか鯖管メモがほとんどを占めている...</description>
	<lastBuildDate>Mon, 23 Jan 2012 10:51:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/category/web%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%9f%e3%83%b3%e3%82%b0/perl/feed/" />
		<item>
		<title>Business::PayPal::API::ExpressCheckoutで住所とか氏名とかの日本語が正しく送れない理由</title>
		<link>http://perl.no-tubo.net/2011/12/12/businesspaypalapiexpresscheckout%e3%81%a7%e4%bd%8f%e6%89%80%e3%81%a8%e3%81%8b%e6%b0%8f%e5%90%8d%e3%81%a8%e3%81%8b%e3%81%ae%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%81%8c%e6%ad%a3%e3%81%97%e3%81%8f%e9%80%81/</link>
		<comments>http://perl.no-tubo.net/2011/12/12/businesspaypalapiexpresscheckout%e3%81%a7%e4%bd%8f%e6%89%80%e3%81%a8%e3%81%8b%e6%b0%8f%e5%90%8d%e3%81%a8%e3%81%8b%e3%81%ae%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%81%8c%e6%ad%a3%e3%81%97%e3%81%8f%e9%80%81/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 12:24:36 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[PayPal]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[Business::PayPal::API]]></category>
		<category><![CDATA[SOAP::Lite]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1851</guid>
		<description><![CDATA[PerlでPayPalのExpressCheckoutを実装するのにBusiness::PayPal::APIを使っているのだけれど、日本語（2byte文字）がうまく送信できない。
UTF-8で送ろうが、フラグ付きにしよ [...]]]></description>
			<content:encoded><![CDATA[<p>PerlでPayPalのExpressCheckoutを実装するのにBusiness::PayPal::APIを使っているのだけれど、日本語（2byte文字）がうまく送信できない。</p>
<p>UTF-8で送ろうが、フラグ付きにしようが、だ。</p>
<p>散々悩んだ挙句、原因はSOAP::Liteが勝手にbase64エンコードするからだと判明。</p>
<p>Business::PayPal::APIは内部でSOAP::Liteを使用していて、Asciiだとそのままなのだけれど、それ以外はbase64にエンコードする仕様の様子。</p>
<p>前々から、例えば、OrderDescription に日本語を使うと上手くいかないと思っていたのだけれど、、、。<br />
仕方がないから Business::PayPal::API::ExpressCheckoutをカスタマイズする。</p>
<p>/usr/lib/perl5/site_perl/5.8.8/Business/PayPal/API/ExpressCheckout.pm</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">## add all the other fields</span>
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$field</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%types</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">next</span> <span style="color: #b1b100;">unless</span> <span style="color: #000066;">defined</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$field</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'MaxAmount'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">push</span> <span style="color: #0000ff;">@secrd</span><span style="color: #339933;">,</span> SOAP<span style="color: #339933;">::</span><span style="color: #006600;">Data</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">type</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$types</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>
              <span style="color: #339933;">-&gt;</span><span style="color: #006600;">attr</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#123;</span>currencyID <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$currencyID</span><span style="color: #339933;">,</span> xmlns <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">C_xmlns_ebay</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">## 追記ココから</span>
        <span style="color: #b1b100;">elsif</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$field</span> <span style="color: #b1b100;">eq</span> <span style="color: #ff0000;">'Address'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@addr</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$f</span> <span style="color: #009900;">&#40;</span> <span style="color: #000066;">keys</span> <span style="color: #0000ff;">%$hash</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000066;">push</span> <span style="color: #0000ff;">@addr</span><span style="color: #339933;">,</span>  SOAP<span style="color: #339933;">::</span><span style="color: #006600;">Data</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$f</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">type</span><span style="color: #009900;">&#40;</span>string <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$f</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$obj</span> <span style="color: #339933;">=</span> SOAP<span style="color: #339933;">::</span><span style="color: #006600;">Data</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\SOAP</span><span style="color: #339933;">::</span><span style="color: #006600;">Data</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">value</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">@addr</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066;">push</span> <span style="color: #0000ff;">@secrd</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$obj</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">## 追記ココまで</span>
        <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">push</span> <span style="color: #0000ff;">@secrd</span><span style="color: #339933;">,</span> SOAP<span style="color: #339933;">::</span><span style="color: #006600;">Data</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">name</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$field</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$args</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">type</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">$types</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$field</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>SOAPなんて使ってたらダメだね&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/12/12/businesspaypalapiexpresscheckout%e3%81%a7%e4%bd%8f%e6%89%80%e3%81%a8%e3%81%8b%e6%b0%8f%e5%90%8d%e3%81%a8%e3%81%8b%e3%81%ae%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%81%8c%e6%ad%a3%e3%81%97%e3%81%8f%e9%80%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/12/12/businesspaypalapiexpresscheckout%e3%81%a7%e4%bd%8f%e6%89%80%e3%81%a8%e3%81%8b%e6%b0%8f%e5%90%8d%e3%81%a8%e3%81%8b%e3%81%ae%e6%97%a5%e6%9c%ac%e8%aa%9e%e3%81%8c%e6%ad%a3%e3%81%97%e3%81%8f%e9%80%81/" />
	</item>
		<item>
		<title>MojoliciousアプリをCGIとして動作させる時は、静的ファイルの扱いに注意が必要</title>
		<link>http://perl.no-tubo.net/2011/11/28/mojolicious%e3%82%a2%e3%82%a6%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92cgi%e3%81%a8%e3%81%97%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%82%8b%e6%99%82%e3%81%af%e3%80%81/</link>
		<comments>http://perl.no-tubo.net/2011/11/28/mojolicious%e3%82%a2%e3%82%a6%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92cgi%e3%81%a8%e3%81%97%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%82%8b%e6%99%82%e3%81%af%e3%80%81/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 01:30:11 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[Mojolicious]]></category>
		<category><![CDATA[CGI]]></category>
		<category><![CDATA[speedyCGI]]></category>
		<category><![CDATA[webアプリケーション]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=2106</guid>
		<description><![CDATA[MojoliciousアプリケーションをCGIで動かす時に、スタイルシートや外部JavaScriptファイル、画像など静的ファイルの扱いには注意が必要になる。
これはテンプレートでタグヘルパーを使った時、例えば

Sel [...]]]></description>
			<content:encoded><![CDATA[<p>MojoliciousアプリケーションをCGIで動かす時に、スタイルシートや外部JavaScriptファイル、画像など静的ファイルの扱いには注意が必要になる。</p>
<p>これはテンプレートでタグヘルパーを使った時、例えば</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #339933;">&lt;%=</span> stylesheet <span style="color: #ff0000;">'/css/style.css'</span> <span style="color: #339933;">%&gt;</span></pre></div></div></div>

<p>とスタイルシートを読み込んだつもりだけど、これはスタイルシートを読み込むCGIを実行するのと同じ（スタイルシートのURLにリダイレクト処理するCGIとして実行される）<br />
静的ファイルを呼び出す回数分のCGIが実行されることになるので、パフォーマンスも悪くなることは当然ながら、データベースの接続処理をstartup内でしていたりすると実行回数分の接続を行う事となる。</p>
<p>以前speedyCGIとしてMojoliciousアプリケーションを動作させていた時に、MySQLの最大同時接続数が異常になってMySQLサーバがダウンした事もある。</p>
<p><a href="http://d.hatena.ne.jp/perlcodesample/20101224/1300598136">MojoliciousでCSSなどの静的ファイルを利用する &#8211; サンプルコードによるPerl入門</a></p>
<blockquote><p>　ローカル環境で試験をするときは、組み込みのWebサーバーがCSSなどの静的ファイルをディスパッチしてくれるので、パフォーマンスの問題は起こりません。一方さくらのレンタルサーバーではCGIというプロトコルを利用してWebアプリケーションを起動しています。CGIというプロトコルはひとつのアクセスに対してひとつのプロセスを立ち上げます。プロセスの起動という処理はとても時間がかかるので、静的ファイルをCGIでディスパッチするとパフォーマンスの劣化が顕著に見られることになります。</p></blockquote>
<p>リンク先にあるように、組み込みのwebサーバやその他のプリフォークサーバ（starman等）ならば問題ないのかもしれない。まぁ、それ自体がwebサーバとして動作するものなので当然といえば当然か。</p>
<p>CGIとして動作させる場合の解決策としてmod_rewriteの利用が提唱されている。.httaccess(またはhttpd.conf)でApacheが静的ファイルは静的ファイルとして処理するようにmod_rewriteルールを設定する。</p>
<p>もう一つ解決する方法としては、タグヘルパーを使わずに</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #339933;">&lt;</span><span style="color: #990000;">link</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/css/style.css&quot;</span> media<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;screen&quot;</span> rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> <span style="color: #339933;">/&gt;</span></pre></div></div></div>

<p>などと直接タグを書いてしまう方法。これはデプロイするときにディレクトリ構成が変わったりすると後々面倒（テンプレートをすべて修正する必要があるとか）なので、推奨される方法ではないのかもしれないけれど。</p>
<p>MojoliciousをCGI動作させていてパフォーマンスがイマイチだな・・・と思っているなら調べてみるといいかもしれない。</p>
<p>App.pmとかのstartup()内に</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> startup <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">warn</span> <span style="color: #ff0000;">'warn CGI run.'</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">....</span>
<span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>等としてアプルケーションを実行するとapacheのエラーログに記録されるので1度の実行で複数行のログが残っていたらその行数分アプリケーションが実行されていることになる。</p>
<h3>Apacheでデプロイする様々な方法</h3>
<p><a href="https://github.com/kraih/mojo/wiki/Apache-deployment">Apache deployment &#8211; GitHub</a><br />
Apache/CGI にmod_rewriteの設定が記述されている。その中の Pretty &#8220;Web 2.0&#8243; URLs という項目は参考になる。</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/11/28/mojolicious%e3%82%a2%e3%82%a6%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92cgi%e3%81%a8%e3%81%97%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%82%8b%e6%99%82%e3%81%af%e3%80%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/11/28/mojolicious%e3%82%a2%e3%82%a6%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92cgi%e3%81%a8%e3%81%97%e3%81%a6%e5%8b%95%e4%bd%9c%e3%81%95%e3%81%9b%e3%82%8b%e6%99%82%e3%81%af%e3%80%81/" />
	</item>
		<item>
		<title>Mojoliciousで設定ファイルを読み込んで値を使う時のTips</title>
		<link>http://perl.no-tubo.net/2011/10/15/mojolicious%e3%81%a7%e8%a8%ad%e5%ae%9a%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%aa%ad%e3%81%bf%e8%be%bc%e3%82%93%e3%81%a7%e5%80%a4%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82%e3%81%aetips/</link>
		<comments>http://perl.no-tubo.net/2011/10/15/mojolicious%e3%81%a7%e8%a8%ad%e5%ae%9a%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%aa%ad%e3%81%bf%e8%be%bc%e3%82%93%e3%81%a7%e5%80%a4%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82%e3%81%aetips/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 04:48:32 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[Mojolicious]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=2088</guid>
		<description><![CDATA[configファイルの読み込み
設定ファイルを読み込む / Mojoliciousリファレンス &#8211; サンプルコードによるPerl入門 
　設定(コンフィグ)ファイルを読み込むにはMojolicious::Pl [...]]]></description>
			<content:encoded><![CDATA[<h3>configファイルの読み込み</h3>
<p><a href="http://d.hatena.ne.jp/perlcodesample/20110718/1315269985">設定ファイルを読み込む / Mojoliciousリファレンス &#8211; サンプルコードによるPerl入門</a> </p>
<blockquote><p>　設定(コンフィグ)ファイルを読み込むにはMojolicious::Plugin::Configを利用します。</p></blockquote>
<p>というように、Mojoliciousアプリで設定ファイルを使う事ができる。例えば etc/MyApp.conf を読み込むには以下のような感じ。 stash_keyはオプションで設定できる(デフォルトはconfig?)</p>
<p>etc/MyApp.conf</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># MyApp config</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># サービス名</span>
    SARVICE_NAME <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'hogehoge'</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># 配列のリファレンス等</span>
    CATEGORY <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>MyApp.pm</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> MyApp<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> startup <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># コンフィグファイル読み込み</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">plugin</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'config'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> file <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'etc/MyApp.conf'</span><span style="color: #339933;">,</span> stash_key <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'conf'</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">....</span>
<span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>読み込みはリンク先に詳しく書かれている。</p>
<h3>configで設定された値を使う</h3>
<p>コントローラーとかで使う</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #000066;">index</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">#my $config = $self-&gt;stash('conf');</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$config</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">config</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$service_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$config</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>SARVICE_NAME<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># or </span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$service_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$self</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">config</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'SARVICE_NAME'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">....</span>
<span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>テンプレートで使う</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #339933;">&lt;%=</span> stash<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'conf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>SARVICE_NAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">%&gt;</span>
    <span style="color: #339933;">&lt;%=</span> config<span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#123;</span>SARVICE_NAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">%&gt;</span>
    <span style="color: #339933;">&lt;%=</span> config<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'SARVICE_NAME'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%&gt;</span></pre></div></div></div>

<p>オブジェクト指向的には config()で取得したほうがいいのかな？</p>
<p>テンプレートで使うには</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #339933;">&lt;%=</span> config <span style="color: #ff0000;">'SARVICE_NAME'</span> <span style="color: #339933;">%&gt;</span></pre></div></div></div>

<p>がタイプが一番少ないようだ。</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/10/15/mojolicious%e3%81%a7%e8%a8%ad%e5%ae%9a%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%aa%ad%e3%81%bf%e8%be%bc%e3%82%93%e3%81%a7%e5%80%a4%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82%e3%81%aetips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/10/15/mojolicious%e3%81%a7%e8%a8%ad%e5%ae%9a%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e8%aa%ad%e3%81%bf%e8%be%bc%e3%82%93%e3%81%a7%e5%80%a4%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82%e3%81%aetips/" />
	</item>
		<item>
		<title>MojoliciousでDBIx::CustomやDBIx::Connectorを使う時</title>
		<link>http://perl.no-tubo.net/2011/10/14/mojolicious%e3%81%a7dbixcustom%e3%82%84dbixconnector%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82/</link>
		<comments>http://perl.no-tubo.net/2011/10/14/mojolicious%e3%81%a7dbixcustom%e3%82%84dbixconnector%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 08:42:33 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[Mojolicious]]></category>
		<category><![CDATA[perlモジュール]]></category>
		<category><![CDATA[DBIx::Custom]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=2085</guid>
		<description><![CDATA[すぐ忘れるし、どこにあったか分からなくなるのでメモ。
Mojolicious &#8211; GitHubより引用

Selec All Code:    package MyApp;
&#160;
    use Mo [...]]]></description>
			<content:encoded><![CDATA[<p>すぐ忘れるし、どこにあったか分からなくなるのでメモ。</p>
<p><a href="https://github.com/yuki-kimoto/DBIx-Custom/wiki/Mojolicious">Mojolicious &#8211; GitHub</a>より引用</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #000066;">package</span> MyApp<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">use</span> Mojo<span style="color: #339933;">::</span><span style="color: #006600;">Base</span> <span style="color: #ff0000;">'Mojolicious'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">use</span> DBIx<span style="color: #339933;">::</span><span style="color: #006600;">Custom</span><span style="color: #339933;">;</span>
&nbsp;
    has dbi <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbi</span> <span style="color: #339933;">=</span> DBIx<span style="color: #339933;">::</span><span style="color: #006600;">Custom</span><span style="color: #339933;">-&gt;</span><span style="color: #000066;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">return</span> <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">sub</span> startup <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> <span style="color: #339933;">=</span> <span style="color: #000066;">shift</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">### YOU MUST NOT CALL dbi() method in starup().</span>
    <span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>リンク先にはBad Exampleもあるので参考に。</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/10/14/mojolicious%e3%81%a7dbixcustom%e3%82%84dbixconnector%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/10/14/mojolicious%e3%81%a7dbixcustom%e3%82%84dbixconnector%e3%82%92%e4%bd%bf%e3%81%86%e6%99%82/" />
	</item>
		<item>
		<title>DBIx::Customで where を設定する時の注意点</title>
		<link>http://perl.no-tubo.net/2011/07/30/dbixcustom%e3%81%a7-where-%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b%e6%99%82%e3%81%ae%e6%b3%a8%e6%84%8f%e7%82%b9/</link>
		<comments>http://perl.no-tubo.net/2011/07/30/dbixcustom%e3%81%a7-where-%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b%e6%99%82%e3%81%ae%e6%b3%a8%e6%84%8f%e7%82%b9/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 06:30:26 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[perlモジュール]]></category>
		<category><![CDATA[DBIx::Custom]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1906</guid>
		<description><![CDATA[DBIx::Custom &#8211; search.cpan.org
DBIx-Custom-0.1705
DBIx::Custom select メソッドの whereの項目を見ると様々な指定方法が出来ることが分か [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://search.cpan.org/dist/DBIx-Custom/lib/DBIx/Custom.pm">DBIx::Custom &#8211; search.cpan.org</a><br />
DBIx-Custom-0.1705</p>
<p><a href="http://search.cpan.org/dist/DBIx-Custom/lib/DBIx/Custom.pm#select">DBIx::Custom select メソッドの whereの項目</a>を見ると様々な指定方法が出来ることが分かる。</p>
<p>注意する点はStringを渡す時。</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">where <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">&quot;url = 'http://hoge.com/huga.cgi?key=value' &quot;</span><span style="color: #339933;">,</span></pre></div></div></div>

<p>上のように条件に ? が含まれる場合、SQLの作成に失敗する。（DBIx::Custom::QueryBuilder::build_queryでかな？）</p>
<p>? がメタ文字として扱われるため？（\?とエスケープしてもダメ）</p>
<p>Hashリファレンスで渡すと大丈夫です。</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">where <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>url <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'http://hoge.com/huga.cgi?key=value'</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div></div>

<p>これを踏まえてコードにすると、安全なのはこんな感じのコードになるのかな。</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$url</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'http://hoge.com/huga.cgi?key=value'</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #000066;">select</span><span style="color: #009900;">&#40;</span>
        table   <span style="color: #339933;">=&gt;</span>  <span style="color: #ff0000;">'table_name'</span><span style="color: #339933;">,</span>
        column  <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #000066;">qw</span><span style="color: #339933;">/</span>title content url<span style="color: #339933;">/</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        where <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">&quot;and&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;{= id}&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;url = :url&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;update_time &gt; SUBDATE( NOW(), interval 7 day )&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                    <span style="color: #009900;">&#123;</span> id <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$id</span><span style="color: #339933;">,</span> url <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$url</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div></div>

<p>※ {= id} は &#8220;id = :id&#8221; とも書ける（url = :urlも同様）。あえて両方含めてみた。</p>
<p>where にはDBIx::Custom::Whereオブジェクトも指定できますが、DBIx::Custom::Whereオブジェクトを生成する時にも同様で、? を含めた条件には注意が必要。</p>
<p>追記：タグ機能は廃止予定だそうです。<br />
<a href="http://twitter.com/#!/perlcodesample/status/97162783644319744">http://twitter.com/#!/perlcodesample/status/97162783644319744</a></p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/07/30/dbixcustom%e3%81%a7-where-%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b%e6%99%82%e3%81%ae%e6%b3%a8%e6%84%8f%e7%82%b9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/07/30/dbixcustom%e3%81%a7-where-%e3%82%92%e8%a8%ad%e5%ae%9a%e3%81%99%e3%82%8b%e6%99%82%e3%81%ae%e6%b3%a8%e6%84%8f%e7%82%b9/" />
	</item>
		<item>
		<title>perlモジュールのバージョンを調べる便利なコマンド pmvers</title>
		<link>http://perl.no-tubo.net/2011/07/29/perl%e3%83%a2%e3%82%b8%e3%83%a5%e3%83%bc%e3%83%ab%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e8%aa%bf%e3%81%b9%e3%82%8b-pmvers/</link>
		<comments>http://perl.no-tubo.net/2011/07/29/perl%e3%83%a2%e3%82%b8%e3%83%a5%e3%83%bc%e3%83%ab%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e8%aa%bf%e3%81%b9%e3%82%8b-pmvers/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 11:08:28 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[perlモジュール]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1899</guid>
		<description><![CDATA[perlモジュールのバージョンを一発で調べられるコマンド
$ pmvers DBIx::Cutom
0.168
cpanmがインストールしてあれば
$ cpanm pmvers
でインストールできます。
もうこんな事しな [...]]]></description>
			<content:encoded><![CDATA[<p>perlモジュールのバージョンを一発で調べられるコマンド</p>
<pre>$ pmvers DBIx::Cutom
0.168</pre>
<p>cpanmがインストールしてあれば</p>
<pre>$ cpanm pmvers</pre>
<p>でインストールできます。</p>
<p>もうこんな事しなくていいんです。</p>
<pre>perl -MDBI -e 'print $DBIx::Custom::VERSION'</pre>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/07/29/perl%e3%83%a2%e3%82%b8%e3%83%a5%e3%83%bc%e3%83%ab%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e8%aa%bf%e3%81%b9%e3%82%8b-pmvers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/07/29/perl%e3%83%a2%e3%82%b8%e3%83%a5%e3%83%bc%e3%83%ab%e3%81%ae%e3%83%90%e3%83%bc%e3%82%b8%e3%83%a7%e3%83%b3%e3%82%92%e8%aa%bf%e3%81%b9%e3%82%8b-pmvers/" />
	</item>
		<item>
		<title>DBIx::Customでデータベースの関数（MySQLのADDDATE()とかSUBDATE()とか）を使う</title>
		<link>http://perl.no-tubo.net/2011/07/29/dbixcustom%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e9%96%a2%e6%95%b0%ef%bc%88mysql%e3%81%aeadddate%e3%81%a8%e3%81%8bsubdate%e3%81%a8%e3%81%8b%ef%bc%89%e3%82%92/</link>
		<comments>http://perl.no-tubo.net/2011/07/29/dbixcustom%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e9%96%a2%e6%95%b0%ef%bc%88mysql%e3%81%aeadddate%e3%81%a8%e3%81%8bsubdate%e3%81%a8%e3%81%8b%ef%bc%89%e3%82%92/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 10:00:38 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[perlモジュール]]></category>
		<category><![CDATA[DBIx::Custom]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1869</guid>
		<description><![CDATA[DBIx::Customが生SQL感覚でも使えてお気に入りなのだけれど、ちょっこしハマったのでメモ。
DBIx::Custom &#8211; search.cpan.org 
MySQL（に限らないと思う）でwhere [...]]]></description>
			<content:encoded><![CDATA[<p>DBIx::Customが生SQL感覚でも使えてお気に入りなのだけれど、ちょっこしハマったのでメモ。</p>
<p><a href="http://search.cpan.org/~kimoto/DBIx-Custom/lib/DBIx/Custom.pm">DBIx::Custom &#8211; search.cpan.org</a> </p>
<p>MySQL（に限らないと思う）でwhereに関数を利用したい時は多々あると思います。<br />
例えば、更新時刻を見て7日以上古ければ選択しないという条件をwhereに指定したい時、</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span>  <span style="color: #ff0000;">`table`</span> <span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>
    <span style="color: #ff0000;">`update_time`</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`table`</span> <span style="color: #993333; font-weight: bold;">WHERE</span> update_time <span style="color: #66cc66;">&gt;</span> SUBDATE<span style="color: #66cc66;">&#40;</span>NOW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">INTERVAL</span> <span style="color: #cc66cc;">7</span> <span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div></div>

<p>このようなSQLだとします。</p>
<p><a href="http://d.hatena.ne.jp/perlcodesample/20110210/1300165343">行の選択 select / DBIx::Custom リファレンス &#8211; サンプルコードによるPerl入門</a>によれば、where句の設定は、</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">where <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Perl'</span><span style="color: #339933;">,</span> author <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Ken'</span><span style="color: #009900;">&#125;</span></pre></div></div></div>

<p>と、ハッシュリファレンスで渡すことになっています。</p>
<p>=  以外での比較は DBIx::Custom::Whereオブジェクト を作成して渡すことになっているのですが、ハッシュリファレンスで SUBDATE(NOW(),  interval 7 day) を渡すとうまくいかないので悩んでいました。</p>
<p>append =&gt; &#8221; で文字列を追加できるので、無理やり実装してみたのですが作者にツイートしてみたところ、</p>
<p><a href="http://perl.no-tubo.net/wp-content/uploads/2011/07/2bfc96a45921677282a36721a07709ca1.png"><img src="http://perl.no-tubo.net/wp-content/uploads/2011/07/2bfc96a45921677282a36721a07709ca1-500x307.png" alt="twitterのキャプチャ" title="twitterのキャプチャ" width="500" height="307" class="alignnone size-medium wp-image-1881" /></a></p>
<p>がーん、そうなのか。恥ずかしい。。さらに、</p>
<p><a href="http://perl.no-tubo.net/wp-content/uploads/2011/07/d8eddf5329882d76f0b5323c6fd8e79a.png"><img src="http://perl.no-tubo.net/wp-content/uploads/2011/07/d8eddf5329882d76f0b5323c6fd8e79a-500x319.png" alt="twitterのキャプチャ2" title="twitterのキャプチャ2" width="500" height="319" class="alignnone size-medium wp-image-1883" /></a></p>
<p>がーん、DBIx::Custom::Where でも文字列が指定できたのか。。。（恥恥</p>
<p>という事で、</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> DBIx<span style="color: #339933;">::</span><span style="color: #006600;">Custom</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$dbi</span> <span style="color: #339933;">=</span> DBIx<span style="color: #339933;">::</span><span style="color: #006600;">Custom</span><span style="color: #339933;">-&gt;</span><span style="color: #000066;">connect</span><span style="color: #009900;">&#40;</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;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #000066;">select</span><span style="color: #009900;">&#40;</span>
    table <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'table'</span><span style="color: #339933;">,</span>
    where <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'create_time &gt; SUBDATE(NOW(),  interval 7 day)'</span><span style="color: #339933;">,</span> 
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div></div>

<p>あるいは、</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> DBIx<span style="color: #339933;">::</span><span style="color: #006600;">Custom</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$where</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">where</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">$where</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">clause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>
    <span style="color: #ff0000;">'and'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'create_time &gt; SUBDATE(NOW(),  interval 7 day)'</span>
<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #000066;">select</span><span style="color: #009900;">&#40;</span>
    table <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'table'</span><span style="color: #339933;">,</span>
    where <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$where</span><span style="color: #339933;">,</span> 
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div></div>

<p>で、出来るって事なんですね。（ソース嫁！）</p>
<p>id:perlcodesampleさんありがとうございました！<br />
DBIx::Customいいよー。</p>
<h3>ところで </h3>
<p>SYNOPSYSにあるinsertとかupdateとかが失敗するのはボクの日頃の行いが悪いせいでしょうか？</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;"># Insert </span>
    <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Perl'</span><span style="color: #339933;">,</span> author <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Ken'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> table  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Update </span>
    <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Perl'</span><span style="color: #339933;">,</span> author <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Ken'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> table  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'book'</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;">5</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div></div>


<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;"># Insert </span>
    <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">insert</span><span style="color: #009900;">&#40;</span> param <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Perl'</span><span style="color: #339933;">,</span> author <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Ken'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> table  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'book'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Update </span>
    <span style="color: #0000ff;">$dbi</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">update</span><span style="color: #009900;">&#40;</span> param <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>title <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Perl'</span><span style="color: #339933;">,</span> author <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'Ken'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> table  <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'book'</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;">5</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div></div>

<p>だと性交いや、成功するのですが。<br />
param が必要？？（だからソース嫁！）</p>
<p>追記：DBIx::Customのバージョンが 0.168 だったからのようです&#8230;APIが追加された模様。</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/07/29/dbixcustom%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e9%96%a2%e6%95%b0%ef%bc%88mysql%e3%81%aeadddate%e3%81%a8%e3%81%8bsubdate%e3%81%a8%e3%81%8b%ef%bc%89%e3%82%92/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/07/29/dbixcustom%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e9%96%a2%e6%95%b0%ef%bc%88mysql%e3%81%aeadddate%e3%81%a8%e3%81%8bsubdate%e3%81%a8%e3%81%8b%ef%bc%89%e3%82%92/" />
	</item>
		<item>
		<title>Mojolicious::LiteなアプリケーションをSpeedyCGIで動かす</title>
		<link>http://perl.no-tubo.net/2011/04/13/mojoliciouslite%e3%81%aa%e3%82%a2%e3%83%97%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92speedycgi%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%99/</link>
		<comments>http://perl.no-tubo.net/2011/04/13/mojoliciouslite%e3%81%aa%e3%82%a2%e3%83%97%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92speedycgi%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%99/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 10:34:34 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[Mojolicious]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perlモジュール]]></category>
		<category><![CDATA[Mojolicious::Lite]]></category>
		<category><![CDATA[speedyCGI]]></category>
		<category><![CDATA[WAF]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1647</guid>
		<description><![CDATA[やっと出来たのでメモ。
起動スクリプト mojo.cgiを
#!/usr/bin/env perl

use strict;
use warnings;
use lib "../lib";
use App;

app-&#038; [...]]]></description>
			<content:encoded><![CDATA[<p>やっと出来たのでメモ。<br />
起動スクリプト mojo.cgiを</p>
<pre>#!/usr/bin/env perl

use strict;
use warnings;
use lib "../lib";
use App;

app-&gt;start('cgi');
exit;</pre>
<p>こんな感じで呼び出すと通常のCGIとしてならなんの問題もなく実行出来るのだけれど、SpeedyCGIで実行しようとすると初回実行時に不具合が出る（テンプレートが読み込まれない？）上記実行方法でも２回目以降（キャッシュ後）は動作している感じ。</p>
<p>FastCGIやmod_perlでのエントリーは見かけるんだけど、いかんせんマイナーなSpeedyCGI。結構便利なのにあまり使われていないのかなぁ。</p>
<p>で、とりあえず解決方法が見つかったのでメモ。結論から言えば起動スクリプトで Mojo::Server::CGI を使ってアプリを実行すればいいみたい。</p>
<h3>ApacheでCGI（SpeedyCGI）としてデプロイする</h3>
<p>起動用スクリプトを mojoアプリケーション本体と同じディレクトリに置く。これは必須ではないので公開するときには配置を変えたり出来る。<br />
（例えば App/public/mojo.cgi として  App/public を公開ディレクトリにするなど）</p>
<p>__DATA__セクションにテンプレートを書けるけど、ここではテストのため外部ファイル化している。</p>
<h4>最小構成</h4>
<pre>App +
    - App.pm
    - mojo.cgi
    - templates +
                - index.html.ep</pre>
<h4>スクリプト</h4>
<p>起動用スクリプト mojo.cgi</p>
<pre>#!/usr/bin/env speedy

use strict;
use warnings;

use Mojo::Server::CGI;
$ENV{MOJO_APP} = 'App';
<span style="color: #ff9900;">#$ENV{SCRIPT_NAME} = '';</span>
Mojo::Server::CGI-&gt;new-&gt;run;
exit;</pre>
<p>App.pm と 起動スクリプトが同じディレクトリの場合は use lib; しなくても大丈夫のようだ。別のディレクトリに起動スクリプトを置く場合、例えばApp/public/mojo.cgi とするなら use &#8220;../&#8221;; が必要になる。</p>
<p>mojoアプリケーション本体 App.pm</p>
<pre>package App;

use strict;    <span style="color: #ff9900;"># use Mojolicious::Lite;で自動でされるが明示的に</span>
use warnings;  <span style="color: #ff9900;"># しておく。</span>
use utf8;
use Mojolicious::Lite;

<span style="color: #ff9900;"># テンプレートへ渡すハッシュリファレンス</span>
my $params = {};

<span style="color: #ff9900;"># インデックスページ</span>
get '/' =&gt; sub {
    my $self = shift;

    $params-&gt;{title}="Hello World!";
    $params-&gt;{messe} = "ようこそ 世界へ！";
    $self-&gt;stash(params =&gt; $params);
} =&gt; 'index';

<span style="color: #ff9900;"># mojo.cgi/任意の文字に対応</span>
get '/:foo' =&gt; sub {
    my $self = shift;

    $params-&gt;{title}= $self-&gt;param('foo');
    $params-&gt;{messe} = "このページは" . $self-&gt;param('foo') . "です";
    $self-&gt;stash(params =&gt; $params);
} =&gt; 'index';</pre>
<p>テンプレートファイル templates/index.html.ep</p>
<pre>&lt;!doctype html&gt;
&lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;&lt;%= $params-&gt;{title} %&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h1&gt;&lt;%= $params-&gt;{messe} %&gt;&lt;/h1&gt;
    &lt;/body&gt;
&lt;/html&gt;</pre>
<p>上記は最低限のファイル構成だけれど、テンプレートでレイアウト使っても動作していたのでSpeedyCGI化もこの方法なら大丈夫そう。</p>
<p>参考</p>
<li><a href="http://yanor.net/wiki/?Perl-Mojolicious%2FApacheにCGIでデプロイ">Perl-Mojolicious/ApacheにCGIでデプロイ &#8211; yanor.net/wiki</a></li>
<li><a href="http://d.hatena.ne.jp/rightgo09/20100101/p2">Mojoことはじめ &#8211; Perl日記</a></li>
<li><a href="http://d.hatena.ne.jp/charsbar/20081105/1225820041">Mojoを使ってみた &#8211; Charsbar::Note<br />
</a></li>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/04/13/mojoliciouslite%e3%81%aa%e3%82%a2%e3%83%97%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92speedycgi%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%99/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/04/13/mojoliciouslite%e3%81%aa%e3%82%a2%e3%83%97%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%92speedycgi%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%99/" />
	</item>
		<item>
		<title>perl メール送信にテンプレートを使う Email::MIME::MobileJP::Template</title>
		<link>http://perl.no-tubo.net/2011/04/05/perl-%e3%83%a1%e3%83%bc%e3%83%ab%e9%80%81%e4%bf%a1%e3%81%ab%e3%83%86%e3%83%b3%e3%83%97%e3%83%ac%e3%83%bc%e3%83%88%e3%82%92%e4%bd%bf%e3%81%86-emailmimemobilejptemplate/</link>
		<comments>http://perl.no-tubo.net/2011/04/05/perl-%e3%83%a1%e3%83%bc%e3%83%ab%e9%80%81%e4%bf%a1%e3%81%ab%e3%83%86%e3%83%b3%e3%83%97%e3%83%ac%e3%83%bc%e3%83%88%e3%82%92%e4%bd%bf%e3%81%86-emailmimemobilejptemplate/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 04:58:46 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[perlモジュール]]></category>
		<category><![CDATA[メール]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1567</guid>
		<description><![CDATA[id:tokuhirom氏作の Email::MIME::MobileJP::Template を発見したので使ってみる。
PODにあるコードは動作しないので注意。renderメソッドのAPIが変わったのかな？ソースを見 [...]]]></description>
			<content:encoded><![CDATA[<p>id:tokuhirom氏作の <a href="http://search.cpan.org/~tokuhirom/Email-MIME-MobileJP-0.05/lib/Email/MIME/MobileJP/Template.pm">Email::MIME::MobileJP::Template</a> を発見したので使ってみる。</p>
<p>PODにあるコードは動作しないので注意。renderメソッドのAPIが変わったのかな？ソースを見ると引数の最初に送り先のメールアドレスを指定しないとダメみたい。</p>
<p>それと、<a href="http://search.cpan.org/~tokuhirom/Email-MIME-MobileJP-0.05/lib/Email/MIME/MobileJP.pm#Email::MIME::MobileJP::Template_(12398)(12420)(12388)(12434)(12388)(12363)(12358)(12497)(12479)(12540)(12531)">Email::MIME::MobileJP</a> にあるソースも間違えているので注意。syntax=&gt;&#8217;TTerse&#8217; は newする時に引数で指定する事。</p>
<pre>use Email::MIME::MobileJP::Template;
use Email::Sender::Simple;

    my $token='123';
    my $to_email = '&lt;foo@hoge.jp&gt;'; <span style="color: #ff0000;"># &lt;&gt;で囲わないとdocomoで受信したときToがおかしなことに。</span>

    my $mail_maker = Email::MIME::MobileJP::Template-&gt;new(
                    'Text::Xslate' =&gt; {
                            syntax =&gt; 'TTerse',
                            path =&gt; ['email_tmpl/']     <span style="color: #ff0000;"># テンプレートの保存場所</span>
                    }
    );

    my $mail = $mail_maker-&gt;render(
                    <span style="color: #ff9900;"><strong>$to_email,</strong></span>                  <span style="color: #ff0000;"># 送信先</span>
                    'signup.eml',               <span style="color: #ff0000;"># テンプレートファイル</span>
                    {token =&gt; $token}    <span style="color: #ff0000;"># テンプレートで利用する変数</span>
    );
    <span style="color: #ff0000;"># メールを送信</span>
    Email::Sender::Simple-&gt;send($mail);</pre>
<h3>テンプレートファイル</h3>
<p>path で指定したディレクトリにテンプレートファイルを作成する。MIMEヘッダはテンプレートでしか指定できない。Fromは記述しないと致命的なエラーとなるので注意。</p>
<p>./email_tmpl/signup.eml</p>
<pre>Subject: [Example] サインアップ!
<span style="color: #ff9900;">From: &lt;name@hoge.com&gt;
</span>
    以下をクリックせよ
    http://example.com/signup/[% token %]</pre>
<h3>携帯電話宛のメールが正しく送れない？</h3>
<p><span id="more-1567"></span><br />
メールテンプレートで、Fromに指定するメールアドレスは&lt;&gt;で囲わないとdocomoへ送ったときにUnknown user となってしまった。from=&lt;&gt; となっている。これは自分の環境の問題？</p>
<p>エラーになったテンプレートファイル例</p>
<pre>Subject: [Example] サインアップ!
<span style="color: #ff9900;">From: name@hoge.com
</span>
    以下をクリックせよ
    http://example.com/signup/[% token %]</pre>
<p>Postfixのエラーログ</p>
<pre>Apr  5 17:44:56 vm postfix/smtp[24073]: 7D4ED32358D: to=&lt;xxxxx.@docomo.ne.jp&gt;, relay=mfsmax.docomo.ne.jp[203.138.180.112]:25, delay=0.17, delays=0.09/0.01/0.03/0.04, dsn=5.0.0, status=bounced (host mfsmax.docomo.ne.jp[203.138.180.112] said: 550 Unknown user xxxxx.@docomo.ne.jp (in reply to end of DATA command))
Apr  5 17:44:56 vm postfix/cleanup[23741]: A169832358F: message-id=&lt;20110405084456.A169832358F@mail.hoge.jp&gt;
Apr  5 17:44:56 vm postfix/qmgr[1698]: A169832358F: <span style="color: #ff9900;">from=&lt;&gt;</span>, size=2480, nrcpt=1 (queue active)</pre>
<p>あと、送信先のメールアドレスは</p>
<pre>my $to_email = '&lt;foo@hoge.jp&gt;';</pre>
<p>のように指定しないと携帯（docomoムーバ）で受信したときにおかしな挙動となる。メールを開くと返信不可マークのToヘッダが表示されている（メールアドレスは送った携帯のメアド）。返信しようとすると返信先を選択する画面になる。</p>
<p>メール送信の実装はガラケーとカオスな文字コードのおかげでやたらと面倒なことが多い。</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/04/05/perl-%e3%83%a1%e3%83%bc%e3%83%ab%e9%80%81%e4%bf%a1%e3%81%ab%e3%83%86%e3%83%b3%e3%83%97%e3%83%ac%e3%83%bc%e3%83%88%e3%82%92%e4%bd%bf%e3%81%86-emailmimemobilejptemplate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/04/05/perl-%e3%83%a1%e3%83%bc%e3%83%ab%e9%80%81%e4%bf%a1%e3%81%ab%e3%83%86%e3%83%b3%e3%83%97%e3%83%ac%e3%83%bc%e3%83%88%e3%82%92%e4%bd%bf%e3%81%86-emailmimemobilejptemplate/" />
	</item>
		<item>
		<title>perl SpeedyCGIでデータベースの接続を永続化する</title>
		<link>http://perl.no-tubo.net/2011/04/01/perl-speedycgi%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e6%8e%a5%e7%b6%9a%e3%82%92%e6%b0%b8%e7%b6%9a%e5%8c%96%e3%81%99%e3%82%8b/</link>
		<comments>http://perl.no-tubo.net/2011/04/01/perl-speedycgi%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e6%8e%a5%e7%b6%9a%e3%82%92%e6%b0%b8%e7%b6%9a%e5%8c%96%e3%81%99%e3%82%8b/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 07:36:53 +0000</pubDate>
		<dc:creator>clicktx</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[DBI]]></category>
		<category><![CDATA[speedyCGI]]></category>
		<category><![CDATA[データベース]]></category>

		<guid isPermaLink="false">http://perl.no-tubo.net/?p=1555</guid>
		<description><![CDATA[perlスクリプトをSpeedyCGIスクリプトとして動作させる場合、グローバル変数の値は保持され続ける。なので、一度接続したデータベースハンドルを使い回すことでデータベースへの接続コストを削減できる。
テスト用スクリプ [...]]]></description>
			<content:encoded><![CDATA[<p>perlスクリプトをSpeedyCGIスクリプトとして動作させる場合、グローバル変数の値は保持され続ける。なので、一度接続したデータベースハンドルを使い回すことでデータベースへの接続コストを削減できる。</p>
<p>テスト用スクリプト<span id="more-1555"></span></p>
<pre>#!/usr/bin/speedy

use strict;
use warnings;
use DBI;

<span style="color: #ff9900;">#DB用設定</span>
my $db_user = 'db_user_name';
my $db_pass = 'db_password';
my $db_name = 'db_name';
my $db_host = 'localhost';
my $dbi = "DBI:mysql:$db_name:$db_host";

<span style="color: #ff9900;"># テスト用</span>
print "content-type:text/html;\n\n";
our $c;
$c=1 if !$c;
print "($c回目)\n";
$c++;

our $dbh;    <span style="color: #ff9900;"># データベースハンドルは our でグローバル変数を宣言する必要がある。</span>
my  $db_connect_flag;

<span style="color: #ff9900;"># $dbhがある場合データベースハンドルが有効かチェック</span>
eval{
    $db_connect_flag = $dbh-&gt;ping;
    print "DB check.\n";  <span style="color: #ff9900;"># ２回目以降はチェック(テスト用)</span>
} if defined($dbh);

<span style="color: #ff9900;"># 未接続ならDBに接続する</span>
if(!$db_connect_flag){
    eval {
        $dbh = DBI-&gt;connect($dbi, $db_user, $db_pass)|| die $dbh-&gt;errstr;
    };
    <span style="color: #ff9900;"># 接続エラーならエラー処理</span>
    error("DB could not connect.")  if $@;
    print "DB conect OK.\n";   <span style="color: #ff9900;"># 初回は接続(テスト用)</span>
}
exit;</pre>
<p>ポイントとしては our $dbh; としてグローバル変数宣言する事。my でレキシカル変数を宣言してしまうと毎回初期化されてしまう。</p>
<p>defined で $dbh が定義済みか調べて、定義済みなら ping を送って有効か調べる。無効ならデータベースへ接続する。</p>
<p>必要な部分はこんな感じ。</p>
<pre>our $dbh;
my  $db_connect_flag;

<span style="color: #ff9900;"># $dbhがある場合データベースハンドルが有効かチェック</span>
eval{ $db_connect_flag = $dbh-&gt;ping; } if defined($dbh);

<span style="color: #ff9900;"># 未接続ならDBに接続する</span>
if(!$db_connect_flag){
    eval {
        $dbh = DBI-&gt;connect($dbi, $db_user, $db_pass) || die $dbh-&gt;errstr;
    };
    <span style="color: #ff9900;"># 接続エラーならエラー処理</span>
    error("DB could not connect.")  if $@;
}</pre>
<p>あくまでSpeedyCGIでのスクリプトで、FastCGIの場合とかは違うかも。</p>
]]></content:encoded>
			<wfw:commentRss>http://perl.no-tubo.net/2011/04/01/perl-speedycgi%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e6%8e%a5%e7%b6%9a%e3%82%92%e6%b0%b8%e7%b6%9a%e5%8c%96%e3%81%99%e3%82%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://perl.no-tubo.net/2011/04/01/perl-speedycgi%e3%81%a7%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e6%8e%a5%e7%b6%9a%e3%82%92%e6%b0%b8%e7%b6%9a%e5%8c%96%e3%81%99%e3%82%8b/" />
	</item>
	</channel>
</rss>

