homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

I need to ADD to the BEGINNING of a data file

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


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)

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)

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) ?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved