Monday, December 05, 2005

 

Perl: Conditionals

To do comparisons on different values, perl provides two sets of
conditionals - one for numbers and one for strings.
Comparison Strings Numbers
Equality eq ==
Inequality ne !=
Greater than gt >
Greater than or equal to ge >=
Less than lt <
Less than or equal to le <=
Comparison
returns -1,0,1 cmp <=>

It's important to use the right conditional - there are no "syntax"
errors, so if you use a variable in the wrong context, there could be
unexpected results.
You can build up conditionals using && (AND) and || (OR) operations.

Truth is a very flexible value in perl:

#### Some examples of truth:
1; # traditionally
("a","b"); # the list has elements
" "; # whitespace is true
"hello"; # strings are true
"00" # a string
#### Expressions that evaluate to false
0; # traditionally
(); # the empty list
""; # the null string
"0"; # The one string of non-zero length that is false.


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?