<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2japanesefull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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" version="2.0">

<channel>
	<title>おいぬま日報</title>
	
	<link>http://tech.lampetty.net/tech</link>
	<description>Apache, MySQL, Perl, WordPressあたり</description>
	<lastBuildDate>Mon, 23 Aug 2010 10:15:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</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://tech.lampetty.net/tech/index.php/feed" />
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/oinume/diary" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="oinume/diary" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://superfeedr.com/hubbub" /><item>
		<title>Perl開発環境としてのEmacsの設定</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/384</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>最近社内で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>

<p><a href="http://feedads.g.doubleclick.net/~a/awa0RELO0atOV7jYMRM0wDACk9U/0/da"><img src="http://feedads.g.doubleclick.net/~a/awa0RELO0atOV7jYMRM0wDACk9U/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/awa0RELO0atOV7jYMRM0wDACk9U/1/da"><img src="http://feedads.g.doubleclick.net/~a/awa0RELO0atOV7jYMRM0wDACk9U/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/0bggEVw__80" height="1" width="1"/>]]></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>Linodeの最下位プランのメモリが増量されてる</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/383</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/383#comments</comments>
		<pubDate>Sun, 18 Jul 2010 19:00:11 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[VPS]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=383</guid>
		<description>昨日まで知らなかったのですが、
Linode Turns 7, BIG RAM Increaseというアナウンスがあって、Linodeの最下位プランのメモリが360MBから512MBになっていました(値段は$19.95) [...]</description>
			<content:encoded><![CDATA[<p>昨日まで知らなかったのですが、<a href="http://blog.linode.com/2010/06/16/linode-turns-7-big-ram-increase/"><br />
Linode Turns 7, BIG RAM Increase</a>というアナウンスがあって、Linodeの最下位プランのメモリが360MBから512MBになっていました(値段は$19.95)。さらにその一つ上は540MB→768MBとこれまた大幅増量です。その他のプランについては覚えていませんが、最大で42%増量されているっぽいです。そろそろ<a href="http://prgmr.com/xen/">prgmr</a>に乗り換えようかと思っていたのですが、LinodeはCPUコアを最大で4つまで使えるので、当面居座ろうかなと思っています。</p>
<p>というわけで、Linode絶賛おすすめ中です！</p>

<p><a href="http://feedads.g.doubleclick.net/~a/0E7EugiurEBUe1GZPTpN9T0tprE/0/da"><img src="http://feedads.g.doubleclick.net/~a/0E7EugiurEBUe1GZPTpN9T0tprE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/0E7EugiurEBUe1GZPTpN9T0tprE/1/da"><img src="http://feedads.g.doubleclick.net/~a/0E7EugiurEBUe1GZPTpN9T0tprE/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/mBozp4YZMTg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/383/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/383" />
	</item>
		<item>
		<title>Eclipseの覚えておきたいショートカットキーとTips</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/382</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/382#comments</comments>
		<pubDate>Thu, 08 Jul 2010 13:09:14 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=382</guid>
		<description>こんにちは。最近仕事ではめっきりJavaばっかり書いています。そんなわけでそろそろ大嫌いなEclipseを使い始めて3ヶ月以上経ったので、今まで得たノウハウの一部をまとめておきたいと思います。
Eclipseで覚えておき [...]</description>
			<content:encoded><![CDATA[<p>こんにちは。最近仕事ではめっきりJavaばっかり書いています。そんなわけでそろそろ大嫌いなEclipseを使い始めて3ヶ月以上経ったので、今まで得たノウハウの一部をまとめておきたいと思います。</p>
<h3>Eclipseで覚えておきたいショートカットキー</h3>
<p>とりあえず自分が日常的に使っているものを挙げます。</p>
<table>
<tr>
<th>ショートカットキー</th>
<th>動作</th>
</tr>
<tr>
<td>Ctrl + Shift</td>
<td>コード補完</td>
</tr>
<tr>
<td>(クラスや変数にカーソルがある状態で)F3</td>
<td>宣言を開く</td>
</tr>
<tr>
<td>Ctrl + /</td>
<td>リージョンをコメントアウト または コメントアウト解除</td>
</tr>
<tr>
<td>Ctrl + Shift + O</td>
<td>importの整理</td>
</tr>
<tr>
<td>Ctrl + Shift + G</td>
<td>変数やクラスなどのリファレンス検索(workspace内)</td>
</tr>
<tr>
<td>Alt + Shift + R</td>
<td>リファクタ(リネーム)</td>
</tr>
<tr>
<td>Alt + Shift + W</td>
<td> 現在エディタで開いているファイルをパッケージエクスプローラでも表示</td>
</tr>
<tr>
<td>Ctrl + Shift + T</td>
<td>絞り込み可能なクラス一覧を開く</td>
</tr>
<tr>
<td>Ctrl + Shift + R</td>
<td>絞り込み可能なリソース(ファイル)一覧を開く</td>
</tr>
<tr>
<td>Ctrl + F11</td>
<td>プログラムの実行</td>
</tr>
<tr>
<td>F11</td>
<td>デバッグモードで実行</td>
</tr>
<tr>
<td>Ctrl + Alt + U</td>
<td>svn update</td>
</tr>
<tr>
<td>Ctrl + Shift + L</td>
<td>選択しているファイルの差分を表示(Compare With ->Latest from Repository)</td>
</tr>
</table>
<p>きっと便利なものがまだまだあるはずですが、ショートカットキーが多すぎて最近脳が飽和状態です&#8230;</p>
<p>また、上記の他に独自のキーバインドで</p>
<table>
<tr>
<td>Ctrl + Shift + C</td>
<td>プロジェクトのクリーンビルド</td>
</tr>
<tr>
<td>Ctrl + B</td>
<td>プロジェクトをビルド</td>
</tr>
</table>
<p>を定義しています。キーバインドの定義はWindow -> Preferences で Preferences ダイアログを開いて、General -> Keys から設定できます。</p>
<h3>定型文の挿入</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger log <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>MyClass.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>のような定型的なものは Templates を使って任意のキーワードに紐付けて Ctrl + Shift で挿入できるようにします。やり方は Preferences を開いて、Java -> Editor -> Templates から登録できます。</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger log <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#123;</span>primary_type_name<span style="color: #009900;">&#125;</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>というコードを &#8220;logger&#8221; という名前で登録すると、エディタで logger と入力してCtrl + Shiftで登録したコードが挿入されてハッピーになれます。ただし、クラス名が長すぎると</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger log <span style="color: #339933;">=</span> LoggerFactory
	.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>MonitoringServerRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>のように残念な感じで改行されてしまいます。これは、Eclipseのデフォルトは80カラムで改行するような仕様になっているからなのですが、Preferences の Java -> Code Style -> Formatter -> Edit -> Line Wrapping タブ -> Maximum line width で変更できるので、ちょっと余裕を持たせてやると上のような改行は起こりにくくなります。ちなみに僕は100 に設定しています。</p>
<h3>プロジェクトにあるフォルダーをエクスプローラーで開きたい</h3>
<p>まーこれはピンポイントですがｗ、Eclipseのプロジェクトのフォルダーをエクスプローラーで開きたい場合がよくあります。対象のフォルダを選択 -> 右クリック -> Properties でフルパスが表示されるのでそのパスを丸ごとエクスプローラーにコピペ、が楽だと思います。他にもっといいやり方ありそうですが&#8230;</p>
<h3>まとめ</h3>
<p>Eclipseは調べると意外とナイスなショートカットキーがあるので、いろいろ調べてみるとよいと思います。こういうことを覚えることで少しづつコードを書くのが速くなりました。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/Eclipse-3-5-%E5%AE%8C%E5%85%A8%E6%94%BB%E7%95%A5-%E5%AE%AE%E6%9C%AC-%E4%BF%A1%E4%BA%8C/dp/4797356669%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4797356669"><img src="http://ecx.images-amazon.com/images/I/51jcB1mdBoL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/Eclipse-3-5-%E5%AE%8C%E5%85%A8%E6%94%BB%E7%95%A5-%E5%AE%AE%E6%9C%AC-%E4%BF%A1%E4%BA%8C/dp/4797356669%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4797356669">Eclipse 3.5 完全攻略</a></p>
	<p><em>著者／訳者：</em>宮本 信二</p>
	<p><em>出版社：</em>ソフトバンククリエイティブ( 2009-10-08 )</p>
	<p>大型本 ( 540 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>

<p><a href="http://feedads.g.doubleclick.net/~a/vOFxho0sap2OPBfHbYdv-RTp7Rc/0/da"><img src="http://feedads.g.doubleclick.net/~a/vOFxho0sap2OPBfHbYdv-RTp7Rc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/vOFxho0sap2OPBfHbYdv-RTp7Rc/1/da"><img src="http://feedads.g.doubleclick.net/~a/vOFxho0sap2OPBfHbYdv-RTp7Rc/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/9PcZPZ97oQw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/382/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tech.lampetty.net/tech/index.php/archives/382" />
	</item>
		<item>
		<title>EmacsでPythonの設定</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/380</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/380#comments</comments>
		<pubDate>Sat, 03 Jul 2010 16:36:01 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=380</guid>
		<description>最近Pythonいいなぁと思ってPython始めているのですが、コードを書くにあたってエディタの設定も多少しとかないと、ということでEmacsでPython回りの設定をしました。あんまりヘビーなことはしないので、とりあえ [...]</description>
			<content:encoded><![CDATA[<p>最近Pythonいいなぁと思ってPython始めているのですが、コードを書くにあたってエディタの設定も多少しとかないと、ということでEmacsでPython回りの設定をしました。あんまりヘビーなことはしないので、とりあえず今回はpython-mode.elとflymakeの設定だけしています。</p>
<h3>python-mode.el</h3>
<p><a href="https://launchpad.net/python-mode">ここ</a>からpython-mode.elをダウンロードして load-path が通っているところに置きます。自分はこんな感じのコードを .emacs に書いています。</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>add-hook 'python-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;">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> py-indent-offset <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>flymake(PyFlakes)</h3>
<p>Pythonのコードのシンタックスチェックを行う<a href="http://divmod.org/trac/wiki/DivmodPyflakes">PyFlakes</a>というソフトウェアをダウンロードして下記の要領でインストールします。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tar</span> xvzf pyflakes-0.4.0.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> pyflakes-0.4.0
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>DebianやUbuntuにはdebがあるので</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> pyflakes</pre></div></div>

<p>すればよいでしょう。あとは下記の設定を .emacs に追加すれば、.pyなファイルを保存したときに pyflakes によるシンタックスチェックが走ります。</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> flymake-python-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;pyflakes&quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</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>defconst flymake-allowed-python-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>.py$&quot;</span> flymake-python-init<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defvar flymake-python-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>):<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;">\\</span>(.*<span style="color: #000099; font-weight: bold;">\\</span>)&quot;</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">3</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-python-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>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-python-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-python-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>
<span style="color: #66cc66;">&#40;</span>add-hook 'python-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-python-load<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>実行イメージとしてはこんな感じ。</p>
<p style="text-align: center;"><a class="" rel="lightbox" title="pyflakes_on_emacs" href="http://www.flickr.com/photos/8250493@N05/4758028172/"><img class="alignnone" src="http://farm5.static.flickr.com/4141/4758028172_2475bab06c.jpg" alt="pyflakes_on_emacs"></a></p>
<p>ちなみに自分は C-c e でエラー内容が表示できるように下記の設定も足しています。</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><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>-c e&quot;</span> 'next-flymake-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>さて、これからPythonのコードをガンガン書いていきます！</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/%E3%81%BF%E3%82%93%E3%81%AA%E3%81%AEPython-%E6%94%B9%E8%A8%82%E7%89%88-%E6%9F%B4%E7%94%B0-%E6%B7%B3/dp/4797353953%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4797353953"><img src="http://ecx.images-amazon.com/images/I/41eXQ%2B8Rh2L._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/%E3%81%BF%E3%82%93%E3%81%AA%E3%81%AEPython-%E6%94%B9%E8%A8%82%E7%89%88-%E6%9F%B4%E7%94%B0-%E6%B7%B3/dp/4797353953%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4797353953">みんなのPython 改訂版</a></p>
	<p><em>著者／訳者：</em>柴田 淳</p>
	<p><em>出版社：</em>ソフトバンククリエイティブ( 2009-04-11 )</p>
	<p>単行本 ( 484 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>

<p><a href="http://feedads.g.doubleclick.net/~a/msqUI6d6LDfQdg7-EVEuw8ahTBo/0/da"><img src="http://feedads.g.doubleclick.net/~a/msqUI6d6LDfQdg7-EVEuw8ahTBo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/msqUI6d6LDfQdg7-EVEuw8ahTBo/1/da"><img src="http://feedads.g.doubleclick.net/~a/msqUI6d6LDfQdg7-EVEuw8ahTBo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/g-EtUf2gkkw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/380/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/380" />
	</item>
		<item>
		<title>サーバの設定ファイルの管理方法</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/379</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/379#comments</comments>
		<pubDate>Sun, 20 Jun 2010 12:32:22 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[SysAdmin]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=379</guid>
		<description>最近このブログの置き場所を自宅サーバからlinodeに移しました。それにあたってサーバの設定ファイルを git リポジトリで管理して linode に同期するようにしてみました。今日はそんなことをつらつらと書いてみたいと [...]</description>
			<content:encoded><![CDATA[<p>最近このブログの置き場所を自宅サーバからlinodeに移しました。それにあたってサーバの設定ファイルを git リポジトリで管理して linode に同期するようにしてみました。今日はそんなことをつらつらと書いてみたいと思います。</p>
<p>まず、なぜにサーバの設定ファイルをバージョン管理する必要があるのか、ということですが</p>
<ol>
<li>ファイルを任意の時点の状態に簡単に戻すことができる</li>
<li>いつどんな修正をしたのか簡単に見れる</li>
</ol>
<p>というのがメリットだと思ってます。あと、自分の場合はGitリポジトリがあるディレクトリを丸ごとバックアップ取っているので、リポジトリで管理しておくと自動的にバックアップされるというのがメリットとしてあります。色々熟考されて作られた設定ファイルは大事な財産だったりします。</p>
<h3>Gitリポジトリの構成</h3>
<p>僕は以下のような構成でGitリポジトリに設定ファイルを置いています。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">savacan
<span style="color: #000000; font-weight: bold;">|</span>
<span style="color: #000000; font-weight: bold;">`</span>-- napoli.lampetty.net
    <span style="color: #000000; font-weight: bold;">|</span>
    <span style="color: #000000; font-weight: bold;">`</span>-- files
        <span style="color: #000000; font-weight: bold;">`</span>-- etc
            <span style="color: #000000; font-weight: bold;">|</span>-- apache2
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">|</span>-- error-document.conf
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">|</span>-- icon.conf
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">|</span>-- language.conf
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">|</span>-- log-env.conf
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">`</span>-- mod_php.conf
            <span style="color: #000000; font-weight: bold;">|</span>-- init.d
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">|</span>-- mysql
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">|</span>-- reverse_proxy_nginx
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">`</span>-- svscan
            <span style="color: #000000; font-weight: bold;">|</span>-- logrotate.d
            <span style="color: #000000; font-weight: bold;">|</span>   <span style="color: #000000; font-weight: bold;">`</span>-- reverse_proxy_nginx
            <span style="color: #000000; font-weight: bold;">|</span>-- my.cnf
            <span style="color: #000000; font-weight: bold;">`</span>-- nginx
                <span style="color: #000000; font-weight: bold;">|</span>-- htpasswd
                <span style="color: #000000; font-weight: bold;">|</span>-- mime.types
                <span style="color: #000000; font-weight: bold;">`</span>-- reverse_proxy_nginx.conf</pre></div></div>

<p>TOPレベルの&#8221;savacan&#8221; というのは適当につけた名前ですが(*1)、簡単に解説するとこんな感じでしょうか。</p>
<ul>
<li>savacan配下にサーバ毎に napoli.lampetty.net のようなディレクトリを用意</li>
<li>その下の files ディレクトリに設定ファイルを置く</li>
<li>設定ファイルは実際にサーバに置くパスと同じものに</li>
</ul>
<p>そして、この files 配下のファイルをサーバに rsync して、各ファイルの symlink を張るスクリプトを書いています。symlink を張るという形を取ることで、サーバにアップすれば最新のファイルに置き換わるのが良いところかなぁと。あと実際にサーバ上に配置するパス通りに管理しておくのもわかりやすくて気に入っています。また、おそらく様々な用途のサーバがあるような環境だと、savacan配下に作るディレクトリは&#8221;db_master&#8221;,&#8221;reverse_proxy&#8221; のような役割にしておいて、db_master/files 配下のファイルはマスタDBサーバにrsyncする、みたいな運用にすればいいのかなと思っています。</p>
<p>最近は puppet も盛り上がってますし、puppet 使えば幸せになれることも多々あると思いますが(*2)、シンプルに運用したいのであればこのぐらいでもいいのかなぁと思っています。</p>
<ul>
<li>*1)サーバ管理 -> 鯖管 -> savacan</li>
<li>*2)ファイルの権限設定したい場合とか</li>
</ul>

<p><a href="http://feedads.g.doubleclick.net/~a/1EmDdnZBlss30wUKMozeA15_His/0/da"><img src="http://feedads.g.doubleclick.net/~a/1EmDdnZBlss30wUKMozeA15_His/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1EmDdnZBlss30wUKMozeA15_His/1/da"><img src="http://feedads.g.doubleclick.net/~a/1EmDdnZBlss30wUKMozeA15_His/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/kiO87p7D1z4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/379/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/379" />
	</item>
		<item>
		<title>VirtualBoxでディスクを拡張する</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/378</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/378#comments</comments>
		<pubDate>Wed, 09 Jun 2010 13:57:17 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[VirtualBox]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=378</guid>
		<description>VirtualBox + ゲストOS:Ubuntuでディスクを拡張する方法です。はしょっていうと、単に新しい仮想マシンを作ってそのディスクを古い仮想マシンのディスクにしてしまえばOKです。
1.新しく仮想マシンを作る
新 [...]</description>
			<content:encoded><![CDATA[<p>VirtualBox + ゲストOS:Ubuntuでディスクを拡張する方法です。はしょっていうと、単に新しい仮想マシンを作ってそのディスクを古い仮想マシンのディスクにしてしまえばOKです。</p>
<h3>1.新しく仮想マシンを作る</h3>
<p>新しいディスクを用意するために仮想マシンを新しく作ります。この時に作成したディスクが新しいディスクになるので、容量を多めに取っておきましょう。</p>
<h3>2.インストールディスクからUbuntuを起動</h3>
<p>Ubuntu 10.04のインストール用のisoを入手します。そのisoイメージをストレージのCD/DVDデバイスとして追加して、さらに1.でつくったディスクもストレージとして追加します。そして仮想マシンのUbuntu(古い方)を起動します。</p>
<h3>3.ディスクをコピー</h3>
<p>Ubuntuが起動したら、ターミナルを開いて</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">dd</span> <span style="color: #007800;"><span style="color: #000000; font-weight: bold;">if</span></span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda <span style="color: #007800;">of</span>=<span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb</pre></div></div>

<p>を実行します。これで古いディスク(/dev/sda)の内容が新しい方(/dev/sdb)にコピーされます。/dev/sda は環境によっては /dev/hda だったりするかもしれません。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>partitions</pre></div></div>

<p>してアタッチされているディスクを確認すると良いでしょう。</p>
<h3>4.パーティションをリサイズ</h3>
<p>GParted を使って、新しいディスクのパーティションを拡張しましょう。</p>
<h3>5.仮想マシンを再起動</h3>
<p>仮想マシンを一旦シャットダウンして、isoと古いディスクを使用しないようにしてから仮想マシンを起動して下さい。これで拡張済みのディスクがマウントされていれば、ディスクの拡張は完了です。</p>

<p><a href="http://feedads.g.doubleclick.net/~a/g_fJ8MbG0l8jUU58fE6zIMEHLRA/0/da"><img src="http://feedads.g.doubleclick.net/~a/g_fJ8MbG0l8jUU58fE6zIMEHLRA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/g_fJ8MbG0l8jUU58fE6zIMEHLRA/1/da"><img src="http://feedads.g.doubleclick.net/~a/g_fJ8MbG0l8jUU58fE6zIMEHLRA/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/bob7nYeTIsk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/378/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/378" />
	</item>
		<item>
		<title>qpstudy #01 “rsyncのちょっとイイ話”のスライド</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/377</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/377#comments</comments>
		<pubDate>Sat, 22 May 2010 01:02:43 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[勉強会]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=377</guid>
		<description>qpstudy #01で「rsyncのちょっとイイ話 」というタイトルで話させてもらいました。勉強会で発表というのは初めてで緊張したのですが、なんとか終えられてよかったです。終わった後も参加者の方々からtwitterでr [...]</description>
			<content:encoded><![CDATA[<p><a href="http://sites.google.com/site/qpstudy/qpstudy01">qpstudy #01</a>で「rsyncのちょっとイイ話 」というタイトルで話させてもらいました。勉強会で発表というのは初めてで緊張したのですが、なんとか終えられてよかったです。終わった後も参加者の方々からtwitterでrsyncについて色々フォローしてもらい、自分としても勉強になりました。</p>
<p>というわけで資料はこちら。</p>
<div style="width:425px" id="__ss_4168853"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/oinume/qpstudy01-rsync-4168853" title="rsyncのちょっとイイ話">rsyncのちょっとイイ話</a></strong><object id="__sse4168853" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=qpstudy01rsync-100519223533-phpapp02&#038;stripped_title=qpstudy01-rsync-4168853" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse4168853" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=qpstudy01rsync-100519223533-phpapp02&#038;stripped_title=qpstudy01-rsync-4168853" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px">View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/oinume">Kazuhiro Oinuma</a>.</div>
</div>
<p>qpstudy 第2回目も予定されているとのことなので楽しみですね。また何かネタがあったらやりたいな。</p>

<p><a href="http://feedads.g.doubleclick.net/~a/uIQkBubghYxsOV_hjYBJcyyjaBE/0/da"><img src="http://feedads.g.doubleclick.net/~a/uIQkBubghYxsOV_hjYBJcyyjaBE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/uIQkBubghYxsOV_hjYBJcyyjaBE/1/da"><img src="http://feedads.g.doubleclick.net/~a/uIQkBubghYxsOV_hjYBJcyyjaBE/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/fp0WUkVQA24" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/377/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/377" />
	</item>
		<item>
		<title>VirtualBoxでのネットワーク設定(NAT+ホストオンリーアダプタによるホストOSとゲストOS間の通信)</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/375</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/375#comments</comments>
		<pubDate>Sun, 11 Apr 2010 06:35:21 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[VirtualBox]]></category>
		<category><![CDATA[仮想化]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=375</guid>
		<description>CassandraやMogileFS、KumoFSなど、複数のサーバで構成されるサーバソフトウェアをテストしたいことが多々あるので、VirtualBoxを使って分散環境を作ってみました。ネットワーク関連の設定でややコツが [...]</description>
			<content:encoded><![CDATA[<p>CassandraやMogileFS、KumoFSなど、複数のサーバで構成されるサーバソフトウェアをテストしたいことが多々あるので、VirtualBoxを使って分散環境を作ってみました。ネットワーク関連の設定でややコツが必要だったので、ちょっとまとめてみました。</p>
<h3>要件</h3>
<p>ホストOS上で複数のゲストOS(Linux)を立ち上げる。</p>
<ul>
<li>ホストOSからゲストOSにSSHでアクセスしたいので、ホストOSからゲストOS間にアクセスできること</li>
<li>分散コンピューティングのテストがしたいので、ゲストOS間でもアクセスできること</li>
<li>ゲストOSからインターネットに出れること</li>
<li>ネットワーク上にある他のマシンからはゲストOSにはアクセスできなくてもOK</li>
</ul>
<h3>やり方</h3>
<p>今回はネットワーク上の他のマシンからはアクセスできなくてもよいので、ネットワークの設定ではブリッジアダプタを使わずに、ネットワークアダプタを2つ使い、NAT+ホストオンリーアダプタという構成にします。NATというのはゲストOSから外部ネットワークに接続できる構成で、ホストオンリーアダプタというのはホストOSとゲストOS間のみでやり取りできるアダプタです。</p>
<p>具体的なやり方を書いておくと</p>
<ol>
<li>VirtualBoxを<a href="http://jp.sun.com/products/software/virtualbox/">Sunのサイト</a>からダウンロードします。(自分は3.1.6 を使用しています)</li>
<li>仮想マシンを作成し、ネットワークの設定でアダプタ1で&#8221;NAT&#8221;を、アダプタ2に&#8221;ホストオンリーアダプタ&#8221;を選択します</li>
<li>終わったらOSをインストールして下さい。自分は今回CentOS 5.4を使用しました。</li>
</ol>
<p>となります。これをゲストOSの数だけ繰り返して下さい。(もしくは仮想マシンを一つ作ってそれをコピーするのもよいでしょう)</p>
<p>インストールが終わったらゲストOSにログインして /sbin/ifconfig を実行してみて下さい。自分は今回centos-01とcentos-02 の2つのゲストOSを作ったところ、DHCPで下記のようにIPが割り当てられていました。</p>
<ul>
<li>centos-01: eth0 10.0.2.15, eth1: 192.168.56.101</li>
<li>centos-02: eth0 10.0.2.15, eth1: 192.168.56.102</li>
</ul>
<p>この時点で期待していたことができるか確かめてみます。</p>
<ul>
<li>ホストOSから192.168.56.101,102にping</li>
<li>ゲストOSからgoogle.com(外部)にping</li>
<li>ゲストOSから192.168.56.1(ホストOSのIP)にping</li>
<li>ゲストOS 192.168.56.101から192.168.56.102(centos-02)にping</li>
</ul>
<p>以上で複数サーバによる分散環境ができましたが、サーバ用途ではDHCPだと後々面倒な事になりそうなので、最後にこのDHCPで割り当てられた設定を参考に static なIPを設定しておきます。具体的にはゲストOSのeth1を192.168.56.10xにすればOKです。以下はcentos-01 の /etc/sysconfig/network-scripts/ifcfg-eth1 の変更箇所です。</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">--- ifcfg-eth1.orig     <span style="">2010</span>-04-<span style="">12</span> 00:<span style="">11</span>:<span style="">39.000000000</span> +0900
<span style="color: #888822;">+++ ifcfg-eth1  <span style="">2010</span>-04-<span style="">12</span> 00:<span style="">18</span>:<span style="">52.000000000</span> +0900</span>
<span style="color: #440088;">@@ -<span style="">1</span>,<span style="">6</span> +<span style="">1</span>,<span style="">9</span> @@</span>
 # Advanced Micro Devices <span style="">&#91;</span>AMD<span style="">&#93;</span> <span style="color: #440088;">79c970</span> <span style="">&#91;</span>PCnet32 LANCE<span style="">&#93;</span>
 DEVICE=eth1
<span style="color: #991111;">-BOOTPROTO=dhcp</span>
<span style="color: #00b000;">+BOOTPROTO=static</span>
 HWADDR=08:00:<span style="">27</span>:BD:DD:6C
 ONBOOT=yes
<span style="color: #991111;">-DHCP_HOSTNAME=centos-01</span>
<span style="color: #00b000;">+IPADDR=192.168.56.101</span>
<span style="color: #00b000;">+NETMASK=255.255.255.0</span>
<span style="color: #00b000;">+NETWORK=192.168.56.0</span></pre></div></div>

<p>変更したらnetworkを再起動します。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/init.d/network restart</span></pre></div></div>

<p>centos-02は 192.168.56.102 のIPに設定しています。centos-01からcentos-02にpingしてみて通るか確認してみて下さい。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ping</span> 192.168.1.102</pre></div></div>

<p>これで通れば設定完了です。無事複数サーバによる分散環境ができました。</p>
<h3>余談：なんでVirtualBox？</h3>
<ul>
<li>無償で使える</li>
<li>Windows,Mac,Linuxに対応している</li>
<li>GUIによる設定画面がわりとわかりやすい</li>
</ul>
<p>というのが気に入って使っています。VMwareもWindows,Mac,Linuxで使えますが、Mac版のVMware Fusionは有償なのがちょっと嫌でした。VirtualBoxは使っていてちょっと重いと思うこともありますが、突然落ちたりはせず非常に安定しているので、デスクトップPCで軽く仮想環境を作りたい場合には重宝しています。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/CentOS%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-DVD%E4%BB%98-%E4%B8%AD%E5%B3%B6-%E8%83%BD%E5%92%8C/dp/4798114626%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798114626"><img src="http://ecx.images-amazon.com/images/I/4139-4nMWdL._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/CentOS%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-%E7%AC%AC2%E7%89%88-DVD%E4%BB%98-%E4%B8%AD%E5%B3%B6-%E8%83%BD%E5%92%8C/dp/4798114626%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4798114626">CentOS徹底入門 第2版 (DVD付)</a></p>
	<p><em>著者／訳者：</em>中島 能和 飛田 伸一郎 </p>
	<p><em>出版社：</em>翔泳社( 2008-02-05 )</p>
	<p>大型本 ( 580 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>

<p><a href="http://feedads.g.doubleclick.net/~a/_c51nTR9ROWJ8fJIMZC88ZMt4IU/0/da"><img src="http://feedads.g.doubleclick.net/~a/_c51nTR9ROWJ8fJIMZC88ZMt4IU/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_c51nTR9ROWJ8fJIMZC88ZMt4IU/1/da"><img src="http://feedads.g.doubleclick.net/~a/_c51nTR9ROWJ8fJIMZC88ZMt4IU/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/xHFAUMgtF5M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/375/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/375" />
	</item>
		<item>
		<title>コマンドラインオプションを解析するPerlモジュールGetopt::Compact</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/374</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>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: #0000ff;">@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/417K6qXAgvL._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>

<p><a href="http://feedads.g.doubleclick.net/~a/9-53Szpww0EG6Zg9XxrBG1H32hI/0/da"><img src="http://feedads.g.doubleclick.net/~a/9-53Szpww0EG6Zg9XxrBG1H32hI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/9-53Szpww0EG6Zg9XxrBG1H32hI/1/da"><img src="http://feedads.g.doubleclick.net/~a/9-53Szpww0EG6Zg9XxrBG1H32hI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/92tJ41_38IE" height="1" width="1"/>]]></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>Shellでgetoptsを使ってコマンドライン引数の解析</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/373</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/373#comments</comments>
		<pubDate>Tue, 06 Apr 2010 11:28:56 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=373</guid>
		<description>だいぶ前の話、Perlを使わないでShellスクリプトで頑張っていた頃、Shellでコマンドラインオプションの解析をやる時は

#!/bin/sh
&amp;#160;
for OPT in $*
do
    case $OP [...]</description>
			<content:encoded><![CDATA[<p>だいぶ前の話、Perlを使わないでShellスクリプトで頑張っていた頃、Shellでコマンドラインオプションの解析をやる時は</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> OPT <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$*</span>
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$OPT</span> <span style="color: #000000; font-weight: bold;">in</span>
    <span style="color: #ff0000;">'-x'</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">FLAG_X</span>=<span style="color: #ff0000;">&quot;TRUE&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #ff0000;">'-y'</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #7a0874; font-weight: bold;">shift</span>
        <span style="color: #007800;">FLAG_Y</span>=<span style="color: #ff0000;">&quot;TRUE&quot;</span>
        <span style="color: #007800;">VALUE_Y</span>=$<span style="color: #000000;">1</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">shift</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$FLAG_X</span>&quot;</span> = <span style="color: #ff0000;">&quot;TRUE&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Option -x specified.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$FLAG_Y</span>&quot;</span> = <span style="color: #ff0000;">&quot;TRUE&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Option -y <span style="color: #007800;">$VALUE_Y</span> specified.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>という感じで $* と shift を使ってやっていたのですが、最近 getopts なる素敵なビルトインコマンドを知りました。これを使うと上のスクリプトは下のように書き直すことができます。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">getopts</span> xy: OPT
<span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #007800;">$OPT</span> <span style="color: #000000; font-weight: bold;">in</span>
    <span style="color: #ff0000;">&quot;x&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">FLAG_X</span>=<span style="color: #ff0000;">&quot;TRUE&quot;</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #ff0000;">&quot;y&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">FLAG_Y</span>=<span style="color: #ff0000;">&quot;TRUE&quot;</span>
        <span style="color: #007800;">VALUE_Y</span>=<span style="color: #007800;">$OPTARG</span>
        <span style="color: #000000; font-weight: bold;">;;</span>
    <span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$FLAG_X</span>&quot;</span> = <span style="color: #ff0000;">&quot;TRUE&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Option -x specified.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$FLAG_Y</span>&quot;</span> = <span style="color: #ff0000;">&quot;TRUE&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Option -y <span style="color: #007800;">$VALUE_Y</span> specified.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>スクリプトの行数自体はあんまり変わらないように見えますが、shift 忘れをよくやらかしていたので、それが無くなった分つまらないバグを入れ込まなくなった気がします。</p>
<h3>getoptsの解説</h3>
<p>getopts の引数にオプションの文字を指定しますが、文字のあとに : (コロン)をつけると、引数ありのオプションという扱いになります。さらに</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>getopts.sh <span style="color: #660033;">-z</span></pre></div></div>

<p>のように未定義のオプションを指定すると</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>oinume<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>getopts.sh: illegal option <span style="color: #660033;">--</span> z</pre></div></div>

<p>というように怒られます。このエラーハンドリングを自前でやるには :xy: のように、getopts の引数の文字列の最初を : にすればいいみたい。</p>
<p>さらに $OPTIND という変数を下記のように -1 してやることで、オプション以降に与えられた引数を取得することができます。例えば、上のスクリプトに下記を足して</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">shift</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$OPTIND</span> - <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Argument $1 specified.&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ ~<span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>getopts.sh <span style="color: #660033;">-x</span> <span style="color: #660033;">-y</span> yyy hoge</pre></div></div>

<p>のように実行すると</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Option <span style="color: #660033;">-x</span> specified.
Option <span style="color: #660033;">-y</span> yyy specified.
Argument hoge specified.</pre></div></div>

<p>となります。getopts を実行すると、$OPTIND にはオプションのインデックス番号が保存されているので、これを -1 して shift  してやることで、オプション以降の引数が取得できるようになるという仕組みです。</p>
<p>最近はこういうレベルのプログラムだったらPerlで書くことも多かったのですが、Shell でも十分いけそうだと実感しました。</p>
<div class="tmkm-amazon-view">
	<p><a href="http://www.amazon.co.jp/%E8%A9%B3%E8%A7%A3-%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88-Arnold-Robbins/dp/4873112672%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873112672"><img src="http://ecx.images-amazon.com/images/I/51EAPCH56ML._SL160_.jpg" border="0" alt="" /></a></p>
	<p><a href="http://www.amazon.co.jp/%E8%A9%B3%E8%A7%A3-%E3%82%B7%E3%82%A7%E3%83%AB%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88-Arnold-Robbins/dp/4873112672%3FSubscriptionId%3DAKIAJGVWECUONT35GEJA%26tag%3Dkazzhomeunixo-22%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D4873112672">詳解 シェルスクリプト</a></p>
	<p><em>著者／訳者：</em>Arnold Robbins Nelson H. F. Beebe </p>
	<p><em>出版社：</em>オライリージャパン( 2006-01-16 )</p>
	<p>大型本 ( 345 ページ )</p>
<hr class="tmkm-amazon-clear" /></div>

<p><a href="http://feedads.g.doubleclick.net/~a/BpvC4n-S4wycUX3vcxTgMod0LBg/0/da"><img src="http://feedads.g.doubleclick.net/~a/BpvC4n-S4wycUX3vcxTgMod0LBg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/BpvC4n-S4wycUX3vcxTgMod0LBg/1/da"><img src="http://feedads.g.doubleclick.net/~a/BpvC4n-S4wycUX3vcxTgMod0LBg/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/Q6QArLcsliI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/373/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/373" />
	</item>
		<item>
		<title>Shellで配列のマージ</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/372</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/372#comments</comments>
		<pubDate>Thu, 25 Mar 2010 11:24:03 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=372</guid>
		<description>Shellスクリプトで配列のマージってどうやるんだろうって思ったので調べてみたら、${ARRAY[*]} か ${ARRAY[@]} で配列の要素を全部取得できるらしいので、それを () 使ってマージすればいいらしい。
 [...]</description>
			<content:encoded><![CDATA[<p>Shellスクリプトで配列のマージってどうやるんだろうって思ったので調べてみたら、${ARRAY[*]} か ${ARRAY[@]} で配列の要素を全部取得できるらしいので、それを () 使ってマージすればいいらしい。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #007800;">ARRAY1</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span>1.1.1.1 2.2.2.2<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">ARRAY2</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span>3.3.3.3 4.4.4.4<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #007800;">ARRAY3</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span>4.4.4.4 5.5.5.5<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #007800;">MERGED</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #800000;">${ARRAY1[*]}</span> <span style="color: #800000;">${ARRAY2[*]}</span> <span style="color: #800000;">${ARRAY3[*]}</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> n <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${MERGED[*]}</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$n</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">1.1.1.1
2.2.2.2
3.3.3.3
4.4.4.4
4.4.4.4
5.5.5.5</pre></div></div>

<p>となる。重複してる値はもちろんケアしてくれないので、頑張って取り除く必要あり。</p>

<p><a href="http://feedads.g.doubleclick.net/~a/guyiLbT-RmkzuRiTT5BmaX3cxeI/0/da"><img src="http://feedads.g.doubleclick.net/~a/guyiLbT-RmkzuRiTT5BmaX3cxeI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/guyiLbT-RmkzuRiTT5BmaX3cxeI/1/da"><img src="http://feedads.g.doubleclick.net/~a/guyiLbT-RmkzuRiTT5BmaX3cxeI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/dwLQD-eRlJI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/372/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/372" />
	</item>
		<item>
		<title>海外のVPS Linodeを借りました</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/370</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/370#comments</comments>
		<pubDate>Fri, 19 Mar 2010 22:20:57 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[VPS]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=370</guid>
		<description>表題の通りなのですが、やっぱり自宅サーバでサービスを運営するのがすごい嫌になったのでLinodeを契約しました。プランは一番安いLinode 360で、契約してもうかれこれ3週間ぐらいですが、かなり快適に使ってます。良い [...]</description>
			<content:encoded><![CDATA[<p>表題の通りなのですが、やっぱり自宅サーバでサービスを運営するのがすごい嫌になったので<a href="http://www.linode.com/">Linode</a>を契約しました。プランは一番安いLinode 360で、契約してもうかれこれ3週間ぐらいですが、かなり快適に使ってます。良い点を挙げると&#8230;</p>
<ul>
<li>データセンターにFremont(カリフォルニア)を選ぶと、SSHでストレスはない(<a href="http://www.slicehost.com/">Slicehost</a>の時はアメリカの真ん中でネットワーク遅延が激しかったので使い物にならなかった)</li>
<li>CPUが(空いてれば)最大で4つまで使える。ちなみにCPUは&#8221;Intel(R) Xeon(R) CPU L5520  @ 2.27GHz&#8221; でかなり速い</li>
<li>まだ試してないけど簡単にプランがアップグレードできる(12分間のダウンタイムあり)</li>
<li>OSが自由に選べる(日本のVPSだと選択肢が少なかったり)</li>
</ul>
<p>という感じで、今のところ不満はありません。なお契約してから知ったのですが、メモリの増量も 90MBで$5 / month で可能なようです。というわけでもしVPS探している人がいれば<a href="http://www.linode.com/?r=daff622a3693dd3eee9b4133bfdbc13957ca06af">こちら</a>からどうぞ。(僕にアフィリエイトが入ります)</p>

<p><a href="http://feedads.g.doubleclick.net/~a/LWvuQFZKC22nZYWiTHIByw937Mk/0/da"><img src="http://feedads.g.doubleclick.net/~a/LWvuQFZKC22nZYWiTHIByw937Mk/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/LWvuQFZKC22nZYWiTHIByw937Mk/1/da"><img src="http://feedads.g.doubleclick.net/~a/LWvuQFZKC22nZYWiTHIByw937Mk/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/Ix8sqbUfQro" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/370/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/370" />
	</item>
		<item>
		<title>MuninでMySQLプラグインのグラフが描画されない時の解決方法</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/369</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/369#comments</comments>
		<pubDate>Tue, 09 Mar 2010 14:09:58 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Munin]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=369</guid>
		<description>先日設定したサーバでMuninをインストールして監視していたのですが、どうもMySQL関連のプラグイン(mysql_bytes, mysql_queries, mysql_threadsなど)でグラフが描画されず、どうし [...]</description>
			<content:encoded><![CDATA[<p>先日設定したサーバでMuninをインストールして監視していたのですが、どうもMySQL関連のプラグイン(mysql_bytes, mysql_queries, mysql_threadsなど)でグラフが描画されず、どうしたものかと思ってぐぐってみたところ、<a href="http://www.mydigitallife.info/2007/08/25/fix-for-munin-mysql-monitoring-plugin-graph-not-showing-or-blank/">このエントリ</a>を見つけました。まさにビンゴで /etc/munin/plugin-conf.d/munin-node に以下の行を足してmunin-nodeをrestartしたところ、うまくグラフが描画されるようになりました。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">env.mysqladmin <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>mysqladmin</pre></div></div>

<p>Muninの問題なのか何なのか、自分のケースでは mysql 関連のコマンドを /usr/local/bin/ 配下にインストールしてたので、ここにPATHが通っていなかったのが問題のようです。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #660033;">-umunin</span> munin-run mysql_bytes</pre></div></div>

<p>と実行してもうまく数値が出てきたのでどうしたものかと悩んでいたのですが、解決してすっきりしました。</p>

<p><a href="http://feedads.g.doubleclick.net/~a/a-tMaH-I4zPtU5M2Dde5dNc-TM4/0/da"><img src="http://feedads.g.doubleclick.net/~a/a-tMaH-I4zPtU5M2Dde5dNc-TM4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/a-tMaH-I4zPtU5M2Dde5dNc-TM4/1/da"><img src="http://feedads.g.doubleclick.net/~a/a-tMaH-I4zPtU5M2Dde5dNc-TM4/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/hv4lECwJu4k" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/369/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/369" />
	</item>
		<item>
		<title>UbuntuでTimeZoneを変更する方法</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/368</link>
		<comments>http://tech.lampetty.net/tech/index.php/archives/368#comments</comments>
		<pubDate>Sun, 28 Feb 2010 05:37:44 +0000</pubDate>
		<dc:creator>oinume</dc:creator>
				<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://tech.lampetty.net/tech/?p=368</guid>
		<description>昔は tzconfig というのを使えばよかったらしいのですが、deprecated と言われました&amp;#8230;

# dpkg-reconfigure tzdata

と入力して、Asia -&gt; Tokyo を選択す [...]</description>
			<content:encoded><![CDATA[<p>昔は tzconfig というのを使えばよかったらしいのですが、deprecated と言われました&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># dpkg-reconfigure tzdata</span></pre></div></div>

<p>と入力して、Asia -> Tokyo を選択すればOKです。終わったら date コマンドで確認すればOK。</p>

<p><a href="http://feedads.g.doubleclick.net/~a/DpK28k6xmea49h_Deh0ieX1qwno/0/da"><img src="http://feedads.g.doubleclick.net/~a/DpK28k6xmea49h_Deh0ieX1qwno/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/DpK28k6xmea49h_Deh0ieX1qwno/1/da"><img src="http://feedads.g.doubleclick.net/~a/DpK28k6xmea49h_Deh0ieX1qwno/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/hW6OLbqVLtk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tech.lampetty.net/tech/index.php/archives/368/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/368" />
	</item>
		<item>
		<title>DBI-&gt;connectのHandleError</title>
		<link>http://tech.lampetty.net/tech/index.php/archives/366</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>DBIのちょっとしたTIPSです。DBIには connect する時に

DBI-&amp;#62;connect&amp;#40;'dbi:mysql:database=hoge', 'root', 'whatever', &amp;#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 23<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>

<p><a href="http://feedads.g.doubleclick.net/~a/6x0EUstPxSaoDF2ciwZ2n8tmtkY/0/da"><img src="http://feedads.g.doubleclick.net/~a/6x0EUstPxSaoDF2ciwZ2n8tmtkY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/6x0EUstPxSaoDF2ciwZ2n8tmtkY/1/da"><img src="http://feedads.g.doubleclick.net/~a/6x0EUstPxSaoDF2ciwZ2n8tmtkY/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/oinume/diary/~4/1NMq2XkEZTU" height="1" width="1"/>]]></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>
	</channel>
</rss>
