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

Hello, I am working on a homework assignment and I can't seem to get the Perl script to run off the server. This is my first time working with Perl, but I'm fairly comfortable with HTML. I've tried debugging and I've checked it out using OptiPerl, which show it just fine. Also, I've been searching for a tutorial on this for about 8 hours now. If someone knows where I can find a tutorial on this, it'd be much appreciated. I'm supposed to use the POST method and I'm not supposed to use CGI. I'll post both of my files here, so I'm apologizing now for being a bit long. My HTML script:
<html> <head></head> <body> <form action="/cgi-bin/hw4.pl" method="POST"> <br><br><br> <center><p>Please enter the following data:</p> <br> <table border=0><tr><td>First Name:</td> <td> <input type="text" name="first"></tr> <tr><td>Last Name:</td> <td> <input type="text" name="last"></tr> <tr><td>Age:</td> <td> <input type="text" name="age"></tr> <tr><td>Favorite Color:</td> <td> <input type="text" name="color"></tr> <tr> <td colspan=2><center><input type="submit"></center></td></tr> </table></center></form> </body> </html>
My Perl script:
#! /usr/local/bin/perl -w use strict; $buffer = $ENV{'QUERY_STRING'}; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $FORM{$name} = $value; } print "Content-type:text/html\n\n"; print "<html><body>"; print "<center>"; print "<h2>Your Data:</h2>\n"; foreach $name (keys(%FORM)) { print "$name = $FORM{$name}<br>"; } print "</center>"; print "</body></html>";
Update: I don't know if anyone will check this again, but I thought I'd comment to everyone here. Thanks to all of you for your comments. This is my first Perl assignment in this class, and for that matter, ever. I have updated my code to what suggestions that I received and what I realized I didn't do linearly. However, I still get the same errors, where that debugger says that most of my variables require an explicit package name. I'm sorry I assumed everyone would know that is what would happen. I don't really understand what that means, unfortunately. Update Again: After scouring the Perl help files, I found my bane, which actually happened to be the word "my." How come I never learned that I'd have to use that, in class? Seems like something awefully important to gloss over! Is a "my" statement usually necessary when declaring your own variables? Well, thanks again to all of you who have commented! ^_^ <--happy n00b

Replies are listed 'Best First'.
Re: Trouble getting a Perl script to run with an HTML form
by PodMaster (Abbot) on Oct 20, 2003 at 20:39 UTC
    You would do yourself a world of good by use-ing strict and warnings.

    About the PerlMonks FAQ -> How do I post a question effectively?

    Tutorials -> Network/CGI Programming -> CGI (and related) Programming -> Troubleshooting and FAQ's -> CGI Help Guide

    PS -- Just because you can't use CGI; does not mean you can't use it (crack open CGI.pm, copy, paste)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Trouble getting a Perl script to run with an HTML form
by Coruscate (Sexton) on Oct 20, 2003 at 20:29 UTC

    First thing you do is step up to your teacher and tell them that not using CGI (or at least a skimmed down version such as CGI::Simple) is completely idiotic. As is currently, your perl script is trying to access the form data through the GET method. It looks as though you tried to get through POST but that wasn't working. Basically, you need to change read($_, ...) to read read(STDIN, ...), but don't do that. Use CGI::Simple. Anything else is just wrong.

    Update: The fact that this may be the first or second assignment in a class did not occur to me :). If this teacher is anyone decent, then it is probably as hardburn stated: it is most likely so you can see where POST data is being received from. After you have learned this area, I'm sure the teacher will not be against using a well-tested module to do the job securely. A quick thanks to hardburn for the insight.


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

      I could understand if this was an assignment early on in the course's Perl section and the teacher wanted the students to at least learn how params are encoded. Hopefully, after the first few assignments, the teacher will say something along the lines of "anybody who parses CGI params by hand in an assignment from now on will get marked down one letter grade". Though I have an odd feeling that this won't happen.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      :(){ :|:&};:

      Note: All code is untested, unless otherwise stated

Re: Trouble getting a Perl script to run with an HTML form
by freddo411 (Chaplain) on Oct 20, 2003 at 21:13 UTC
    One way to learn is to look at a bit of code someone else has written that does the same thing. You aren't allowed to "use CGI;" but you could go look at the source code in CGI to see how it is done.

    How to find the CGI source code is left as an exersize for the student (its not very hard).

    --Fred

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

      What am I looking for? I don't even know what I did wrong! :\

        I know what you mean. Once someone gets used to something, they can be pretty close to the problem and not explain it well. You might want to check out my CGI course (link is below), but since I don't know what level you're at, it still might be a bit advanced for you. Lesson two explains what you "did wrong". However, I wouldn't say you did anything wrong if you didn't know. After you read lesson 2, you should have a slight idea of what the issue is, but if you're too new to Perl, it might seem a bit daunting.

        Good luck!

        Cheers,
        Ovid

        New address of my CGI Course.

        Nobody else has any clue what you did wrong either, aside from various guesses about changing the param list of read. What isn't working? What does the server say? How are you testing it? How do you know it doesn't work? What are the errors you receive? This is all vital information that you didn't provide.
Re: Trouble getting a Perl script to run with an HTML form
by ChrisR (Hermit) on Oct 21, 2003 at 00:03 UTC
    OK, this is a shot in the dark and has nothing to do with perl but your post doesn't give much information about the problem you are having (except that it appears to be a textbook example of a first cgi script). My guess is that your script simply isn't running "I can't seem to get the Perl script to run off the server". A simple problem like this has bitten me more than once. Be sure to make the perl script executable. A chmod 755 pathandfilename from the command line should suffice (if this is the problem.)

    Other than that, I always run a tail -f error_log in one session and a tail -f access_log in another so that I see exactly what is going on while I debug. You should also try changing the first line in your script to
    #!/usr/local/bin/perl -w
    The next line is a must have
    use strict;
    And while some may feel this is overkill, it will help you solve a lot of problems a lot quicker.
    use warnings; use diagnostics;
Re: Trouble getting a Perl script to run with an HTML form
by zentara (Cardinal) on Oct 21, 2003 at 16:37 UTC
    "I can't seem to get the Perl script to run off the server."

    <form action="/cgi-bin/hw4.pl" method="POST">

    If you want to run it off of the server, maybe you should put the full URL in there. Like "http://myserver.net/cgi-bin/hw4.pl method="POST"

    Or use http://localhost/cgi-bin/hw4.pl if you are testing on a local machine.

      thanks for your attempt, but I know where to reference things. I'm fine with HTML. Also, if you noticed, I updated last night to say that I fixed it all with the help I had.