new_2_perl has asked for the wisdom of the Perl Monks concerning the following question:

I simply want a cgi script that will take form variables from an html page and print the specifc varaibles I want into a file. Then print other variables from the form page into another file and so on. The things I'm trying are not working HELP!! How do I open a files and print the variables I want into them?

Replies are listed 'Best First'.
Re: Writing variables to Files
by Beatnik (Parson) on Jun 05, 2001 at 21:51 UTC
    /me points to Web form -- File

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Writing variables to Files
by nysus (Parson) on Jun 05, 2001 at 22:01 UTC
Re: Writing variables to Files
by tachyon (Chancellor) on Jun 05, 2001 at 23:26 UTC

    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; }
      Thanks this helps a lot. I'm getting errors still and I found that it's because I don't have the CGI.pm module. I'm using telnet to work on my code in a UNIX shell where the cgi-bin is. Is there a way to get the cgi.pm module and throw it in the UNIX shell bin so I can use it?
        ok I go the CGI.pm module working thanks