有没有更短的方法来剪切这个中文文本,使其适合$print_length
宽度打印列?
#!/usr/bin/env perl
use warnings;
use 5.10.1;
use utf8;
binmode STDOUT, ':utf8';
use Unicode::GCString;
my $print_length = 15;
my $string1 = 'abcdefghijklmnopqrstuvwxyz';
say substr( $string1, 0, $print_length );
my $string2 = '大佛頂如來密因修證了義諸菩薩萬行首楞嚴經'; # don't know what that means
say fit_for_column( $string2 );
sub fit_for_column {
my ( $string ) = @_;
my $gcs = Unicode::GCString->new( $string );
my $pcw = $gcs->columns();
while ( $pcw > $print_length ) {
$string =~ s/\X\z//;
$gcs = Unicode::GCString->new( $string );
$pcw = $gcs->columns();
}
return $string;
}