perl58日本語

perlと日本語



プログラムコードをUTF8で書く方法

use utf8;↓
binmode STDIN, ":encoding(cp932)";↓
binmode STDOUT, ":encoding(cp932)";
use open IO => ":encoding(cp932)";  

#4行目はファイルopen の文字コード指定 open プラグマで指定します.
# ↑ これがないと、第2引数を開く場合に変換されないので、< を明示する必要あり。
#  perl z.pl sjis.txt ..... NG
# perl z.pl < sjis.txt ... OK
# これがあれば、どちらもOK。

#個別ファイルオープンの文字コード指定
open( F, "<:encoding(cp932)", "filename" ) or die;
print <F>;
close F;

UTF8は1文字3バイト

プログラムコードをSJISで書く方法

use encoding "cp932";
$/ = "\r\n";

 とりあえずお手軽。
 正規表現や文字列定数で、漢字の2バイト目がコントロールコードの場合 (たとえばポの2バイト目が | ) 正常動作しない。この場合文字コードに置換する必要がある。

過去の難しいはなしたち。

perl 5.8以降はunicodeで動いているので、標準で漢字OKになった。
Winの場合は以下の宣言。
use encoding "cp932";
$/ = "\r\n";

Encode標準モジュールを使った文字コード変換

なにはともあれ↓を読むべし。


use Encode;

$string = '変換対象文字列';
$length = Encode::from_to($string, 'shiftjis', 'euc-jp');


すると、$stringが直接書き換わる。

UTF8ファイルを開く

use utf8;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";

while(<>){
  print "¥t", $_ if /あ/;
}

print "--- open utf.txt ---¥n";
open(FH, "<:utf8", "utf.txt" ) or die "Cannot open file¥n";
while(<FH>){
  print;
}
close FH;

print "--- open sj.txt ---¥n";
open(FH, "<:encoding(shiftjis)", "sj.txt" ) or die "Cannot open file¥n";
while(<FH>){
  print;
}
close FH;
      

内部文字列をUTFにする場合(コード自体をUTF8にする)

use utf8;
use Encode;
use Encode::Guess qw/euc-jp shiftjis 7bit-jis/;

my $original_string = '変換される文字列';
my $internal_string = decode('Guess', $original_string);
(文字列処理とか)
my $shiftjis_string = encode('shiftjis', $internal_string);
(printとかファイル出力とか)

$shiftjis = '変換対象文字列';
$internal = decode('shiftjis', $shiftjis);
$euc = encode('euc-jp', $internal);
print $euc;

意味:各種文字コードから内部コードへ … $string = decode('文字コード', $string);
内部コードから各種文字コードへ … $string = encode('文字コード', $string);
各種文字コードから別の文字コードへ … decodeしてからencode
文字コードを自動判別する … 文字コードに’Guess’を指定する(use Encode::Guessしてあることが条件)


元の文字列が不明の場合
$string = (謎の文字列);
$string = decode('Guess', $string) unless (Encode::is_utf8($string));
$string = encode('euc-jp', $string);
print $string;


指定できる日本語文字コードを調べる … Encode::JPのPerldocを見る

C:> perldoc Encode::JP

NAME
  Encode::JP - Japanese Encodings

SYNOPSIS
      use Encode qw/encode decode/; 
       $euc_jp = encode("euc-jp", $utf8);   # loads Encode::JP implicitly
       $utf8   = decode("euc-jp", $euc_jp); # ditto

ABSTRACT
  This module implements Japanese charset encodings. Encodings supported
   are as follows.

    Canonical   Alias             Description
     --------------------------------------------------------------------
     euc-jp      /\beuc.*jp$/i     EUC (Extended Unix Character)
                 /\bjp.*euc/i   
                 /\bujis$/i
     shiftjis    /\bshift.*jis$/i  Shift JIS (aka MS Kanji)
                 /\bsjis$/i
     7bit-jis    /\bjis$/i         7bit JIS
     iso-2022-jp                   ISO-2022-JP                  [RFC1468]
                                   = 7bit JIS with all Halfwidth Kana 
                                     converted to Fullwidth
     iso-2022-jp-1                 ISO-2022-JP-1                [RFC2237]
                                   = ISO-2022-JP with JIS X 0212-1990
                                     support.  See below
     MacJapanese                   Shift JIS + Apple vendor mappings
     cp932       /\bwindows-31j$/i Code Page 932
                                   = Shift JIS + MS/IBM vendor mappings
     jis0201-raw                   JIS0201, raw format
     jis0208-raw                   JIS0201, raw format
     jis0212-raw                   JIS0201, raw format
     --------------------------------------------------------------------

DESCRIPTION
  To find out how to use this module in detail, see Encode.

Note on ISO-2022-JP(-1)?
  ISO-2022-JP-1 (RFC2237) is a superset of ISO-2022-JP (RFC1468) which
   adds support for JIS X 0212-1990. That means you can use the same code
   to decode to utf8 but not vice versa.

    $utf8 = decode('iso-2022-jp-1', $stream);

  and

    $utf8 = decode('iso-2022-jp',   $stream);

  yield the same result but

    $with_0212 = encode('iso-2022-jp-1', $utf8);

  is now different from

    $without_0212 = encode('iso-2022-jp', $utf8 );

  In the latter case, characters that map to 0212 are first converted to
   U+3013 (0xA2AE in EUC-JP; a white square also known as 'Tofu' or 'geta
   mark') then fed to the decoding engine. U+FFFD is not used, in order to
   preserve text layout as much as possible.

BUGS
  The ASCII region (0x00-0x7f) is preserved for all encodings, even though
   this conflicts with mappings by the Unicode Consortium. See

  <http://www.debian.or.jp/~kubota/unicode-symbols.html.en>

  to find out why it is implemented that way.

SEE ALSO
  Encode

  • 最終更新:2011-11-03 18:40:39

このWIKIを編集するにはパスワード入力が必要です

認証パスワード