I want to add a text on the top of my data.txt file, this code add the text at the end of the file. how I can modify this code to write the text on the top of my data.txt f
Appending to the top is normally called prepending.
open(M,"<","data.txt");
@m = ;
close(M);
open(M,">","data.txt");
print M "foo\n";
print M @m;
close(M);
Alternately open data.txt- for writing and then move data.txt- to data.txt after the close, which has the benefit of being atomic so interruptions cannot leave the data.txt file truncated.