$input = " 1234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890 1234567890123456789012345678901234567890123456789012345678901234567890 "; # do the magic with a snazzy regex $input =~ s[(\S+)] # grab the words into $1 { local $_ = $1; # assign word to $_ if (length $1>30) { # if word to long s/(.{30})/$1 /g; # add spaces every 30 char } $_; # substitute in $_ for word # it may or may not have changed }gex; # global, execute, comments print $input;