homepage Welcome to WebmasterWorld Guest from 54.211.7.174
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
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
fredfletcher




msg:4417239
 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!";

 

phranque




msg:4417288
 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

rocknbil




msg:4417471
 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.

fredfletcher




msg:4417564
 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