$summary = substr($text, 0, 500); Thanks.
Use [doc://length|length]. Specifically, use length($summary).
In context:
$text = '.'x600; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 500 $text = '.'x500; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 500 $text = '.'x400; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 400
As an addendum -- in case it's not clear for the OP or others -- this works because substr used in the way described takes up to as many characters specified (e.g. 500 in this case). If used on a string shorter than the maximum length, there is no padding added at the end. Thus length works on the summary exactly as one would expect.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
Of course none of these is efficient, but if you really do not want to use the built-in length function, ...
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
or counting nibbles and dividing by two (bit fraught for multi-byte characters though):
use strict; use warnings; my $text = '.'x400; my $summary = substr($text, 0, 500); print length(unpack 'H*', $summary) / 2, "\n";
Prints:
400
perlmonks.org content © perlmonks.org and Anonymous Monk, antirice, CountZero, GrandFather, ikegami, PodMaster, xdg
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03