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

Hello you've helped me in the past and I am stuck again. Another newbie trying something new. I've created an html form and a cgi application to process the data a user will enter on this form. Being new to gaining input through a cgi form, I am getting the "use of an unititialized value" error on each line of with the print DATA command. From what I can tell, the variables are being passed from the html form to the script just fine. Before I put the CGI into use I ran it from the command line by typing this for example:

./thinner.cgi date=01/01/05 Goal_Weight=247 weight=265

and I get the error on each line that I use as a scalar for the inputed values. Below is the code, its real simple, and works if I run it from the command line using <STDIN> as my variables. Am I passing the variables into the code incorrectly? Please help I've been racking my brain and turning through every book I know to figure this out. Here is the code, the file paths are not the actual ones, but that should not matter.

#!/usr/bin/perl -w use strict; my $dfile = "/stuff/stuff/contest/me/td"; my $wfile = "/stuff/stuff/contest/me /tw"; my $gfile = "/stuff/stuff/contest/me /tg"; my %input; my $input; my $wt=$input{'weight'}; my $d=$input{'date'}; my $g=$input{'Goal_Weight'}; open (DATA,">$gfile") or die "File cannot be opened $!\n"; print DATA "$g"; close (DATA); open (DATA,">>$wfile") or die "File cannot be opened $!\n"; print DATA "$wt\n"; close (DATA); open (DATA,">>$dfile") or die "File cannot be opened $!\n"; print DATA "$d\n"; close (DATA);

janitored by ybiC: Balanced <code> tags around codeblock

Replies are listed 'Best First'.
Re: My cgi nonsense.
by Joost (Canon) on Mar 02, 2005 at 16:52 UTC
Re: My cgi nonsense.
by InfiniteLoop (Hermit) on Mar 02, 2005 at 17:07 UTC
    You could try using CGI, it will make things simpler for you.

    Having said that, this is what is causing the error messages:

    my %input; my $wt=$input{'weight'}; my $d=$input{'date'}; my $g=$input{'Goal_Weight'};
    You need to get the input variables (arguments), first, into the hash.
Re: My cgi nonsense.
by RazorbladeBidet (Friar) on Mar 02, 2005 at 16:53 UTC
    The CGI module would probably be your best bet here. It can reduce an inordinate amount of complexity that would be very time-consuming and frustrating to recreate!
    --------------
    It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs
Re: My cgi nonsense.
by manav (Scribe) on Mar 02, 2005 at 17:01 UTC
    If im not wrong, CGI scripts usually get the corresponding form values at their STDIN, not ARGV. But even with the above script, you are not reading @ARG anywhere. Just specifying values as command-line parameters doesnt populate %input. Perl doesnt show that much of magic yet!!

    As others have suggested, you are much better off using the CGI module.

    Manav

      You're somewhat right -- CGI forms can get input from STDIN, but they only get the content that's been sent to them. (you'd normally get this data from a form submission with method='POST') There is also data that's passed in through $ENV{'QUERY_STRING'}, which you'd get if you used a form with method='GET', or if you encoded it manually into a URL, such as:

      http://server/path/script?param1=value1a;param1=value1b;param2=value2

      For older CGI scripts, you'll have to use amperands instead of semicolons to seperate the key/value pairs, but you then have to encode them in the HTML, like so:

      http://server/path/script?param1=value1a&amp;param1=value1b&amp;param2=value2

      To make things even more confusing, you can also pass things in through $ENV{'PATH_INFO'}, by giving a URL with path information past the script, like so

      http://server/path/script/arg1/arg2/arg3

      (You don't have to seperate the arguments with slashes -- I don't think there's a set convention for that one)

      But, it gets worse when you consider that you could declare a form with

      <form action='http://server/path/script/PATH_INFO?QUERY_STRING' method='POST'>

      which would result in three different sources of input. And let's not forget that you could also pass extra input in the HTTP headers.

      So, what's the solution to all this? Well, use CGI or use CGI::Lite would probably be the easiest, and let them do all the work:

      use CGI::Lite; my $cgi = new CGI::Lite; my %input = $cgi->parse_form_data();

      Update : missing a 'my'. Doh.