in reply to Re^4: How to remove underscore at the end of the line?
in thread How to remove underscore at the end of the line?

Instead of $prefix =~ s/_$//;, maybe try this:
my $newfile = $prefix . $suffix; $newfile =~ s/_$//; rename "$dir/$file", "$dir/$newfile";