dyno2316 ロヒミフ perl VBA Excel 秀丸

Windows版ActivePerl5.8.Xで日本語処理

-------------------------------------------------------------
Windows版 ActivePerl 5.8.X で日本語処理をする時のおまじない
-------------------------------------------------------------
Windows版 ActivePerl 5.8.X 日本語テキストファイルの操作


Windows 版 ActivePerl 5.8.8 以降で
シフトJISの日本語テキストファイルを操作するときは、
次のように宣言したほうがいいということ。
(以前のバージョンはバク抱えている)

use encoding "shiftjis";
use open ":encoding(shiftjis)";
use open ":std";
map{ binmode($_,":crlf"); } qw/STDIN STDOUT STDERR/;

1行目、use encoding "shiftjis"; --- テキストファイルの中でシフトJISを
使うという宣言。そして、標準入出力もシフトJISになる。
2行目、use open ":encoding(shiftjis)"; --- オープンするファイルのデ
フォルトのエンコードの宣言。つまり、シフトJISとして扱う。
3行目、use open ":std"; --- 標準入力(STDIN)、標準出力(STDOUT)、標準エ
ラー出力(STDERR)のencodingを1行目で宣言したshiftjis にする。なぜ、こ
こでもう一度、宣言するかというと、2行目の use open ":encoding
(shiftjis)"; を実行すると、標準入出力がデフォルトにリセットされるため
である。
4行目、map{ binmode($_,":crlf"); } qw/STDIN STDOUT STDERR/; では、標準
入力、標準出力、標準エラー出力の行末コードをCR LFに設定している。これ
は前回、Windows版Active Perl 5.8.X 日本語テキストファイルの標準入出力
で説明したように内部的には設定されている :crlf が無効になっているので、
再設定している。そんな悲しいバグがある。

"rem > test.txt " などと、コマンドラインで、実行して、予め、"test.txt"
というファイルを用意して、次のプログラムを試してみるとわかる。

use encoding "shiftjis";
use open ":encoding(shiftjis)";

open(F1,"test.txt") or die;
open(F2,"<","test.txt") or die;
open(F3,">","outfile1.txt") or die;
open(F4,"<test.txt") or die;
open(F5,">outfile2.txt") or die;
map{print $_,":¥t",join(" / ",PerlIO::get_layers($_)),"¥n" } qw/F1 F2 F3 F4 F5/;

Perl 5.8.8 で試したところ、次のような結果になった。

F1: unix / crlf / encoding(shiftjis) / utf8
F2: unix / crlf / encoding(shiftjis) / utf8
F3: unix / crlf / encoding(shiftjis) / utf8
F4: unix / crlf / encoding(shiftjis) / utf8
F5: unix / crlf / encoding(shiftjis) / utf8


(引用)http://hardsoft.at.webry.info/200604/article_2.html
(おまけ)Windows版 ActivePerl 5.8.X で Excel から日本語データを読む
     http://hardsoft.at.webry.info/200603/article_11.html