sub wrap {
# intelligent wrap
my ($l, $msg) = @_; # are passed parameters, length and original text.
my ($temp, @newmsg, $l2); # working vars.
return unless (defined $msg); # return if original $msg empty
$temp = $msg;
while (length($temp) > $l) # while length of working copy of $msg is
# longer than desired line length
{
$l2 = $l;
$l2-- while (substr($temp,$l2-1,1) ne " "); # shorten line until the end is a space
push @newmsg, substr($temp,0,$l2); # the length found is stored in $l2
$temp = substr($temp,$l2); # put $l2 chars in the array and then
# remove $l2 chars from working copy }
push @newmsg, $temp; # put remaining bit on the array
return join("\r\n", @newmsg); # join array with CRLF separating each line/element
}