This code will get you started. Not tested but I expect it will work. There are three bits - the HTML form, the data processing script and a script which prints to the browser.
If you don't understand what is happening a quick search of the relevant keyword will reward you with a far better explanation than I am likely to be able to tender.
When dealing with files the '>' and '<' chars have special significance. '<' means open for reading. '>' means open for writing AND overwrite the existing file (if any). You will note we use '>>' which means APPEND the data to the end of the file without overwriting the existing data.
Hope this helps
tachyon
HTML FORM
<html>
<head>
<title>Form</title>
<head>
<body>
<h1>Mailing list</h1>
<p>Please fill out this form to subscribe</p>
<FORM ACTION="/cgi-bin/myscript.cgi" METHOD="post">
<p>Name: <INPUT TYPE="text" NAME="name"></p>
<P>Email: <INPUT TYPE="text" NAME="email"></p>
<p><INPUT TYPE="submit" VALUE="Subscribe me!"</p>
</FORM>
</body>
</html>
CGI script. I have assumed it is called myscript.cgi, and lives in the cgi-bin directory when I call it in the form. It needs execute permission - chmod 755
#!/usr/bin/perl -wT
# myscript.cgi
# always use strict
use strict;
# use CGI.pm to process CGI input
use CGI;
my $q = CGI->new;
# use Fcntl to allow file locking
use Fcntl qw(:DEFAULT :flock);
# configuration variables
my $path = '/usr/your/home/logs';
my $name_file = 'names.txt';
my $email_file = 'emails.txt';
# assign form data to variables
my $name = $q->param('name');
my $email = $q->param('email');
# print our data to a file
print_to_file("$path/$name_file", $name);
print_to_file("$path/$email_file", $email);
# print data to file
print_to_file {
my $file = shift;
my $data = shift;
open (FILE, ">>$file") ||
die "Unable to open $file for appending: $!";
flock (FILE, LOCK_EX) ||
die "Can't get an exclusive lock on $file: $!";
print FILE "$data\n";
close FILE;
}
CGI script to output to browser
#!/usr/bin/perl -wT
# myprint.cgi
# always use strict
use strict;
# configuration variables
my $path = '/usr/your/home/logs';
my $name_file = 'names.txt';
# print the header line, and html stuff
print <<HTML;
Content-type: text/html
<html>
<head>
<title>Names</title>
<head>
<body>
<h1>Names list</h1>
HTML
# get data from a file into an array and print it
my @file_data = get_file_data("$path/$name_file");
print "<p>Name: $_\n</p>" for @file_data;
# finish neatly
print"</body>\n</html>\n"
# get data from a file
get_file_data {
my $file = shift;
open (FILE, "<$file") ||
die "Unable to read from $file: $!";
my @data = <FILE>;
close FILE;
return @data;
}
|