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

Okay, I am kinda stuck. I am now experimenting with CGI on Activestate Perl in Windows and have got an Apache server. I am trying to use the info entered in the comments box so that for every word entered I can take this as a variable to use in a separate program. Is this possible? Thanks for any kind of help.

My HTML Page, guestbook.html:
<html><head> <title>Guestbook</title> </head> <body> <form action="/cgi-bin/guestbook.pl" method="get"> <table> <tr><td>Comments</td><td> <TEXTAREA name="comments" rows="10" cols="32"></TEXTAREA></td></tr> </table><br><br> <input type="submit" value="Add Entry"> </form> </body> </html>
My Program, guestbook.pl:
#!/usr/local/bin/perl my $query_string = ""; #Get the input if ($ENV{REQUEST_METHOD} eq 'POST') { read(STDIN, $query_string, $ENV{CONTENT_LENGTH}); } else { $query_string = $ENV{QUERY_STRING}; } ##### We will remove this print "Content-Type: text/html\n\n"; print "Query String is \n<br> $query_string"; ##### We will remove this # Split the name-value pairs @pairs = split(/&/, $query_string); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); #Converting Hex to English. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $FORM{$name} = $value; } @thanks = split (/ /, $FORM{'comments'}); foreach $thank (@thanks){ $FORM{$thank} = $thank; } #Give output print <<END; Content-Type: text/html\n\n <html><head> <title>Guest book Result</title> <body> <h1 align="center">Guest book Results</h1> <br> Comments : $FORM{'comments'} <br> Words: $FORM{$thank} </body> </html> END

Replies are listed 'Best First'.
Re: Taking Variable from Form Entry Using CGI
by shmem (Chancellor) on Jul 02, 2006 at 14:38 UTC
    Are you not using CGI because you want to know how's the guts of all that stuff?

    Then you should dump all your environment variables and what you read from STDIN into some file and examine it.

    As for POST, I doubt that this works:

    # Split the name-value pairs @pairs = split(/&/, $query_string);

    Otherwise, just use CGI and have that module give you the content of the textarea:

    #!/usr/local/bin/perl use CGI; use strict; my $q = CGI->new; print $q->header; my $text = $q->param('comments'); my $words = scalar(split (/ /, $text)); #Give output print <<END; <html><head> <title>Guest book Result</title> <body> <h1 align="center">Guest book Results</h1> <br> Comments : $text <br> Words: $words </body> </html> END
    Cleaner code, less hassle.

    cheers,
    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks for the reply. Unfortunately, I get the following error:
      Can't locate HTML/TokeParser/Simple.pm in @INC (@INC contains: C:/Prog +ram Files/xampp/perl/site/lib/ C:/Program Files/xampp/perl/lib C:/Pro +gram Files/xampp/perl/site/lib . C:/Program Files/xampp/apache) at (e +val 76) line 19. BEGIN failed--compilation aborted at (eval 76) line +19. ,
      But, when I go to ppm in activestate it says I have installed this module. Any suggestions?
        The CGI.pm included with the perl core doesn't use HTML::TokeParser::Simple (at my unix box, that is - dunno of windows). It must be pulled in somewhere else. Use the search facility of the windows explorer to identify that file. If you just happen to use CGI, check the location of CGI like so
        print STDERR $INC{'CGI'}."\n";
        anywhere in your cgi file, and check the apache error_log. Many modules have a Package::whatever::CGI, but you should be using the core CGI.pm, not Package/whatever/CGI.pm.

        HTH,
        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        At the command prompt type the following:

        perl -MCGI -e "print $CGI::VERSION;"

        ...and then tell us what output you get. Do you still get that HTML::TokeParser error? If not, then the error has nothing to do with your use of CGI. If you do, I'm perplexed. ;)


        Dave

Re: Taking Variable from Form Entry Using CGI
by Ovid (Cardinal) on Jul 02, 2006 at 21:34 UTC

    Hi there. No offense, but your code is broken. You should be using the CGI.pm module. I detail the reasons at lesson 2 of my CGI course. As for your comments about HTML::TokeParser::Simple, since I'm the author of that module, I can guarantee you that this module is not required by CGI.pm, so if running that script results in a "Can't locate HTML::TokeParser::Simple in @INC" error, then you have something seriously wrong with your system. Try running the script from the command line with this:

        perl -e myscript.pl

    Since you're not passing in any parameters, it probably won't work, but if you don't see the HTML::TokeParser::Simple error, then the error is not being generated by your code, but probably by something in your Web server configuration.

    Cheers,
    Ovid

    New address of my CGI Course.