Welcome to WebmasterWorld Guest from 18.104.22.168 , register , free tools , login , search , pro membership , help , library , announcements , recent posts , open posts Pubcon Platinum Sponsor 2014
I need to ADD to the BEGINNING of a data file fredfletcher Hello, I have done my best in trying to find a way to be able to do the following using a Perl script (.pl) I have a text file (data.txt) that I would like to add some info at the beginning of the file (prepend I believe, not append). From what I have gathered, "append" adds to the "end" of a file. I need to add to the "beginning" of a file after a form submission. Any help would be appreciated, thank you. Here is what I have so far:
use CGI ':standard'; open WRITE, "<data.txt"; # 'name' is what is taken from my submission form $name = param('name'); open(WRITE, ">>./data.txt"); print WRITE "$name\n"; close WRITE; print "Content-type: text/html\n\n"; print "Success!";
there is no native prepend operator/capability/command in perl. but as always with perl - TIMTOWTDI. depending on your requirements for fault tolerance, this simple method may suffice: - write the form data to a temporary file - copy your text file to the temp file - rename the temp file to your original text file name, replacing the original contents with the prepended version rocknbil
If the file is not huge, another way is to open the file, store all lines in a scalar $tmp $tmp = param('name') . "\n" . $tmp; overwrite file. I generally store the original $tmp in a backup first to be (a little bit) safer about it. fredfletcher
Any chance of getting a rewrite (Rocknbil) ?