Hey, for once I'm going to actually put some code on one of these darn things!!
use warnings;
use strict;
my $text = "I'd like a foobar(s)! What about you?";
print "Pretext: $text\n";
my $num_of_chars = 13;
$text =~ s/(.{$num_of_chars}[\w|\(.*\)\w]*).*/$1/;
print $text;
OUTPUT:
Pretext: I'd like a foobar(s)! What about you?
I'd like a foobar(s)
Now, all you'd really need to do is expand on the last part to correctly handle which punctation you want to handle for the last part.
here's my second try if you want word counts
use warnings;
use strict;
my $text = " I'd like a foobar(s)! What about you?";
print "Pretext: $text\n";
my $num_of_words = 6;
my @text;
for (my $i=1; $i<=$num_of_words; $i++) {
$text =~ s/^\s*([\w|\(|\)|'|:|!|\.|\,|;]*\s*)(.*)/$2/;
push (@text,$1);
}
for (@text) { print $_; }
OUTPUT:
Pretext: I'd like a foobar(s)! What about you?
I'd like a foobar(s)! What about
This is just taking the first word, regardless of puctuation, which may cause some grief, and the next space(s), and putting it in an array. A good check to put in there would be that if $text becomes undef to stop looping. Also, if you're reading in from a file line by line, what I'd do is get the first few lines and concatenate them together to form your text to get the data from if you're going over say, 50 or so words.
Please note that I'm nowhere near the level of others here and the code above is laughable by perl monk standards, but it should help you get along. :)
UPDATE: Updated code a bit.