for (my $pos = length($text); $pos ; --$pos) { my $char = substr($text, $pos-1, 1); if ($char =~ /\S/ and (int rand 2)) # 0 or 1 { substr($text, $pos-1, 1) = "$opening_tag$char$closing_tag"; } }