ファイル内の行を置換したいのですが、置換後に別の行を追加したいと考えています。ご覧のとおり、ファイルを 2 回開いて閉じる必要があります。ファイルを 1 回だけ開くだけで実行できますか? ありがとう
  use strict;
  use warnings;
   open(FILE,"tmp1.txt") || die "Can't open file: $!";
   undef $/;
   my $file = <FILE>;
   my @lines = <FILE>;
   my @newlines;
   for each(@lines) {
     $_ =~ s/hello/hi/g;
     push(@newlines,$_);
    }
   close(FILE);
   open(FILE, "> tmp1.txt ") || die "File not found";
   print FILE @newlines;
  close(FILE);
  open(FILE,"tmp1.txt") || die "Can't open file: $!";
  undef $/;
  my $file = <FILE>;
  my @lines = <FILE>;
  my $first_line = "hi";
  my $second_line = "sun";
  my $insert = "good morning";
  $file =~ s/\Q$first_line\E\n\Q$second_line\E/$first_line\n$insert\n$second_line/;
  open(OUTPUT,"> tmp3.txt") || die "Can't open file: $!";
  print OUTPUT $file;
  close(OUTPUT);