如何 delete/insert 在另一个文件中的标志之间使用 Perl 整个文件

How to delete/insert an entire file using Perl between flags in another file

y.txt -> 要编辑的文件 x.txt -> y.txt

中需要复制写入的文件

y.txt 有

c...
w...
// search for this comment and copy the entire x.txt file here 
// search end comment when you need to delete lines between these comments
g...

我如何在 Perl 中执行此操作?另外,如何搜索这两个评论并删除它们之间的行?

所以y.txt插入后看起来像这样

c...
w...
// search for this comment and copy the entire x.txt file here 
Entire x.txt file here
// search end comment when you need to delete lines between these comments
g...

要插入的文件总共约20行。我也创建了这个文件。

这是一种方法:

use strict;
use warnings;

my $xfn = 'x.txt';
open ( my $fh, '<', $xfn ) or die "Could not open file '$xfn': $!";
my $xstr = my $str = do { local $/; <$fh> };
close $fh;
@ARGV = qw(y.txt);
$^I = '.bak';  # Backup file extension
my $delete = 0;
while(<<>>) {
   if (m{// search end comment}) {
      $delete = 0;
   }
   next if $delete;
   print;
   if (m{// search for this comment}) {
      $delete = 1;
      print $xstr;
   }
}