Operators
Arithmetic
Binary operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (returns the integer remainder)
** Exponent
Unary operators
- Unary negation (reverses the sign)
++ Increment (can be prefix or postfix)
-- Decrement (can be prefix or postfix)
Assignment
= Assign
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign
**= Exponent and assign
Comparison
Strings Numbers
------- -------
eq ==
ne !=
gt >
ge >=
lt <
le <=
cmp <=>
Conditional
? : Ternary comparison operator
condition ? val1 : val2 (Evaluates val1 if condition is true; otherwise, evaluates val2)
Boolean
- Short-circuit logical operations
- Evaluates the minimal number of expressions necessary
- Partial evaluation (rather than full evaluation)
&& And
|| Or
! Not (logical negation)
Bitwise
Binary operators
& And
| Or
^ Xor
<< Shift left (zero fill)
>> Shift right (sign-propogating); copies of the leftmost bit (sign bit) are shifted in from the left.
Unary operators
~ Not (inverts the bits)
String
= Assignment
. Concatenation
x Repetition (multiplication)
.= Concatenate and assign
x= Repeat and assign
$str = "ab" . "cd"; # "abcd"
$str .= " "; # Append a space
$str x= 5; # Repeat the string 5 times
List
() List constructor
, Used to separate elements of the list
[] Used to take slices of the list
.. Range operator; creates a list of numbers in the range n..m
("zero", "one", "two")[0, 2]; # ("zero", "two")
("zero", "one", "two")[1..2]; # ("one", "two")
Binding
=~
!~ Logical negation of =~
- Apply the regex to the variable
- Used for pattern matching
$str =~ /one/; # Search $str for "one".
Resources URL:
notes/perl/resources
Sources URL:
notes/perl/sources