package CJKprintf; require 5.008_000; use strict; use Exporter qw/import/; our @EXPORT = qw/CJKprintf CJKsprintf/; sub CJKprintf { print _normalize_width( @_ ); } sub CJKsprintf { _normalize_width( @_ ); } sub _normalize_width { my ( $format, @args ) = @_; my $string = sprintf( $format, @args ); $string =~ tr/ !-~/\x{3000}\x{ff01}-\x{ff5e}/; return $string; } =head1 NAME CJKprintf =head1 SYNOPSIS use CJKprintf; CJKprintf( "%5d %-8s\n", $number, $chinese_string ); $string = CJKsprintf( "%10s", $korean_string ); =head1 DESCRIPTION The functions "CJKprintf" and "CJKsprintf" are exported by default (and nothing else is exported). These functions can be used as replacements for the standard "printf/sprintf" whenever you need to maintain visual alignment of fixed-width fields when Asian character data is involved. This works by replacing all ASCII characters (space through tilde, \x20 - \x7E) with their "FULLWIDTH" (double-wide) unicode versions, so that these characters have the same display width as the CJK characters. As currently written, it does not "widen" any non-CJK (single-width) characters that lie outside the ASCII range. =cut