|Keep It Simple, Stupid|
Re: appending to html at beginningby haukex (Abbot)
|on Feb 03, 2017 at 08:13 UTC||Need Help??|
It appears you have found some very old example code - the code you are using to get the form parameters is outdated, and your script isn't doing use warnings; use strict; at the top (see Use strict and warnings).
If you are just learning Perl / CGI scripting, there are several modern web frameworks nowadays, whose use I'd strongly recommend. For example, a good starting point is Mojolicious::Guides::Tutorial. The "old" CGI module, which is the way a script like the one you've shown would handle form parameters, is no longer recommended for new developments.
Anyway, moving on to your question: Your open is using a mode of '>>', which is "append mode", that's why anything you write to the filehandle $fh is being inserted at the end of the file. If you wanted to insert lines at the top of a file, there are several ways to do so. However, simply inserting lines at the top of the file will break the HTML, so instead you'd need to insert the lines at the correct place instead. One way to do this would be to insert a marker comment at the proper place in the file, like <!-- INSERT HERE -->, and then one relatively easy way to insert lines in the middle of a file is Tie::File (see also the documentation of splice).
Update: Code now inserts current time to make the order of updates to the file more clear.
Hope this helps,