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

I've been trying to write a basic contact form but the data isn't getting to the Perl variable I'm trying to use. All I get is an empty string. I'm fairly new to Perl and have been struggling with this for some time now - I don't understand why the code isn't working I tried several different examples I found on the web. It could be my html that's causing the problem? Or could it be the Perl is wrong? Here is the html for the form:
<form method="post" action="scgi-bin/formhandler.pl" enctype="text/pla +in"> <p id="invite">Leave Us a Message</p> <div> <label for="name">Name:</label> <input type="text" id="name" /> </div> <div> <label for="mail">E-mail:</label> <input type="email" id="mail" /> </div> <div> <label for="message">Message:</label> <textarea id="message"></textarea> </div> <div class="button"> <button type="submit">Send Message</button> </div> </form>
and the perl is:
read (STDIN, $in, $ENV{'CONTENT_LENGTH'}); @in = split(/&/, $in); foreach $i (0 .. $#in) { $in[$i] =~ s/\*/ /g; $in[$i] =~ s/%(..)/pack("c", hex($1))/ge; ($key, $val) = split(/=/, $in[$i],2); $in{key} .= '\0' if (defined($in{$key})); $in{key} .= $val; }
I checked and $in is getting no data, it's just an empty string - why? Utterly confused!

Replies are listed 'Best First'.
Re: Not getting form data from stdin
by hippo (Archbishop) on Nov 05, 2014 at 14:21 UTC

    I would not recommend such a low level parsing of the data stream unless you have a very good reason to do so. CGI may not be the most complex of protocols but there's no need to reinvent the wheel yourself given CGI, CGI::Lite, Plack, Dancer2, Mojolicious, etc. Pick one of those and see how much easier it makes things.

    To answer your question, however, you've initialised @in which is an array but then used it like $in{$key} which is a hash. I guess you have not used strict as that should catch this for you.

Re: Not getting form data from stdin
by boftx (Deacon) on Nov 05, 2014 at 23:44 UTC

    Okay, it's been a while since I played with cgi scripts and my memory isn't what it used to be, but since when does a cgi script take input from C<STDIN>? I think OP would benefit from reading the docs for CGI. It might be old, but it's simple.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Not getting form data from stdin
by Laurent_R (Canon) on Nov 05, 2014 at 20:20 UTC
    In addition to what hippo said about @in and %in, if you check $in, you for sure won't get anything because it is yet another variable.
Re: Not getting form data from stdin
by x12345 (Novice) on Nov 05, 2014 at 13:58 UTC
    perl debug is helpful for me to find my problem.Maybe you can also try, step by step debug.

    http://perldoc.perl.org/perldebtut.html

Re: Not getting form data from stdin
by trewornan (Initiate) on Nov 06, 2014 at 08:30 UTC
    OK guys, thanks for the pointers, seems I haven't understood list v scalar context as well as I thought - guess I'll have to re-read that bit in "Learning Perl". Also may have confused myself unnecessarily by using the same variable name in different places. Will look up the CGI module - I'm very unfamiliar with what modules are available. I was also planning on using Mail::Sendmail for the next part of the script.
      Yeah, stop right there, don't write any more code, read TFMail, study it, study the functions it uses ... if you're going to start in the past at least start with something that works :) and these FAQs (a copy of a copy paste)

        Right - so far I've got:

        sub parse { my $q = new CGI; print $q->header; print '<html><head>'; print '<title>Test</title>'; print '</head><body>'; print "These are the parameters I received:<p>"; my( $name, $value ); foreach $name ( $q->param ) { print "$name:\n"; foreach $value ( $q->param( $name ) ) { print " $value\n"; } } print '</p></body></html>'; }

        Unfortunately the output is:

        These are the parameters I received: POSTDATA:

        So really not much progress. However since it seems I've been going about this all wrong maybe just best to leave that and start over from the beginning with TFMail. Feeling somewhat discouraged.

Re: Not getting form data from stdin (webserver)
by Anonymous Monk on Nov 06, 2014 at 01:24 UTC
    What webserver are you using?
      I'm using Apache2 but it's hosted so I can't change the config (or anything else).