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

Well since I first got here, my first idea for a script that I've never seen done was a program that took the parameters from a user and spit the out in alphabetical order. Now, I know this probably puts me back down to acolyte for my lack of knowledge here but I can't get this to work. When I run it on AP it runs perfectly, but when I run it on the web I type in the parameters, press "Submit" and get a 500 error which states : "Improper header text1". I don't get that error message! Oh well, heres the script so you can all tell me what I'm so stupid for :-). NOTE: This is just to be basic, nothing great is supposed to go on here.
#!/usr/bin/perl -w use CGI qw(param); use diagnostics; use strict; my $query = new CGI; if ($query->param()) { foreach $key (sort {$a cmp $b} $query->param) { print "<STRONG>$key</STRONG> = "; my @values = $query->param($key); print join(", ",@values),"<BR>\n"; } }else{ print "Content-type:text/html\n\n"; print qq~ <HTml><head><title>Alphabetical Sorter</title></head> <body><h2 align="center">Alphabetical Sorter.</h2> <form method="post"> <input type="text" name="1"><Br> <input type="text" name="2"><Br> <input type="text" name="3"><Br> <input type="text" name="4"><Br> <input type="text" name="5"><Br> <input type="submit" value="submit form"> </form> ~; }

Help, please?
Update: I got this working thanks to chipmunk:-) +++++++++++ Thanks all

Wanna be perl hacker.
Dave AKA damian

Replies are listed 'Best First'.
Re: Sorting Problem
by merlyn (Sage) on Dec 20, 2000 at 08:05 UTC
    Don't forget the space after the colon on content-type. It's much simpler just to use all the shortcuts. Here's your whole program as shortcuts:
    use CGI qw(:all); print header, start_html("alphabetical sorter"), h1("alphabetical sort +er"); if (param) { for my $key (sort param) { print strong($key), " = ", join(", ", param($key)), br; } } else { print start_form; print textfield($_), br for 1..5; print submit, end_form; } print end_html;
    Much, much cleaner.

    -- Randal L. Schwartz, Perl hacker

Re: Sorting Problem
by btrott (Parson) on Dec 20, 2000 at 07:32 UTC
    You need to print out a Content-Type header for your if condition, not just your else. You can/should use the header method in CGI.pm:
    print $query->header;
    That'll print out the HTTP header for you.

    Also, when I just pasted in your code, it didn't compile, because you didn't declare $key, and you used strict. You can use

    foreach my $key
    to fix that.
Re: Sorting Problem
by Fastolfe (Vicar) on Dec 20, 2000 at 09:15 UTC
    (Mainly for the benefit of those that wouldn't know where to start looking for solving this problem):

    This boils down to basic CGI debugging. Whenever you get a server error pointing to a lack of headers, that's your first indication that your script either did not compile correctly (errors in the error logs), or it compiled and executed, but spat out content without correct/any headers. The very first thing you check is to be sure each path through your script ends up giving us HTTP headers first.

    In addition, since you're using CGI.pm, you can run your script on the command line and pass your arguments straight to it. This is an excellent way to test form submissions without the added complexity of a web server. Frequently this helps you identify CGI-related problems.

Re: Sorting Problem
by ichimunki (Priest) on Dec 20, 2000 at 10:15 UTC
    update: this comment was proof i should never post after my bedtime. :)
      But / doesn't need to be escaped...
        And here I've been doing it that way for quite a while, needless to say, I'm switching to an all module approach. Who knows what else I've got screwed up! :)

        I just abuse-tested all sorts of different things in the header and I couldn't get this error in lynx or netscape.