Computer Science Canada [Perl5-tut] Perl Intro: Conditionals |
Author: | wtd [ Fri Sep 23, 2005 1:25 am ] | ||||||||||||
Post subject: | [Perl5-tut] Perl Intro: Conditionals | ||||||||||||
If This looks pretty much like most languages. A notable differences is that the curly braces are required. There is no "shortcut" syntax.
Elsif and else As in other languages, there are fallback clauses.
Unless "if" has a counterpart. It's called unless. For instance, instead of:
We can write:
Postfix For very simple tests, we can use "if" and "unless" in a postfix form.
The ternary operator This also functions pretty much the same way it does in other languages.
If the test in front of the question mark is true, then the first expression is evaluated. If not, the second expression is evaluated. The expressions are separated by a colon. Comparison operators Strings and numbers use different comparison operators in Perl5. For numbers, we can use the classic: ==, !=, <, >, <=, >=, and for a three way comparison: <=>. For strings, the corresponding operators are: eq, ne, lt, gt, le, ge, and cmp. |