#!/usr/bin/perl
######################################################################
# eg/tw/mb_split.pl - ç¨ mb::split å¨å­åéçåå²
#
# æ­¤ä¾å±ç¤ºï¼
#   mb::split('', EXPR) å°å­ä¸²æææ´åå¤ä½åçµå­åï¼
#   mb::split(PATTERN, EXPR) åä»¥ MBCS åéç¬¦åå²ï¼ä¸çµä¸æ
#   å¹éå°å¤ä½åçµå­åå§é¨ç byteã
#
# è CORE çåå¥ï¼
#   CORE split(//, "\x82\xA0") åå³å©åä½åçµ("\x82", "\xA0")ï¼
#   éä½åçµå¹³ååè¢«æè£ãmb::split('', ...) ååå³çºä¸
#   åå­åãmb::split æ¯è½è­¯å¾ "split //" çå·è¡æå°æ
#   ç©ï¼åä¸ç¸å®¹è³ Perl 5.005_03ã
#
# æ³¨ï¼åå§ç¢¼è \xHH è³æç¶­æ US-ASCIIï¼æ¬æªçº
# UTF-8ï¼åè¨»è§£å¨å°åçºç¹é«ä¸­æï¼ã
#
#     perl eg/tw/mb_split.pl
#
######################################################################
use strict;
use vars qw($aiu @byte @char $csv @field);

use FindBin;
use lib "$FindBin::Bin/../../lib";
use mb;
mb::set_script_encoding('sjis');

# Shift_JIS ä¸åå¹³ååï¼ a(\x82\xA0) i(\x82\xA2) u(\x82\xA4)ã
$aiu = "\x82\xA0\x82\xA2\x82\xA4";

# CORE split(//, ...) çå° byteï¼éè£¡æå­åã
@byte = split(//, $aiu);
print "CORE split(//)   : ", scalar(@byte), " pieces (bytes)\n";   # 6

# mb::split('', ...) çå°å­åï¼ä¸åã
@char = mb::split('', $aiu);
print "mb::split('')    : ", scalar(@char), " pieces (chars)\n";   # 3

# ä»¥ MBCS åéç¬¦åå²ãåéç¬¦æ¯å¹³åå a
# (\x82\xA0)ï¼mb::split å°å¶ç¶ä½æ´åå­åå¹éï¼èé byte \x82
# æ \xA0 å¨ä»»ä½ä½ç½®åºç¾æã
#     A a B a C  ->  æ¬ä½ï¼ A, B, C
$csv   = "A\x82\xA0B\x82\xA0C";
@field = mb::split("\x82\xA0", $csv);
print "fields on MBCS   : ", scalar(@field), " (", join(',', @field), ")\n"; # 3 (A,B,C)

# ä»¥ mb::split çæ¸å®èçµ¡è¨å­åæ¸ï¼é¡ä¼¼ chars() è¼å©ï¼ã
{
    local($^W) = undef;
    print "character count  : ", scalar(mb::split('', $aiu)), "\n";   # 3
}

exit 0;
