Open the existing file as input and a new file as output. Read thru the existing file writing to the output file as you're going. Write the new record into the output file where you want it. At the end close the files and then rename the new file to the same name as the old file so that it takes over as the existing file.

A filesystem has no concept of a "line". To insert a line, you either need to rewrite the whole thing, or use something like DB_File and tie the file to an array. Also consider DBD::CSV if you really want a flat file database.. You get some basic functionality.