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