blog/content/post/2008-03-13-『新版perl言語プログラミング...

110 lines
8.6 KiB
Markdown

---
title: '『[新版Perl言語プログラミングレッスン入門編』第11章 ファイル操作・途中'
author: kazu634
date: 2008-03-13
url: /2008/03/13/_903/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:3823;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
- Programming
---
<div class="section">
<p>
 今日はファイル操作だよー。
</p>
<p>
<a name="seemore"></a>
</p>
<h4>
カレントディレクトリのファイルを一覧で表示する
</h4>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$dirname</span> = <span class="synConstant">&#34;.&#34;</span>;
<span class="synStatement">opendir</span>(<span class="synIdentifier">DIR</span>, <span class="synIdentifier">$dirname</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$dirname</span><span class="synConstant">: </span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synStatement">while</span> (<span class="synStatement">my</span> <span class="synIdentifier">$dir</span> = <span class="synStatement">readdir</span>(<span class="synIdentifier">DIR</span>)) {
<span class="synStatement">print</span> <span class="synIdentifier">$dir</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
}
<span class="synStatement">closedir</span>(<span class="synIdentifier">DIR</span>);
</pre>
<h4>
カレントディレクトリ内にある特定の拡張子のファイルを一括して処理
</h4>
<p>
こんな感じがお約束らしい。
</p>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synComment"># 変数の宣言</span>
<span class="synStatement">my</span> <span class="synIdentifier">$hline</span> = <span class="synConstant">'=-'</span> x <span class="synConstant">30</span> . <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">my</span> <span class="synIdentifier">$dirname</span> = <span class="synConstant">'.'</span>;
<span class="synComment"># カレントディレクトリを開く</span>
<span class="synStatement">opendir</span>(<span class="synIdentifier">DIR</span>, <span class="synIdentifier">$dirname</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$dirname</span><span class="synConstant">: </span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synComment"># カレントディレクトリのファイルを逐一チェックして処理を行う</span>
<span class="synStatement">while</span> (<span class="synStatement">my</span> <span class="synIdentifier">$dir</span> = <span class="synStatement">readdir</span>(<span class="synIdentifier">DIR</span>)) {
<span class="synComment"># 普通のファイルじゃなければ、次のファイルチェックする</span>
<span class="synStatement">next</span> <span class="synStatement">unless</span> (<span class="synStatement">-f</span> <span class="synIdentifier">$dir</span>);
<span class="synComment"># ファイルの拡張子が「.html」じゃなければパスして、次のファイルへ</span>
<span class="synStatement">next</span> <span class="synStatement">unless</span> (<span class="synIdentifier">$dir</span> =~<span class="synStatement"> /</span><span class="synSpecial">\.</span><span class="synConstant">html$</span><span class="synStatement">/</span>);
<span class="synComment"># 処理するファイルの表示</span>
<span class="synStatement">print</span> <span class="synIdentifier">$hline</span>;
<span class="synStatement">print</span> <span class="synIdentifier">$dir</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synStatement">print</span> <span class="synIdentifier">$hline</span>;
<span class="synComment"># 実際にファイルを開いて、中身を表示する</span>
<span class="synStatement">open</span>(<span class="synIdentifier">FILE</span>, <span class="synConstant">'$dir'</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synStatement">while</span> (<span class="synStatement">my</span> <span class="synIdentifier">$line</span> = <span class="synIdentifier">&#60;FILE&#62;</span>) {
<span class="synStatement">print</span> <span class="synIdentifier">$line</span>;
}
<span class="synStatement">close</span>(<span class="synIdentifier">FILE</span>);
}
<span class="synComment"># ディレクトリを閉じる</span>
<span class="synStatement">closedir</span>(<span class="synIdentifier">DIR</span>😉
</pre>
<h4>
サブディレクトリも再帰的に呼び出すことで作業できるようにする
</h4>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synIdentifier">&#38;traverse</span>(<span class="synConstant">'.'</span>);
<span class="synStatement">sub</span><span class="synIdentifier"> traverse </span>{
<span class="synComment"># 引数を$dirnameに代入</span>
<span class="synStatement">my</span> <span class="synIdentifier">$dirname</span> = <span class="synStatement">shift</span>;
<span class="synComment"># パスの区切り文字を指定</span>
<span class="synStatement">my</span> <span class="synIdentifier">$delim</span> = <span class="synConstant">'/'</span>;
<span class="synComment"># ディレクトリを開いて、一つ一つ処理</span>
<span class="synStatement">opendir</span>(<span class="synIdentifier">DIR</span>, <span class="synIdentifier">$dirname</span>) <span class="synStatement">or</span> <span class="synStatement">die</span> <span class="synConstant">&#34;</span><span class="synIdentifier">$dirname</span><span class="synConstant">: </span><span class="synIdentifier">$!</span><span class="synConstant">&#34;</span>;
<span class="synStatement">foreach</span> <span class="synStatement">my</span> <span class="synIdentifier">$entry</span> (<span class="synStatement">readdir</span>(<span class="synIdentifier">DIR</span>)) {
<span class="synComment"># カレントディレクトリだったらパス</span>
<span class="synStatement">next</span> <span class="synStatement">if</span> (<span class="synIdentifier">$entry</span> <span class="synStatement">eq</span> <span class="synConstant">'.'</span>);
<span class="synComment"># 親ディレクトリだったらパス</span>
<span class="synStatement">next</span> <span class="synStatement">if</span> (<span class="synIdentifier">$entry</span> <span class="synStatement">eq</span> <span class="synConstant">'..'</span>);
<span class="synComment"># $dirnameの最後が「/」or「\」だったら、区切り文字をnullに</span>
<span class="synStatement">if</span> (<span class="synIdentifier">$dirname</span> =~<span class="synStatement"> /</span><span class="synConstant">[</span><span class="synSpecial">\/\\</span><span class="synConstant">]$</span><span class="synStatement">/</span>) {
<span class="synIdentifier">$delim</span> = <span class="synConstant">''</span>;
}
<span class="synComment"># 表示</span>
<span class="synStatement">my</span> <span class="synIdentifier">$filename</span> = <span class="synConstant">&#34;</span><span class="synIdentifier">$dirname$delim$entry</span><span class="synConstant">&#34;</span>;
<span class="synStatement">print</span> <span class="synIdentifier">$filename</span>, <span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>;
<span class="synComment"># ディレクトリなら再帰的にtraverseを呼び出す</span>
<span class="synStatement">if</span> (<span class="synStatement">-d</span> <span class="synIdentifier">$filename</span>) {
<span class="synIdentifier">&#38;traverse</span>(<span class="synIdentifier">$filename</span>);
} <span class="synStatement">else</span> {
ここで処理したい内容を書く!
}
}
}
</pre>
</div>