I want to add a line to top and bottom of the file. I can do it following way.
open (DATA, \"
I don't really speak Perl, but perhaps this works for some situations:
perl -0777 -pi -e 's/^/MY TEXT TO PREPEND/' myfile.txt
That is, open the file in paragraph mode (one line), and replace the start of that line with your new text, doing an in-place rewrite.
Probably not efficient for many large files.