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

Hi. I am trying to post form data to a file, but my code does not appear to be working. At present I only want to know that the information is being saved to a file. Here is the code:
!/usr/bin/perl print <<endHtml; Content-type: text/html\n\n <html><head><title>"Thank You Form</title</head> <body> <h2>Thanks</h2> endHtml if ($ENV{'REQUEST_METHOD'} eq 'POST'){ open (FL, 'feedback.cgi','+>>') || die ("Hello"); read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); print FL "$buffer"; print "<br/>$ENV{CONTENT_LENGTH}"; } print "</body></html>";
----------------------------- The line print FL "$buffer"; does not work

I cannot get the information into the file feedback.cgi

Is there a particular location i need to specify?

Please help.

irvy

Replies are listed 'Best First'.
Re: my code does not work
by ww (Archbishop) on Apr 17, 2010 at 02:52 UTC
    irvy:

    Welcome to the Monastery.... and it is a cheerful welcome because your post is far from the worst-formatted, -phrased, or -titled of even the past few minutes ... so please accept these suggestions as "constructive."

    Formatting?

    Read Markup in the Monastery and Perl Monks Approved HTML tags.

    One (well, ok, "/me") guesses you meant:

    !/usr/bin/perl #looks like you forgot three lines use strict; use warnings; use CGI; # Though not used below, this could make your work easier print <<endHtml; Content-type: text/html\n\n <html><head><title>"Thank You Form</title</head><body> Thanks endHtml if ($ENV{'REQUEST_METHOD'} eq 'POST'){ open (FL, 'feedback.cgi','+>>') || die ("Hello"); read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); print FL "$buffer"; print "$ENV{CONTENT_LENGTH}"; } print "</body></html>";

    As to "The line print FL "$buffer"; does not work I cannot get the information into the file feedback.cgi," you'll get kinder and gentler (and often, "useful") replies if you tell us in somewhat greater detail how it "does not work" -- as, for example "the error message you receive (if any)" and what errors you find in your server's log file (again, if any).

    So, here's another candidate for your reading pleasure.

    IIRC, that will lead you to a note on the subject of titles: loosely, "make your title describe your problem with specificity."

Re: my code does not work
by ikegami (Patriarch) on Apr 17, 2010 at 04:18 UTC
    I agree with ww. Let CGI handle the parsing and use ->param to the value of the fields.
Re: my code does not work
by ig (Vicar) on Apr 18, 2010 at 06:44 UTC

    You might find perl's -c option useful. If you add use strict; and use warnings;, as recommended previously, then check for syntax errors with something like: perl -c script_under_development.cgi, you can find and fix many errors quite easily, without having to look through web server error logs or expose your script to users.

    After you have fixed any errors you find with the above method, then you might try running your script from the command line. In the case of your script, it helps to set the environment variable REQUEST_METHOD. If you are doing this on a *nix system, you might try something like:

    $ REQUEST_METHOD=POST perl script_under_development.cgi

    For me, running your script (as reposted by ww), this gives:

    Content-type: text/html <html><head><title>"Thank You Form</title</head><body> Thanks Unknown open() mode 'feedback.cgi' at test.cgi line 14.

    This illustrates that not all errors are syntax errors or caught by use strict; or use warnings;. Sometimes you have to run your code and check results.

    In this case, you should probably rearrange the arguments to open and I would add its error message to the output from die. Also, as you're not reading from the file, you don't need the + in +>>. Something like the following will probably work better:

    open (FL, '>>', 'feedback.cgi') || die ("Hello: feedback.cgi - $!");

    You might also think about the security implications of accepting input from the browser and writing it to a file with a .cgi extension in a directory containing CGI programs. If you server is configured securely, the web server process will not have permission to write a file in that directory (IMHO). So, you should probably specify some other path in your open statement, and should probably use an extension other than .cgi.