Subject: | scalar virtual method 'length' returns a different value |
I found the scalar virtual method 'length' returns
a different value for an unicode string when using
Template::Stash or Template::Stash::XS.
# -- sample
use strict;
use utf8;
use Template;
use Template::Stash;
use Template::Stash::XS;
my $conf_tmpl = { STASH => Template::Stash->new() };
my $tmpl = Template->new( $conf_tmpl );
my $ret = '';
my $body =<<BODY;
length -- [% str.length %]
split_size -- [% str.split('').size %]
BODY
$tmpl->process( \$body, { str => 'あいうえお' }, \$ret );
print $ret;
# -- end
The length and split_size are both 5.
But if I use Template::Stash::XS, then the length is 15.
So I think that XS's length function returns a byte length.
Here is my patche. Pached Stash::XS returns a same value as
pure-perl Template::Stash.
--- xs/Stash.xs.bak 2009-02-16 11:20:09.000000000 +0900
+++ xs/Stash.xs 2009-02-16 11:41:15.000000000 +0900
@@ -1125,8 +1125,16 @@
/* scalar.length */
static SV *scalar_dot_length(pTHX_ SV *sv, AV *args) {
STRLEN length;
+#if PERL_VERSION >= 8 /* Perl 5.8 and later */
+ if ( SvUTF8(sv) ) {
+ length = sv_len_utf8( sv );
+ }
+ else {
+ length = sv_len( sv );
+ }
+# else
SvPV(sv, length);
-
+# endif
return sv_2mortal(newSViv((IV) length));
}
///////
Regards,