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:
- Turn on warnings (that's the -w switch on the shebang line)
- Turn on taint mode (-T switch. See perlsec)
- Use strict.
- Don't try to parse form data by hand. See use CGI or die;. My apologies for the harsh title :)
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. |