Q:

Perl, Count characters and break at the end of a word

I'm trying to break on 300 characters, which it does fine but I don’t want it to break in the middle of a word. I would like to find either the end of a word or on the next available whitespace. Any help would be gratefully appreciated.
foreach (@$comp) {
    my $c = $_;
    $c->{reviews} = ($c->{reviews} - 1);
    my $stars;
    if($c->{reviews} == 0) {
        $stars = 0;
    } else {
        $stars = int($c->{reviews_total} / $c->{reviews});
    } 
    $c->{stars} = $stars; 

    if($c->{title}) { 
        $c->{name} = $c->{title}; 
        $c->{event} = "FOO"; 
    }
    $c->{description} =~ s/BREAK//g; 

    my (@desc) = split(//, $c->{description});   

    my $adesc;
    my $rowc = 0;
    my $count = 0;

    while($count < 300 ) {
         $adesc .= @desc[$count];
         $rowc++;
         $count++;
    }

    $c->{description} = $adesc;= '...';
}

A:

Let's see what perldoc perlfaq4 has to say:

How do I reformat a paragraph?
Use Text::Wrap (part of the standard Perl distribution):
use Text::Wrap;
print wrap("\t", '  ', @paragraphs);

The paragraphs you give to Text::Wrap should not contain embedded
  newlines. Text::Wrap doesn't justify the lines (flush-right).

But what if you don't like it?
Visit CPAN

Text::Wrap::Smart
use Text::Wrap::Smart 'wrap_smart';
use feature 'say';

my $text = 'lorem ipsum dolor sit amet' x 500;

my $options = { 
                no_split     => 1,   # Activates 'fuzzy matching'
                max_msg_size => 300,
              };

say for wrap_smart( $text, $options );

Text::Flow::Wrap
Text::Reform
Unicode::LineBreak

A:

How about:
my $str = q!A very long string to be wrapped on whitespace!;
my $len = 15; #for test. In your case, let $len=300
my @l = grep{s/^\s+//;$_ ne ''}split(/(.{1,$len})(?=\s)/, $str);
dump@l;

output:
("A very long", "string to be", "wrapped on", "whitespace")

