 |
Wednesday Feburary 18
CGI & Perl (cont.)
This lecture is drawn in part from the general announcement I made on hypernews regarding assignment 7. I'll go over some new Pearl operators and then we'll
discuss the last part of the assignment, and discuss how Pearl does pattern matching.
Ok. We've discussed a few basic operators in Pearl: if, while, and print. After mastering these you began to use split. At this point, you have the tools to create an html page for the browser, pass variables, print to file, and to read text from a file and store it into variables.
Here are some new operators that will help you move even further with Pearl. In the following definitions, A and B stand are placeholders which you can think of as variables ($VAR). However, this definition will expand as you begin to use them.
-
the equals operator for numbers: ==
This operator allows you to check that A is equal to the value of B... (A == B). If A is == B, then this statement is true. You use this when your variable contains a number, not for text, which we call strings.
-
the equals operator for strings: eq
This operator allows you to check that the text in A is equal to the text of B... (A eq B). If A is eq B, then this statement is true. You use this when your variable contains a bunch of characters (a string).
-
the NOT EQUAL operator for numbers: !=
This operator allows you to check that A and B are not equal ...A != B. If A is not equal to the numerical value of B, then the statement is true.
-
the NOT EQUAL operator for strings: ne
To say A ne B means that the string in A is not equal to the string in B.
-
the AND operator: &&
This operator allows you to check that A andB are true (this works for as many items as you want....A&&B&&C&&D...). If A and B are both true, then the statement is true.
When we use this for an IF statement,
IF ($varA eq $varB) && ($varC eq $varD){
...
};
then if the strings A and B are equal AND the strings C and D are equal, then the statement is true. When the IF statement is true, anything inside the curly brackets { } will be executed.
-
the OR operator: ||
This operator allows you to check that A orB are true before you execute a step of your code (this works for as many items as you want....A||B||C||D...). If A or B is true (either one or both) then the statement is true.
When we use this for an IF statement,
IF ($varA eq $varB) || ($varC eq $varD){
...
};
then if the strings A and B are equal OR the strings C and D are equal, then the statement is true. When the IF statement is true, anything inside the curly brackets { } will be executed.
-
Operators work on any logical statement. This means that you can compare complex statements:
if ( ($varA == 10)&&($varB eq "thousand") || ($varA == 20)&&($varB eq "thousand") ) {
...
}
- And just in case it comes up, in a multi-line text area, the user may type in return characters. You can strip out these return charcters with the following line:
$var =~s/\n/ /g;
which substitutes all newline characters with a space, globally (throughout the entire entry).
|