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; }

In reply to Re: Writing variables to Files by tachyon
in thread Writing variables to Files by new_2_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.