Perl:在文件扩展名之前添加文本?

Perl: Adding text before filename extension?

我将如何在扩展名之前将文本附加到文件名?

例如,

open my $in, '<', $my_file or die $!;

open my $out, '>',"$my_file" or die $!;

$my_file 看起来像这样:

/this/is/my/directory/blahblah.txt/ 

我想像这样在输出中附加它:

/this/is/my/directory/blahblah_new.txt
or 
new_blahblah.txt 

希望这是一个好的解释!谢谢。

像这样使用正则表达式替换:

open my $in, '<', $my_file or die "Cannot open $my_file for reading: $!";
( my $out_file = $my_file ) =~ s{([.]txt)$}{_new};
open my $out, '>', $out_file or die "Cannot open $out_file for writing: $!";