in reply to Beginning Perl and Forms

Here's the way I would duplicate the functionality that you are trying to achieve:
#!/usr/local/bin/perl -wT use strict; use CGI qw/:standard/; # Geturl.pl # A little Perl script to read, decode and print the names # and values passed to it from an HTML Form thru CGI. # Get the HTML header, ender, define the page title. my $Title = "Get Information From a URL"; print header(), start_html ( -title => $Title ), h1( $Title ), hr(); # This gets the name of all of the form elements my @names = param(); # The param() function takes the name of the parameter and returns its + value foreach my $name ( @names ) { print "Name = $name, Value = " . param( $name ) . "<br>\n"; } print end_html;
Since you are new to Perl, there are quite a few issues that should be covered. If you want to see some of the basics, you can check out my online Web programming course. That should point you in the right direction. Also, you'll want to read about perl security.

Whenever you write a CGI program, you'll hear experienced programmers say the following:

Those are some of the basic issues. There's a lot of ground to cover. If you have questions after that, let us know.

Incidentally, the code snippet I wrote duplicated your code's functionality, but has a slight flaw. The following is a valid query string:

color=red&color=blue&color=some%20other%20value
Note that there are three values for 'color'. This is common. My code above will only return the first value entered. I did that to simplify the code. The following will correct for that (rough hack follows):
foreach my $name ( @names ) { my @values = param( $name ); print "Name = $name, Value(s) = " , join (',' @values) , "<br>\n"; }
That works because of the following:
# Assign param() to a scalar and you only get the first value my $value = param( $name ); # Assign param() to an array and you get all of the values my @values = param( $name );

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: (Ovid) Re: Beginning Perl and Forms - -w Swtich
by Maclir (Curate) on Jan 19, 2001 at 03:32 UTC
    Ovid's advice:
    Turn on taint mode (-T switch. See perlsec)
    is generally sound advice. However, if your cgi environment involves running under mod_perl, you will find this causes a warning message. Basically, you cannot enable taint mode under mod_perl with the -T switch on the shebank line.

    From the mod_perl doco:

    Since the -T switch doesn't have an equivalent perl variable, mod_perl provides the PerlTaintCheck directive to turn on taint checks. In httpd.conf, enable this mode with:

    PerlTaintCheck On

    Now any code compiled inside httpd will be taint checked.

    If you use the -T switch, Perl will warn you that you should use the PerlTaintCheck configuration directive and will otherwise ignore it.

    Now, since you are running under an enviroment that is outside your control, you may wish to check whether mod_perl is in use, and if so, whether the PerlTaintCheck directive is in place.

    A reply falls below the community's threshold of quality. You may see it by logging in.