http://www.perlmonks.org?node_id=927777


in reply to Trying to determine the output length of a Unicode string

To get the "visual size" (my term, don't know if there's an official one) of a string, you need two pieces of information:

(And that's assuming your input has no control characters such as a newline.)

The first is actually pretty easy:

my @graphemes = $text =~ /\X/g; my $count = () = $text =~ /\X/g;

NFC is definitely not the way to go as it doesn't work for every character-mark combination.

The catch is knowing the width of characters. Some characters are zero-width, and others are double-width. For that, you really a need the help of a module. Unicode::GCString is such a module.

my $size = Unicode::GCString->new($text)->columns();