|Think about Loose Coupling|
Re: Script fails to insert text and appends it towards the end of file.by haukex (Bishop)
|on Nov 21, 2020 at 13:26 UTC||Need Help??|
However, my script is simply appending the text to the output file (after counting 11 lines from the end).
I initially couldn't reproduce this, but managed when running the script on Windows and the files having LF (not CRLF) line endings. Using tie @records, 'Tie::File', "output.fem", recsep=>"\n"; makes it work again. (See also the module's _default_recsep.)
However, I would not implement this script this way, since it requires you to know the correct place to insert the lines beforehand, and it requires there to be enough space for the lines to be inserted, otherwise data will be overwritten. The following uses a state machine pattern, and has the advantages that it does a bit of verification on the input file (it checks that there's only one section of NODE lines) and that it doesn't care how many lines both the input files have. Also, it won't have the above issue, because normal opens on Windows will normally do the right thing with line endings. Plus Tie::File adds significant overhead to the program.
(Update: Shameless plug: If you want to overwrite the input file, then see my module File::Replace for an easier/safer way to do so.)