eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

introduction to web programming
Equality operators  
One of the most important string manipulation functions is that of matching or testing of equality. It is an important tool because you can use it as the basis of complex logical comparisons necessary for the intelligence demanded of a CGI application.

For example, many CGI applications use one of the most basic methods of pattern matching, the "ne" operator, as the basis of their decision making process using the following logic:

    if (the user has hit a specific submit button)
         {
         execute a specific routine.
         }

Consider this code snippet:

    if ($display_frontpage_submit_button ne "")
           {
           &display_frontpage;
           }

If you are confused about the usage of the "if" test, it is explained in greater detail in the "Control Structures" section later.

The "ne" operator asks if the value of the variable $display_frontpage_submit_button is not equal to an empty string. This logic takes advantage of the fact that the HTTP protocol specifies that if a FORM submit button is pressed, its NAME is set equal to the VALUE specified in the HTML code. For example, the submit button may have been coded using the following HTML:

    <INPUT TYPE = "submit" NAME =
    "display_frontpage_submit_button" VALUE =
    "Return to the Frontpage">

Thus, if the NAME in the associative array has a VALUE, the script knows that the client pushed the associated button. The script determines which routines it should execute by following the logic of these pattern matches.

Similarly, you can test for equality using the "eq" operator. An example of the "eq" operator in use is shown below:

    if ($name eq "Selena")
           {
           print "Hi, Selena\n";
           }

When comparing numbers instead of strings however, Perl uses a second set of operators. For example, to test for equality, you use the double equal (==) operator as follows:

    if ($number == 11)
           {
           print "You typed in 11\n";
           }

Warning: Never use the single equal sign (=) for comparison. Perl interprets the equal sign in terms of assignment rather than comparison. Thus the line:

$number = 11;

actually assigns the value of eleven to $number rather than comparing $number to eleven.

There are many other types of comparison operators, but they are better researched in more comprehensive texts. However, we do include several important ones in the following table

Numeric Op. String Op Description
== eq Equal
!= ne Not equal
lt Less than
> gt Greater than
>= le Less than or equal to
>= ge Greater than or equal to

Previous | Next | Table of Contents