... foreach $word (@file) { if ((my $new = $word) =~ s/\.txt$//) { rename($word, $new) or die "unable to rename $word to $new: $!\n"; } }