my $str = "This is my program";
my @words = split ' ', $str;
####
my @chunks = split /(\s+)/, $str;
####
shift @chunks if $chunks[0] eq ''; # and then
my $s = $chunks[0] =~ /\s/ ? 1 : 0;
my $e = $chunks[-1] =~ /\s/ ? -2 : -1;
####
my ($s,$e)=map $_ + ($chunks[$_] =~ /\s/ ?
($_ >=0 ? 1 : -1 ) : 0) => 0, -1;
####
map {s/[^a-z ]//gi} @words;
####
@words = map { (my $s=$_) =~ s/[^a-z ]//gi; $s } @words;
####
y/\0//d for @chunks; # or @chunks[$e,$s];
####
for ([split /(\s+)/, $str]) {
shift @$_ if $_->[0] eq '';
my ($s,$e)= do {
my @c=@$_;
map $_ + ($c[$_] =~ /\s/ ?
($_ >=0 ? 1 : -1 ) : 0) => 0, -1;
};
$_->[$s] ^= $_->[$e];
$_->[$e] ^= $_->[$s];
$_->[$s] ^= $_->[$e];
y/\0//d for @$_;
local $"='';
print "@$_";
}