shadowline
Subroutines << Forms & Hashes >> Matching - Intro
Before you lead on into this page, you should have a good understanding of FORMS.

Up to now, you have probably used either the MAILTO method or a 3rd party CGI service in the ACTION value. Well, it's time to move on and create your own. The value will show a path to your own form processing script.

When a form is subumitted, all of the information entered in the fields is send via HTTP to the cgi script seen in the ACTION value. The information is sent in plain ASCII text as a KEY=VALUE format. The name of the field element, an equal sign, then the entered value. Each key/value pair is separated with an & ampersand.

The next important part of the form tag is the METHOD property. Most of your previous encounters have probably set this to POST (which is the most common and useful).



GET is the default method for submitting forms, but it does come with its downfalls. Because this method appends (attaches) the entered information from the form to the URL in the ACTION, the complete URL can be so long that it exceeds the available environment space. This can cause the server to truncate (shorten) the URL, which then results in a bad submission.

Actual submitted : http://www.someplace.com/cgi-bin/formprocessor.cgi?key1=val1&key2=val2
Truncated : http://www.someplace.com/cgi-bin/formproc...y2=val2

If you decide you won't have any trouble using GET (such as with short forms with small amounts of data) then remember the output is stored in URL-encoded form. When it is in this form, the info can be obtained from environment variable QUERY_STRING which is available to your scripts.

POST is the most common method used. This is because it does not limit the amount of data that can be passed to the server program. Post can do this because the information isn't passed on the URL but instead in the body or STDIN of the request.



Hashes are important in perl/cgi scripts because most form parsing subroutines store the data gathered from a web form in a hash. As you have read over the HASHES page, these work in the same key/value fashion as the entered data. The key in each pair is the field name (from the NAME property) while the value in the pair equals the data the vistior entered (from the VALUE property). You can then use the names (keys) to find, access, and process the related input data (values).



Before going any further, you may want to do a quick download. Below is a link to download a small script called "subparseform.lib". This is a very powerful pre-made script that is used to parse (translate) the entered form data. It simply organizes the key/value pairs for you. On a later tutorial page, this actual script will be broken down and explained, but for now, it is very useful tool.

subparseform.lib

For the examples, and to use it in your own scripts, the lib file should be uploaded to the same area as your other cgi scripts in ASCII format.



GET VALUE USING A KEY. Hashes are made up of pairs. Each has the key first and the value second. You an use the key to access the corresponding value.

Sample coding from form area on web page :
<input type="text" name="first">

The cgi script could be shown similar to :
#!/usr/bin/perl

print "Content-type: text/html\n\n";
require "subparseform.lib";
&Parse_Form;
$value = $formdata{'first'};
print "The value associated for the form field called FIRST is $value";

- The REQUIRE command bring the subparseform script into this current one.
- Parse_Form is the subroutine command that activates the subparseform script.
- The subparsform script uses the hash %formdata to store the collected information from the form.
- The value within single quotes, within curly brackets, is the key you are looking for. The corresponding value for that key is assinged to the $value scalar (in this example). Notice a $ is used instead of % since you are accessing one piece of data. Notice also the use of curly brackets {} is used instead of round brackets ().



GETTING SEVERAL VALUES USING KEYS. If you need more than one value and you already know the specific keys, you can use the keys to access the values. This is known as taking a "slice" of the hash.

Sample coding from form area on web page :
<input type="text" name="first">
<input type="text" name="second">

The cgi script could be shown similar to :
#!/usr/bin/perl

print "Content-type: text/html\n\n";
require "subparseform.lib";
&Parse_Form;
@value = @formdata{'first', 'second'};

The first value will be stored in $value[0] and the second value will be stored in $value[1]. The @ symbol is used because more than one value is being assinged. The @ is used for formdata instead of % as we are accessing individiual pieces of data, not an actual hash.

Getting values from a hash does not alter the hash itself. We are simply finding and copying the values.



GETTING ALL OF A HASH'S VALUES. To print or process all of the values of a hash, PERL has a special function called VALUES.

#!/usr/bin/perl

print "Content-type: text/html\n\n";
require "subparseform.lib";
&Parse_Form;
@arrayname = values{%formdata};

Now all of the values found within the data from the form are stored in the array.



GETTING ALL OF A HASH'S KEYS. To print or process all of the keys of a hash, PERL has a special function called KEYS.

#!/usr/bin/perl

print "Content-type: text/html\n\n";
require "subparseform.lib";
&Parse_Form;
@arrayname = keys{%formdata};

Now all of the keys found within the data from the form are stored in the array.



GETTING EACH KEY AND VALUE. There are 2 main ways to access a hash's keys and values at the same time. The Perl EACH command goes through each key and value in a hash sequentially. Each time you use the EACH command, it returns the next pair of items.

#!/usr/bin/perl

print "Content-type: text/html\n\n";
require "subparseform.lib";
&Parse_Form;
while (($key, $value) = each (%formdata)){
print "They key is $key and the value is $value <br>";
}

The EACH command is extracting the individual key and value from the hash and putting them into the scalar variables. As long as the extraction process is "true" the WHILE loop will execute.

Using a WHILE loop, the above script goes through each pair set in the hash. The loop helps by not having to type the EACH function many times. Each time you use the EACH function it is executed and the next key-value pair is accessed.



The KEYS function combined with a foreach loop can also be used to go through each key-value pair in a hash.

#!/usr/bin/perl

print "Content-type: text/html\n\n";
require "subparseform.lib";
&Parse_Form;
foreach $key (keys %formdata){
print "They key is $key and the value is $formdata{$key} <br>";
}

A key is extracted from the hash and put into the scalar $key. FOREACH of these occurances that are "true" the loop will be executed. The corresponding value for the key is displayed using the normal extraction method.



REMOVING KEY/VALUE PAIRS. To get rid of a key/value pair, the DELETE function is used.

delete $formdata{'second'};

The example deleted the key/value pair that had the key named "second".



CHECKING TO SEE IF A KEY EXISTS. To check if one specific key appears, the PERL command EXISTS is used. Here is a simple example using an IF ELSE statement :

if (exists $formdata{'second'}) {
    do this block;
}
else {
    do this other block;
}

The exists command returns a "true" value as long as the key exists in the hash.
Subroutines << Forms & Hashes >> Matching - Intro
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow