Operators


Perl supports a number of operators to perform various operations. There are comparison operators (used to compare values, as the name implies), compound operators (used to combine operations or multiple comparisons), arithmetic operators (to perform math), and special string constants.

Comparison Operators

The comparison operators used by Perl are similar to those used by C, awk, and the csh shells, and are used to specify and compare values (including strings). Most frequently, a comparison operator is used within an if statement or loop. Perl has comparison operators for numbers and strings. Table 30.1 shows the numeric comparison operators and their behavior.

Table 30.1. Numeric Comparison Operators in Perl

Operator

Meaning

==

Is equal to

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

!=

Not equal to

..

Range of >= first operand to <= second operand

<=>

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


Table 30.2 shows the string comparison operators and their behaviors.

Table 30.2. String Comparison Operators in Perl

Operator

Meaning

eq

Is equal to

lt

Less than

gt

Greater than

le

Less than or equal to

ge

Greater than or equal to

ne

Not equal to

cmp

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

=~

Matched by regular expression

!~

Not matched by regular expression


Compound Operators

Perl uses compound operators, similar to those used by C or awk, which can be used to combine other operations (such as comparisons or arithmetic) into more complex forms of logic. Table 30.3 shows the compound pattern operators and their behavior.

Table 30.3. Compound Pattern Operators in Perl

Operator

Meaning

&&

Logical AND

||

Logical OR

!

Logical NOT

()

Parentheses; used to group compound statements


Arithmetic Operators

Perl supports a wide variety of math operations. Table 30.4 summarizes these operators.

Table 30.4. Perl Arithmetic Operators

Operator

Purpose

x**y

Raises x to the y power (same as x^y)

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

-y

Negates y (switches the sign of y); also known as the unary minus

++y

Increments y by 1 and uses value (prefix increment)

y++

Uses value of y and then increments by 1 (postfix increment)

--y

Decrements y by 1 and uses value (prefix decrement)

y--

Uses value of y and then decrements by 1 (postfix decrement)

x=y

Assigns value of y to x. Perl also supports operator-assignment operators (+=, - =, *=, /=, %=, **=, and others)


You can also use comparison operators (such as == or <) and compound pattern operators (&&, ||, and !) in arithmetic statements. They evaluate to the value 0 for false and 1 for true.

Other Operators

Perl supports a number of operators that don't fit any of the prior categories. Table 30.5 summarizes these operators.

Table 30.5. Other Perl Operators

Operator

Purpose

~x

Bitwise not (changes 0 bits to 1 and 1 bits to 0)

x & y

Bitwise and

x | y

Bitwise or

x ^ y

Bitwise exclusive or (XOR)

x << y

Bitwise shift left (shifts x by y bits)

x >> y

Bitwise shift right (shifts x by y bits)

x . y

Concatenate y onto x

a x b

Repeats string a for b number of times

x , y

Comma operatorevaluates x and then y

x ? y : z

Conditional expressionif x is true, y is evaluated; otherwise, z is evaluated


Except for the comma operator and conditional expression, these operators can also be used with the assignment operator, similar to the way addition (+) can be combined with assignment (=), giving +=.

Special String Constants

Perl supports string constants that have special meaning or cannot be entered from the keyboard. Table 30.6 shows most of the constants supported by Perl.

Table 30.6. Perl Special String Constants

Expression

Meaning

\\

The means of including a backslash

\a

The alert or bell character

\b

Backspace

\cC

Control character (like holding the Ctrl key down and pressing the C character)

\e

Escape

\f

Formfeed

\n

Newline

\r

Carriage return

\t

Tab

\xNN

Indicates that NN is a hexadecimal number

\0NNN

Indicates that NNN is an octal (base 8) number




Red Hat Fedora 5 Unleashed
Red Hat Fedora 5 Unleashed
ISBN: 067232847X
EAN: 2147483647
Year: 2004
Pages: 362

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