Now that utf8 is routine, a couple of basic functions seem to be lagging behind a bit -- (s)printf won't work well at maintaining column alignment when you're working with Chinese/Japanese/Korean (CJK) strings, because the Asian characters are twice as wide as the spaces used to pad a "%s" field. Luckily, unicode (like the Asian character sets it replaces) has a set of "fullwidth" (double-wide) ASCII glyphs, so the fix is pretty easy.
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

Replies are listed 'Best First'.
Re: Asian text vs. (s)printf: CJKprintf
by Anonymous Monk on Jan 25, 2010 at 08:36 UTC
    works great, thanks.