Link to home
Start Free TrialLog in
Avatar of tanc02
tanc02

asked on

help ! write and read to file

let say I have 'enter.html' and 'view.html'.

In enter.html, there is a textfield for name and two button for submit and clear. User should enter his or her name in that textfield, the press submit. Then his or her name will be stored in the text file called 'name.txt'

In view.html, user can see how many name already in the name.txt and print those name out on the screen one by one.


I would like to use perl to write this program but I don't have any clues and I am new to perl , so I am asking experts, please give me a complete code and explanation of the code. Thank you !
Avatar of squimph
squimph

#!/usr/bin/perl

use CGI qw/:standard/; # Use CGI.pm's standard functions

$namefile = '/tmp/name.txt'; # Name and location of file to store the names in

print header; # Print the 'content-type' header required by all CGI scripts.

# Decide whether to show the 'enter name' form, the 'list of names' form,
# or store a name in the name file:

# If parameter 'name' exists, this script must have been
# called from the 'enter name' form, so we should stick the name in a file:
if ( param('name') ) {
    store_name();
    show_thanks();
}
elsif ( param('mode') eq 'view' ) {
    show_view_form()
}
# Otherwise, just print out the 'enter name' form:
else {
    show_enter_form()
}

print end_html;
exit;

# Subroutines:

sub store_name {
    # Add the name to the end of the file:
    open NAMEFILE, ">>$namefile" or die; # Open the file in append mode
    print NAMEFILE param('name'),"\n"; # Print name and a newline to file
    close NAMEFILE; # close the file
}

sub show_view_form {
    print h1('List of names'),p; # Print a nice page heading
    # Print the contents of the name file
    open NAMEFILE, "<$namefile" or die; # Open in read-only mode
    while ( $linein = <NAMEFILE> ) {
        chomp  $linein; # remove trailing newline if there is one
        print "Name = $linein", p, "\n";    # Print the line to the web browser
    }
    close NAMEFILE;
}

sub show_enter_form {
    print h1('Name entry form');
    print start_form;
    print "Enter name: ", textfield('name'),p;
    print submit;
    print end_form;
}

sub show_thanks {
    # Displays a thank you message
    print h1('Thank you');
    print 'Thank you for submitting your name',p;
    print "<A HREF=$myself?mode=view>View names</A>";
}
Comments for the above script:

- This script does everything using just one CGI script. Store the above in a file in your web server's CGI-BIN called 'readwrite.pl' Run it by going to URL http://your.server.name/cgi-bin/readwrite.pl

- You may need to change the line
    #!/usr/bin/perl
  ...in the script to point to the location of perl on your system (sometimes it's /usr/local/bin/perl)

- The script creates the names.txt file in the /tmp directory but you should change it to a more private directory. Whatever directory you choose must be able to be written to by your web server.

- This script uses CGI.pm, a perl library that comes standard with Perl 5. With CGI.pm you can do everything just with one CGI program... generate the HTML forms, and store and retreive names from the file by running it different ways. (see http://www.genome.wi.mit.edu/ftp/pub/software/WWW/cgi_docs.html for the details about CGI.pm )

- I've put as many comments as I could in the above code... once you read about CGI.pm it hopefully won't need a lot more explanation.
Avatar of tanc02

ASKER

I have cgi-lib.pl but i don't have CGI.pm and I tried to get CGI.pm and I think it is
difficult for me to install and download since I have no idea at all. Could you write this script in perl only ?
It's well worth the trouble to get and install CGI.pm ... if you're going to learn how to write CGI scripts, doing it without CGI.pm is just plain painful. Most CGI scripts you download on the Internet require it.

If you're using a Unix machine, it's easy to get and install. But, if you really insist on a plain jane perl version, reject the above answer so I can post another one.
Avatar of tanc02

ASKER

Thank a lot, I hope I am not giving trouble to you
ASKER CERTIFIED SOLUTION
Avatar of squimph
squimph

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tanc02

ASKER

I got an 500 internet error. Here is what I did :

readname.pl and name.txt store in /home/mcs436-2/web-server/cgi-bin
and I have changed only two lins taht you told me.

$namefile = '/home/mcs436-2/web-server/cgi-bin/name.txt'; # File to store the names in
$myself = '/home/mcs436-2/web-server/cgi-bin/readwrite.pl'; # EDIT THIS TO BE THE URL of this script

What is the problem ?
'500 internal error' usually means there's a syntax error in the script or perl cannot be found. But before we tackle that problem...

- Rename the file to 'readwrite.pl' (it has to be exactly the same name as referred to in the $namefile variable)

- Make readwrite.pl executable... eg. do a 'chmod 755 readwrite.pl'

- Move name.txt to some other directory. Data files should *never* go into CGI-BIN for security reasons. Most people create a '/data' directory for their CGI's data files. Don't forget to modify the $namefile variable to point to the new location. Make sure the file can be written to by the web server.

Okay... now to deal with that '500' error:

- Can you run the script yourself from the command line? eg. go to cgi-bin and type './readwrite.pl'  It should spit out something like this:

 Content-type: text/html
 <HTML><HEAD><TITLE>Name entry</TITLE></HEAD>
 <BODY>
 Invalid request method from form at ./readwrite.pl line 49.

If you can't run the script from the command line, your web server won't be able to run it either.

- If get a message like 'syntax error' when you try to run the script, carefully check the script for errors such as one line wrapped across two, etc. The error message will usually give the line number of the error.

- If you an error like 'No such file or directory' you probably have to edit the first line of the script to point to the location of Perl on your system. 'whereis perl' might help you it. For example, if perl is located in the /usr/local/bin directory on your system, change the first line of the script to read:

   #!/usr/local/bin/perl

- Lastly, if you can run the script from the command line, but the web server still gives a '500' error, look in the web server's error log for clues as to why. Usually error messages will be recorded there.
Avatar of tanc02

ASKER

After I ran it on the command line I saw ( I entered this  'perl readwrite.pl' on the commane line)

Content-type: text/html
 
<HTML><HEAD><TITLE>Name entry</TITLE></HEAD>
<BODY>
Invalid request method from form at readwrite.pl line 49.



and in the error_log file, I have :

[Wed Dec  2 14:48:28 1998] access to /home/mcs436-2/web-server/cgi-bin/readwrite.pl failed for 199.17.35.90, reason: Premature end $ end of script headers

do you have any clue
Looks like your web server is not set up to run perl. Have you ever run any other perl CGI script on your web server before?

But before we get into that... we've now left the original "how do I do this in perl" question and entered a totally new question: how to install a CGI script on your web server.

I've provided two scripts that do what you originally asked (they both work, trust me) but it looks like there's some work you have to do on your web server to get it to run perl CGIs. I can help you with that too, but not for the same measly 200 points (*grin*)

Seems to me you should accept the above answer, since your original question HAS been answered (twice!) then open another one about how to install a perl CGI script and configure your web server. In that new question, it would be helpful to describe which web server you're using (Apache? Microsoft IIS?) and on what platform (Linux? Windows NT?). Then I and others can help you get things set up correctly. Sound fair?
Avatar of tanc02

ASKER

I am taking web adminisration at st. cloud state university.
I am sure my configuration is correct 100% because I have ran so many complicated
perl script like guestbook and others, and I don't have any problem at all.
You tell me your script is correct, then can you tell where is URL that will execute
ose both script, if everything is fine, then I will give you the point.

*p/s : my professor don't know much about perl and any cgi script.
One last time then:

You won't say what type of system you are using, but from the pathnames your using I'm going to assume it's Unix. These hints are only valid for a unix system:

- You ran the script from the command line just fine, but I see you used 'perl readwrite.pl' to run it. Try running it by typing only  ./readwrite.pl  (That's DOT FORWARD-SLASH).

- If you get 'no such file or directory' it means the first line of the script is not pointing to the location of perl on your computer. As mentioned above, make sure the line #!/usr/bin/perl points to the perl executable on your system. If your other perl scripts work, adjust the first line in readwrite.pl to match the location of perl used in the other scripts.

- If you get 'permission denied', the permissions on the script are wrong. Assuming you are on a Unix system, 'chmod 755 readwrite.pl' will set the correct permissions on the file.

If you can't run the program using ./readwrite.pl your web server won't be able to either.

- Try to run the script from your web browser (using something like http://your.web.server/cgi-bin/readwrite.pl but the URL you need to use depends on how you have your web server set up) If you get an error message, the last few lines of your server's error log will give clues as to what's wrong.

All the above are just basic Unix administration tasks and have nothing really to do with Perl or CGI.
Avatar of tanc02

ASKER

I got it already. The problem is on the first line.

I have an extra space in front on
#!/usr/bin/perl


Thank a lot from you.

I have another question, but you don't have to answer this

What should I do if I want to have another field for age.

Remeber, you don't have to answer this, but I will appreciated if you can teach me.

Thank you !