leestretton.com

You are in: > Home > Developers Guides

Perl - Operators

Perl Camel

Numeric and String Operators

There are many operators in Perl, here are a few that are commonly used:

OperatorDescriptionExampleResult
.String concatenate'ACTGA' . 'GCG'ACTGAGCG
=Assignment$a = 'ATP'$a variable is storing 'ATP'
+Addition4 + 26
-Subtraction7 - 25
-Negation-3-3
!Not!10
*Multiplication5 * 210
/Division7 / 23.5
%Modulus3%21
**Exponentiation6**236
x
Repetition'A' x 7AAAAAAA

Logical Operators

The logical operators evaluate from left to right testing the truth of the statement.

$a && $b
and
$a if $a is false, $b otherwise
$a || $b
or
$a if $a is true, $b otherwise

Comparison Operators

These operators compare strings or numbers, returning TRUE (1) or FALSE (0):

Numeric ComparisonString Comparison
3 == 20equal to'Santa' eq 'Claus' 0equal to
3 != 21not equal to'Santa' ne 'Claus'1not equal to
3 < 20less than'Santa' lt 'Claus'0less than
3 > 21greater than'Santa' gt 'Claus'1greater than
3 <= 20less or equal'Santa' le 'Claus'0less than or equal
3 >= 21greater than or equal'Santa' ge 'Claus'1greater than or equal
3 <=> 21compare'Santa' cmp 'Claus'1compare

The string comparison operators compare the alphabetical order.

The numeric comparison operators are quite self explanatory except probably the last numeric one, which may need an example:

3 <=> 2

if left side of the operator is greater than the right side, it returns 1
if both sides are equal, 0
if right side of the operator is greater than the left side, it returns -1

Problem with this page? Let us know
© Lee Stretton 2005 | powered by linux powered by php 5 powered by MySQL download rss feeds powered by php 5 valid css