7. Processing and Analyzing Data

 

 

7.1 Retrieving Form Data

 

To analyze Information gathered from Web visitor, Perl supports split() function and regular expressions for splitting, matching, substituting, and translating data elements. We may use split() function to parse the such information as check boxes, menu, etc that we received a form data.

 

Example 7-1: Visitors will be given the formproc.html to fill in the required information. The form then is sent back to the Web server, the formproc.pl CGI script is used to retrieve the form information, and finally the Web server send the confirmation about the submission right back to the visitor. The split function is not used in this example.

 

 

<!-- formproc.html -->

<HTML>

<HEAD>

<TITLE>Processing FORM data </TITLE>

</HEAD>

<H2><STRONG>Please fill out the fields and click submit.</STRONG></H2>  

   <FORM METHOD = "POST" ACTION = "/cgi-bin/formproc.pl">    

      <P><STRONG>First Name:</STRONG></P>

      <INPUT TYPE = "TEXT" NAME = "FNAME"><BR>

      <P><STRONG>Last Name:</STRONG></P>

      <INPUT TYPE = "TEXT" NAME = "LNAME"><BR>

      <p><STRONG>Email: </STRONG></P>

      <INPUT TYPE = "TEXT" NAME = "EMAIL" SIZE = 25><BR><BR>

      <INPUT TYPE = "SUBMIT" VALUE = "Submit">&nbsp

      <INPUT TYPE = "RESET"  VALUE = "Reset">

   </FORM>

</BODY>

</HTML>

 

 

 

 

# formproc.pl

# Read and process the FORM data from formproc.html document.

$email      = param(EMAIL);

$firstName  = param(FNAME);

$lastName   = param(LNAME);

print "<HTML>\n";

print "<BODY>\n";

print "<B>$firstname</B>, Thank you for completing the form<BR>\n";

print "You have not been added to our mailing list.\n";

print "</BODY>\n";

print "</HTML>\n";

 

The confirmation from the Web server:

 

 

 

Example 7-2: A Sample Guest Book for Your Web Site:

 

When user submit the form, the browser might initiate a HTTP transaction by sending the following string to the server:

 GET /cgi-bin/guestbook.pl?firstname=Paul&lastname=Lin HTTP/1.0

 

The server then pass the pairs of names to the CGI program.

 

<!-- CGI_FORM.HTML -->

<!-- After a guest enter the first and last names, then press the

     Submit button, data entered into the <INPUT> text field is passed

     to the CGI program specified by the <FORM> ACTION attribute. -->

<HTML>

<HEAD><TITLE>Guest Book</TITLE></HEAD>

<BODY>

<H1> ECET/EET 499 Web Programming for Industrial Applications <H1>

<H2> Guest Sign In <H2>

<FORM METHOD="GET" ACTION="/cgi-bin/guestbook.pl">

<PRE>

First Name: <INPUT TYPE="TEXT" NAME="firstname">

Last Name:  <INPUT TYPE="TEXT" NAME="lastname">

 

<INPUT TYPE="SUBMIT">   <INPUT TYPE="RESET">

</FORM>

</BODY>

</HTML>

 

 

7.2 Regular Expression Concepts

 

To analyze Information gathered from Web visitor, Perl supports split() function and regular expressions for splitting, matching, substituting, and translating data elements. We use split() function to parse the such information as check boxes, menu, etc that we received a form data. A regular expression is a pattern to be matched against a string that composed of letters, numbers, and special symbols. This string may also defines one or more sub-strings. Most UNIX editors and grep commands also support regular expressions in some form.

 

Perl has different forms of regular expressions for performing matching, substituting, and splitting:

 

m/ / command for Matching in the form of $aString =~ m / /;

            # m " " and m{ } are synonyms

 

s / / / command for Substituting in the form of $aString =~ s / / /;

            # s " " " and s{ } { } are synonyms

 

tr/ / / command for Translating the whole string at once, converting each individual character in turn.

 

 

 

 

 

 

7.3 Regular Expressions for Translation

 

·        Convert the upper case characters in a variable $thisString to lower case characters and leaves special symbols and characters that were not found in the string alone:

 

$thisString = "The Perl Programming Language";

$thisString =~ tr/A-Z/a-z/;

            or

$thisString = "The Perl Programming Language";

$_ = $thisString;

tr/A-Z/a-z/;

$thisString = $_ ;

 

 

·        Convert the lower case characters in a variable $thisString to upper case characters and leaves special symbols and characters that were not found in the string alone:

 

$thisString = "The Perl Programming Language";

$thisString =~ tr/a-z/A-Z/;

            or

$thisString = "The Perl Programming Language";

$_ = $thisString;

tr/a-z/A-Z/;

$thisString = $_ ;

 

7.4 Regular Expressions for Substitution

 

Deletion is can be considered as a substitution.

 

The period (.) matches any single characters.

 

\. for matching a literal period, with a back slash precede the period.

 

\\ matches a single back slash.

 

\? matches a single question mark.

 

\* matches a single asterisk.

 

.* matches any string of any length. We use the wild card asterisk ( *) to match a variable number of characters.

 

..% matches the two characters and a % sign.

 

\d matches any single digit.

 

\d* matches zero any single digit.

 

\w matches "word" characters

 

\s matches any white space characters including a space, tab, newline, etc.

 

.*\. matches anything up to and including the last period.