in reply to append middle of File

Perhaps it is more clear if we give you an algorithm rather than a script:
  1. Open your output file (say fileC)
  2. Open your fileB
  3. Read through fileB file line by line, finishing if all lines have been read.
  4. Have you reached the place of insertion? If no, print to your output file, the line just read and go back to 3. If yes, open fileA and print all of it to your output file.
A rather minimal version of this algorithm could be:
use strict; use warnings; open my $FILEC, '>', 'fileC'; open my $FILEB, '<', 'fileB'; open my $FILEA, '<', 'fileA'; while (my $line = <$FILEB>) { do {local $_; print $FILEC $_ while (<$FILEA>)} if $line =~ m/INSE +RT BEFORE HERE/; print $FILEC $line; }
By using a while loop both on the original file and the file to be inserted you never have more than a few lines in memory, so it will work on any size of file.

Of course you should add some tests to see whether opening of the files and writing to them succeeds.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James