Questions And Answers

More Tutorials

Basic Operators

Arithmetic Name Result
$a + $b Addition Sum of $a and $b
$a * $b Multiplication Product of $a and $b
$a % $b Modulus Remainder of $a divided by $b
$a ** $b Exponentiation $a to the power of $b

Example Name Result
$a . “string” Concatenation String built from pieces
“$a string” Interpolation String incorporating the value of $a
$a x $b Repeat String in which $a is repeated $b times

The basic assignment operator is “=”: $a = $b. Perl conforms to the C idiom that lvalue operator= expression is evaluated as: lvalue = lvalue operator expression So that $a *= $b is equivalent to $a = $a * $b $a += $b $a = $a + $b This also works for the string concatenation operator: $a .= “\n”

Autoincrement and Autodecrement
The autoincrement and autodecrement operators are special cases of the assignment
operators, which add or subtract 1 from the value of a variable:

++$a, $a++ Autoincrement Add 1 to $a
--$a, $a-- Autodecrement Subtract 1 from $a

Conditions for truth:
Any string is true except for “” and “0”
Any number is true except for 0
Any reference is true
Any undefined value is false
Example Name Result
$a && $b And True if both $a and $b are true
$a || $b Or $a if $a is true; $b otherwise
!$a Not True if $a is not true
$a and $b And True if both $a and $b are true
$a or $b Or $a if $a is true; $b otherwise
not $a Not True if $a is not true

Logical operators are often used to “short circuit” expressions, as in:
open(FILE,”< input.dat”) or die “Can’t open file”;

Comparison Numeric String Result
Equal == eq True if $a equal to $b
Not equal != ne True if $a not equal to $b
Less than < lt True if $a less than $b
Greater than > gt True if $a greater than $b
Less than or equal <= le True if $a not greater than $b
Comparison <=> cmp 0 if $a and $b equal 1 if $a greater -1 if $b greater


In this page (written and validated by ) you learned about Perl Basic Operators . What's Next? If you are interested in completing Perl tutorial, your next topic will be learning about: Perl Operator Precedence.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.