NAME
BATsh::MB - Multibyte (CP932/DBCS) script guard for BATsh
VERSION
Version 0.07
SYNOPSIS
use BATsh;
# Explicit encoding
BATsh->run('nihongo.batsh', encoding => 'cp932');
# Or rely on auto-detection (the default):
# a non-UTF-8 script containing bytes >= 0x80 is treated as CP932
BATsh->run('nihongo.batsh');
# Environment-variable override
# set BATSH_ENCODING=cp932
DESCRIPTION
BATsh::MB makes BATsh safe for scripts written in CP932 (Shift_JIS as used on Japanese Windows) and other double-byte character sets whose trail bytes overlap the ASCII range 0x40-0x7E.
Without protection, byte-oriented shell parsing tears such characters apart: the trail byte 0x5C is mistaken for a backslash escape or path separator, 0x7C for a pipeline separator, 0x60 for a backtick, 0x5E for the cmd.exe caret escape, and uc()/lc() corrupt trail bytes in the a-z range. This is the well-known "dame-moji" problem affecting very common characters such as SO (0x835C), HYOU (0x955C), NOH (0x945C), PO (0x837C), CHI (0x8360), and DA (0x835E).
Rather than teaching every scanner about lead/trail bytes, BATsh::MB applies a reversible guard transform on input: each two-byte character whose trail byte falls in the dangerous ASCII range is rewritten to a three-byte form \x01 LEAD (TRAIL+0x80) containing no ASCII bytes. The inverse transform is applied at output boundaries (print, external commands, filesystem calls, %ENV export). Between the two, all of BATsh's byte-oriented parsing is automatically DBCS-safe.
FUNCTIONS
- set_encoding(ENC)
-
Select the encoding: cp932 (sjis), gbk, uhc, big5, utf8, none, auto.
- detect(BYTES)
-
Return 'none', 'utf8', or 'cp932' for a chunk of script source.
- enc(STR) / dec(STR)
-
Apply / remove the guard transform. Identity while inactive.
- mb_length(STR) / mb_substr(STR, OFF, LEN)
-
Character-based length and substring on guarded strings. These give
${#VAR},${VAR:N:L}, and%VAR:~n,m%character semantics for DBCS text (byte semantics, unchanged, while the guard is inactive).
SUPPORTED PERL VERSIONS
Perl 5.00503 or later.
AUTHOR
INABA Hitoshi <ina.cpan@gmail.com>
LICENSE
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.