Hey, it's a pleasure to help out.
If you defined the wordboundaries stricter, you could have it count differently.
my $string = "hello, my name is Xasghjda. What\nyes sadddd/( /(\$dd";
my $count = scalar split(/[,. ?!\r\n]+/, $string);
print $count;
would count 9 words. To make sure your users don't mess with your design, you might also look at spaces, i.e. while I stay below the character-count (let's say 80) and the word-count, it might break your design if I just submit 79 x "a" without any spaces or dashes where the browser could break the line.
with
if($string =~ m/[^\- \r\n]{20,}/s)
would match any strings with 20 or more chars that do not contain spaces, dashes or line breaks. what you do with those is up to you, either yell at the user or silently insert a space every X characters:
$string = "dasdddddddddddddddddddddddddddddddddddddddddddddddddddd";
while($string =~ m/[^\- \r\n]{20,}/s)
{
$string =~ s/([^\- \r\n]{19})/$1 /gs
}
print '"' . $string . '"' . "\n";
=>
"dasdddddddddddddddd ddddddddddddddddddd ddddddddddddddddd"