in reply to Counting Characters

Use length. Specifically, use length($summary).

In context:

$text = '.'x400; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 400 $text = '.'x500; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 500 $text = '.'x600; $summary = substr($text, 0, 500); print(length($summary), "\n"); # 500

Replies are listed 'Best First'.
Re^2: Counting Characters
by xdg (Monsignor) on Mar 02, 2006 at 21:25 UTC

    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.