Operators and Other Statements


Programming has always been about manipulating data, so you can't do much with all these variables unless you have some operators to work on them. There are three main types of operators in Perl: comparison, compound, and arithmetic operators.

Comparison operators compare the value of one operation or statement with that of another to see if the overall statement resolves to either "true" or "false." A different course of action can be followed within the program, depending on how statements resolve. Table 30.1 lists the comparison operators offered by Perl. You'll notice that the operators are different for numerals and strings.

Table 30.1. Perl Comparison Operators

Meaning

Numeric Operator

String Operator

Equal to

==

eq

Less than

<

lt

Greater than

>

gt

Less than or equal to

<=

le

Greater than or equal to

>=

ge

Not equal to

!=

ne

Returns -1 if less than, 0 if equal, and 1 if greater than

<=>

cmp

Range between first and second operands

..

 

Matched by regular expression

 

=~

Not matched by regular expression

 

!~


Compound operators are largely identical to Boolean operators and can be combined with other operator types into more complex forms of results. These are listed in Table 30.2.

Table 30.2. Perl Compound Operators

Meaning

Operator

Logical AND

&&

Logical OR

;;

Logical NOT

!

Group these compound statements

()


Arithmetic operators can be used to perform all sorts of mathematical operations on variables. These are listed in Table 30.3.

Table 30.3. Perl Arithmetic Operators

Meaning

Operator

Raises x to the y power

x**y

Calculates the remainder of x/y

x%y

Adds x to y

x+y

Subtracts y from x

x-y

Multiplies x times y

x*y

Divides x by y

x/y

Negates y

-y

Increments y by 1 and uses value

++y

Uses value of y and then increments by 1

y++

Decrements y by 1 and uses value

--y

Uses value of y and then decrements by 1

y++

Assigns value of y to x

x=y




SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net