1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package MyPackage; use strict; use Exporter qw( import ); our @EXPORT_OK = qw( print_columns ); sub char_width { ... } my $cache = []; sub print_columns { my ( $str ) = @_; my $width = 0; for my $i ( 0 .. ( length( $str ) - 1 ) ) { my $c = ord substr $str, $i, 1; if ( ! defined $cache->[$c] ) { $cache->[$c] = char_width( $c ) } $width = $width + $cache->[$c]; } return $width; } 1;