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

well im still learning to use perl and i wanted to develop a small cgi that could accomplish many tasks all in one, as an excercise. using code samples and making them workable, code snippets, and alot of my own i built the following cgi < The CGI >. its to be a base framework for me to add alot more features to it. however i cant seem to find what i did wrong, might be something small but i dont notice it, when run through perl itself it says everything is fine. but when accessed through a web server it returns param on line 78 is undefined, which is in a subroutine ... and the reason why its undefined is cause that subs job is to define them, its like its reading into the sub befor allowing it to do its function ... would appreciate any help that people could provide me with to make it function properly. ## UPDATE ## used strict and warnings # output : they both returned on variables at the head of the application that were declared for later definition in the code ... reporting that essentially they hadnt been called anywhere. But they are later in the scripting, predeclaration of variables i noticed isnt uncommon in perl ... if i predeclare that $my_shoes = 'your shoes'; print $my_shoes; itll do its function ... so predeclaring variables for later use in the script isnt a problem. actually this was all working as i was using scripting to parse the data on my own without CGI.PM, then i decided to port the script over to it , those variables worked fine under the old scripting , but the main problem is it references only one error type in the script and that is ....
if ($USE_CGI_MODULE == 1) { use CGI; $\ = "\n"; $req = new CGI; print $req->header; &read_fields; } else { # cgi process variables here if CGI.PM is not available } sub read_fields { my(%fields); foreach $f ($req->param) { $name = &clean_name($f); $fields{$f}{name} = $name; $value = &clean_value($f); $fields{$f}{value} = $value; } return(%fields); } sub clean_name { local($f) = shift; $f =~ s/^F\d+_//; $f =~ s/_/ /g; return($f); } sub clean_value { local($f) = shift; local(@val, $val); @val = $req->param($f); $#val-- unless $val[-1] =~/\S/; $val = join(" - ", @val); }
now its error exists under the subroutine read_fields. foreach $f ($req->param) returns that param is undefined. but if you look its defining value comes from the submitted form, so it will be undefined til a form is submitted ... why is it reading into the subroutine in that fashion when it has the data to define that section. As for CGI:Carp qw/fatalsToBrowser/; i used it to see what it would do but ... i dont want to use something of that nature, im wanting the script to return its own error warnings. at present it is simplistic, but wanting to develop it further. I will however keep that in mind when testing CGI scripts, because it does report some nice things. On my script it reported only things that were predeclared and not set anywhere,not anything serious. So thanks to suggesting it. strict and warnings On my script it reported only things that were predeclared and not set anywhere. But did return that param was undefined so on final take from those 3 : predeclaration on variables later defined in the script is ok this even being a direct declaration to reading the submitted form to defining param, its returning undefined in sub return_fields. well like i said if the form is submitted it has the defined values so why is it reading into the sub and undefining it, when it has the data to define it being sent. same error only that one remains. im stumped why the data to be defined is getting undefined, or is it that its reading the sub and finding its undefined and not even bothering with the submitted data that will define it. will look into it more over the week and try some things ... if not ill revert back to my own cgi processing subs that worked before.

Replies are listed 'Best First'.
Re: needing help with a cgi form processor
by rhesa (Vicar) on Feb 27, 2006 at 02:07 UTC
    Eew. Your link is wrong, it's http://fmem.crghosting.com/fp_mailer.txt.

    Your first assignment is to add these lines near the top of your script:

    use strict; use warnings; use CGI::Carp qw/fatalsToBrowser/;
    This will show you a lot of problems with your code. Once you've eliminated all warnings and errors pointed out by warnings and strict, I'd be happy to answer questions.
Re: needing help with a cgi form processor
by McDarren (Abbot) on Feb 27, 2006 at 02:10 UTC
    Hi darkreign and welcome to the monastery :)

    Firstly, that link you gave is broken. If you have some existing code, you can post it here. Be sure to enclose it in <code> tags.

    Also, ensure that you are using the CGI module (if you are not already). This will save you having to re-invent many wheels when it comes to CGI web scripting.

    For troubleshooting CGI, you may find CGI::Carp useful - add use CGI::Carp qw(fatalsToBrowser); to the top of your script, and this will send any fatal error messages to your browser.

    Hope this helps,
    Darren :)