Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: coopster & jatar k & phranque

I need to ADD to the BEGINNING of a data file

   
5:30 am on Feb 14, 2012 (gmt 0)



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:

#!/usr/bin/perl -w

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!";
9:03 am on Feb 14, 2012 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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
4:15 pm on Feb 14, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.
6:59 pm on Feb 14, 2012 (gmt 0)



Any chance of getting a rewrite (Rocknbil) ?
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month