ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Perlコミュのutf8で日本語ファイル名がオープン出来ない

  • mixiチェック
  • このエントリーをはてなブックマークに追加
utf8で、日本語ファイル名を使った場合に、ファイルをオープン出来ない。この時に、
$filename="02.txt"の様にすると、正常にオープン出来る。コードを下記に示します。
下記のコードできちんとファイルをオープンする為には、どうすれば良いのでしょう
か。ご教授をお願いします。

use utf8;
binmode STDIN, ":encoding(cp932)";
binmode STDERR, ":encoding(cp932)";
binmode STDOUT, ":encoding(cp932)";
$filename="日本語処理.txt";
open(IN,"<:encoding(cp932)",$filename)||die "file open err\n";
while(<>){
print;
}
close(IN);

コメント(18)

openできても処理が進まないという間違いがありますが、それを直したら動きました。
ちなみに、Debian Linuxのテスト版、Perlはバージョン5.14.2です。ファイル名はUTF-8で書きました。

dieのところで$!も出力させれば、何のエラーかわかります。
早速のご返事ありがとうございます。dieで$!を表示させた所、No such file or
directoryのエラーです。ファイル名が認識出来ていません。私の環境は最初に説明が足
りませんでしたが。linuxでは無くWindowsXPです。

宜しく、お願いします。
済みません。先ほどのはファイルのオープンの問題では無くて。単に表示をするだけでも
現象が出ます。下記に例を示します。この時のエラーメッセージは、Wide character in
subroutine entry at C:/Perl/lib/Encode.pm line 170.となります。宜しく、お願いし
ます。
use utf8;
binmode STDIN, ":encoding(cp932)";
binmode STDERR, ":encoding(cp932)";
binmode STDOUT, ":encoding(cp932)";
print $filename;
何度も、済みません。表示は正常に出ます。例を下記に示します。
use utf8;
binmode STDIN, ":encoding(cp932)";
binmode STDERR, ":encoding(cp932)";
binmode STDOUT, ":encoding(cp932)";
$filename="持出しリスト.txt";
print __LINE__ . " $filename\n";
open(IN,$filename)||die __LINE__ ." $!\n";
while(<IN>){
print;
}
close(IN);
実行結果は下記です。
7 No such file or directory
6 持出しリスト.txt
何度も済みません。openの所を下記の様に変更しても同じでした。
use utf8;
binmode STDIN, ":encoding(cp932)";
binmode STDERR, ":encoding(cp932)";
binmode STDOUT, ":encoding(cp932)";
$filename="持出しリスト.txt";
print __LINE__ . " $filename\n";
open(IN,"<:encoding(cp932)",$filename)||die __LINE__ ." $!\n";
while(<IN>){
print;
}
close(IN);
実行結果は、下記です。
7 No such file or directory
6 持出しリスト.txt
初心者の素朴な疑問ですが、WinXPのファイルシステムでutf8を正常に取り扱う事が出来るのでしょうか?XPは記述してある様にcp932だと思いますが…。
use utf8;
use Encode;
binmode STDIN, ":encoding(cp932)";
binmode STDERR, ":encoding(cp932)";
binmode STDOUT, ":encoding(cp932)";
$filename="持出しリスト.txt";
$filename = encode("cp932", $filename);
utf8::decode($filename);
open(IN, '<', '$filename)||die __LINE__ ." $!\n";
while(<IN>){
print;
}
close(IN);

こうかな?古いカンペみて実行環境が無い状態だからすまん。
ろーんち様、有難うございます。
実行の結果は下記のエラーが出ます。
Malformed UTF-8 character (unexpected continuation byte 0x8e, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x9d, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x8f, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x82, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0xb5, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x83, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x8a, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x83, with no preceding start byte) at 04.pl line 6.
Malformed UTF-8 character (unexpected continuation byte 0x83, with no preceding start byte) at 04.pl line 6.
9 No such file or directory
Take-P様、有難うございます。
私のバージョンはActive Perl v5.8.9です。
実行すると、下記のエラーが出ます。
Malformed UTF-8 character (unexpected continuation byte 0x8e, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x9d, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x8f, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x82, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0xb5, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x83, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x8a, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x83, with no preceding start byte) at 04.pl line 8.
Malformed UTF-8 character (unexpected continuation byte 0x83, with no preceding start byte) at 04.pl line 8.
11 No such file or directory
>>9 とか >>10 のスクリプト自体をUTF-8で保存して実行するとどうでしょう?
構文チェックと警告チェックという前に、そもそも

use strict;
use warnings;

を入れていないですよね。
なんの問題もなくできると思いますが・・・。

■データファイル(日本語.txt):文字コード=MS932(SJIS)
あいうえお
12345
アイウエオ
abcde
??表

■プログラム(foo.pl):文字コード=MS932(SJIS)
open(IN, "日本語.txt") || die "error";
while(<IN>){
print $_;
}

■実行結果(MS-DOS)
C:\hoge>perl foo.pl
あいうえお
12345
アイウエオ
abcde
??表

■備考
"日本語.txt"ファイルの中身の文字コードがSJIS系ではなく、
UTF8などの場合は、use Encode でエンコードしてあげれば
問題無いと思いますよ。

ログインすると、残り6件のコメントが見れるよ

mixiユーザー
ログインしてコメントしよう!

Perl 更新情報

Perlのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。

人気コミュニティランキング