Hello,

I have done my best in trying to find a way to be able to do the following...

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!";

You will need to overwrite all of the data in the existing file with the new data + old data you want in it.

You can do this by first creating a new file with the new data then append the old data to it, delete the old file, rename the new file to the old name.

or

Read all of the contents from the old file into RAM, then overwrite the file with the new data + the old data.

Any chance to get a rewrite of a script?

Any chance to get a rewrite of a script?

You already know how to do the CGI which I think must be logically independent of how to prepend a string to a file.

#!/usr/bin/perl;
use strict; 
use warnings; 

my $filename = 'data.txt';
#If file doesn't exist, create it
if (-e $filename){
    #File exists, so don't create new file
}
else{
    open my $fh, '>', $filename or die "Failed to create $filename: $!";
}

my $string2prepend = 'Tom';
prepend2file($string2prepend, $filename);

sub prepend2file{
    my ($str, $file) = @_;
    my $old_data = slurp_file($file);
    open my $fh, '>', $filename or die "Couldn't open file: $!";
    print $fh $str, $old_data;
}

sub slurp_file{
    my $filename = shift;
    local $/=undef;
    open my $fh, '<', $filename or die "Couldn't open file: $!";
    my $string = <$fh>;
    return $string;
}

Thanks (d5e5), I'll give this a try :) much appreciated, cheers!

Thanks (d5e5), I'll give this a try :) much appreciated, cheers!

You're welcome. I made one mistake in the third line of the prepend2file subroutine. The open statement should say
open my $fh, '>', $file or die "Couldn't open $file: $!";
to refer to the $file variable declared inside that subroutine.

Thanks, I made the change. I will let you know, I haven't had a chance to test it yet. Having a "whale of a time" with the server I have to work with... they're still using PHP 4 and I've hit a "brick wall" with another script I am trying to get to work in conjunction with the PERL script. Are you well-versed in PHP 4? (d5e5) I'm not a "programmer" but can get around rather well with pieces of code and modifying them to my liking.

Thanks, I made the change. I will let you know, I haven't had a chance to test it yet. Having a "whale of a time" with the server I have to work with... they're still using PHP 4 and I've hit a "brick wall" with another script I am trying to get to work in conjunction with the PERL script. Are you well-versed in PHP 4? (d5e5) I'm not a "programmer" but can get around rather well with pieces of code and modifying them to my liking.

I don't know much PHP. I learned a bit by lurking on the daniweb php forum a couple of years ago but have forgotten most of it since then. You might try asking there.

It's a good place to go, I go there too when I need help with something. I am not the type to ask for something right off the bat, unless I know nothing about it. I do have "some" knowledge in different programming language (PHP, CGI, Pascal, C+) but am not a pro. I do my best first, to try and solve something, but if I am totally stumped, I will then ask for a helping hand.

UPDATE on the script you given me... it's not good. Well, it's good yes, it works and all, it's just that the server it was to work on, is a (pardon the language), a piece of outdated crap.

I get a "connection was reset" in my Web browser when it executes. It makes the changes, but that "reset connection" bit.... a definite sign of an outdated/crappy setup on the server. Definitely "NOT" your fault.

I have never in my 15+ years of working on servers, dealt with a (weird running) server as the one I have to work on now. I managed to find and rework(ed) a "search and replace" PHP script. Had it been PHP 5 on it, all would have been hunkie dory... but not the case; PHP 4... groan. Oh well... them's the breaks I guess LOL! Cheers~ thanks again (d5e5), very much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.