sub break_word { my $str = shift; # break up $str into separate words, # and return the result ... } # search for all words longer than 30 characters: s/(\S{31,})/ break_word($1) /eg;