Operators#
PetaPerl implements the full set of Perl 5 operators with identical semantics. All operators maintain Perl’s context sensitivity and precedence rules.
Binary Operators#
Arithmetic#
Operator |
Operation |
Example |
|---|---|---|
|
Addition |
|
|
Subtraction |
|
|
Multiplication |
|
|
Division |
|
|
Modulus |
|
|
Exponentiation |
|
All arithmetic operators coerce operands to numeric context. Division by zero produces a warning and returns infinity or NaN.
my $x = 5 + 3; # 8
my $y = 10 / 3; # 3.333...
my $z = 2 ** 10; # 1024
my $m = 17 % 5; # 2
String#
Operator |
Operation |
Example |
|---|---|---|
|
Concatenation |
|
|
Repetition |
|
String operators coerce operands to string context.
my $str = "Hello" . " " . "World"; # "Hello World"
my $rep = "X" x 5; # "XXXXX"
my $pad = " " x 10; # 10 spaces
Numeric Comparison#
Operator |
Operation |
Returns |
|---|---|---|
|
Equal |
True if equal |
|
Not equal |
True if not equal |
|
Less than |
True if less |
|
Greater than |
True if greater |
|
Less or equal |
True if ≤ |
|
Greater or equal |
True if ≥ |
|
Three-way compare |
-1, 0, or 1 |
Numeric comparisons coerce operands to numbers.
if ($age >= 18) { ... }
my $cmp = $a <=> $b; # -1 if $a < $b, 0 if equal, 1 if $a > $b
String Comparison#
Operator |
Operation |
Returns |
|---|---|---|
|
Equal |
True if equal |
|
Not equal |
True if not equal |
|
Less than |
True if less |
|
Greater than |
True if greater |
|
Less or equal |
True if ≤ |
|
Greater or equal |
True if ≥ |
|
Three-way compare |
-1, 0, or 1 |
String comparisons use lexicographic (dictionary) ordering.
if ($name eq "John") { ... }
my $cmp = $a cmp $b; # -1, 0, or 1
Logical#
Operator |
Operation |
Short-circuits |
Precedence |
|---|---|---|---|
|
And |
Yes |
High |
` |
` |
Or |
|
|
Defined-or |
Yes |
High |
|
And |
Yes |
Low |
|
Or |
Yes |
Low |
|
Exclusive or |
No |
Low |
Logical operators return the last evaluated value, not just true/false.
my $result = $x && $y; # Returns $y if $x true, else $x
my $default = $user || "guest"; # Returns "guest" if $user false
my $value = $config // 0; # Returns 0 only if $config undefined
Defined-or (//): Unlike ||, this only checks if the left side is defined, not just true. 0 and "" are both false but defined.
my $x = 0;
my $a = $x || 10; # 10 (0 is false)
my $b = $x // 10; # 0 (0 is defined)
Bitwise#
Operator |
Operation |
Example |
|---|---|---|
|
Bitwise AND |
|
` |
` |
Bitwise OR |
|
Bitwise XOR |
|
|
Left shift |
|
|
Right shift |
|
Bitwise operators work on integers. Operands are converted to unsigned integers.
my $mask = 0xFF & $value;
my $flags = $READ | $WRITE;
my $double = $x << 1;
Range#
Operator |
Operation |
Context |
|---|---|---|
|
Inclusive range |
List context creates list |
|
Flip-flop |
Scalar context is stateful |
my @digits = (0..9); # (0, 1, 2, ..., 9)
my @letters = ('a'..'z'); # ('a', 'b', ..., 'z')
for my $i (1..100) { ... } # Loop 1 to 100
In scalar context, .. and ... are flip-flop operators (stateful boolean range).
Binding#
Operator |
Operation |
Example |
|---|---|---|
|
Match |
|
|
Not match |
|
Binding operators connect strings with regex operations.
if ($email =~ /\@/) { ... } # Contains @
if ($name !~ /^\d/) { ... } # Doesn't start with digit
Unary Operators#
Arithmetic#
Operator |
Operation |
Example |
|---|---|---|
|
Negation |
|
|
Unary plus |
|
my $neg = -5;
my $pos = +$x; # Numeric context
Logical#
Operator |
Operation |
Example |
|---|---|---|
|
Not |
|
|
Not (low precedence) |
|
if (!$error) { ... }
die "Failed" if not $ok;
Bitwise#
Operator |
Operation |
Example |
|---|---|---|
|
Bitwise complement |
|
my $inverted = ~$bits;
Reference#
Operator |
Operation |
Example |
|---|---|---|
|
Create reference |
|
my $scalar_ref = \$value;
my $array_ref = \@data;
my $hash_ref = \%config;
Increment/Decrement#
Operator |
Operation |
When evaluated |
|---|---|---|
|
Pre-increment |
After increment |
|
Pre-decrement |
After decrement |
|
Post-increment |
Before increment |
|
Post-decrement |
Before decrement |
my $x = 5;
my $a = ++$x; # $x is 6, $a is 6
my $b = $x++; # $x is 7, $b is 6
String increment: ++ on strings performs “magic increment” (Perl-style).
my $s = "aa";
$s++; # "ab"
$s++; # "ac"
File Test Operators#
File test operators check properties of files and filehandles. All return true/false except -s which returns file size.
Operator |
Test |
Returns |
|---|---|---|
|
Exists |
Boolean |
|
Readable |
Boolean |
|
Writable |
Boolean |
|
Executable |
Boolean |
|
Owned by effective UID |
Boolean |
|
Readable by real UID |
Boolean |
|
Writable by real UID |
Boolean |
|
Executable by real UID |
Boolean |
|
Owned by real UID |
Boolean |
|
Zero size |
Boolean |
|
Non-zero size |
Size in bytes or false |
|
Regular file |
Boolean |
|
Directory |
Boolean |
|
Symbolic link |
Boolean |
|
Named pipe (FIFO) |
Boolean |
|
Socket |
Boolean |
|
Block special file |
Boolean |
|
Character special file |
Boolean |
|
TTY (terminal) |
Boolean |
|
Setuid bit set |
Boolean |
|
Setgid bit set |
Boolean |
|
Sticky bit set |
Boolean |
|
Text file |
Boolean |
|
Binary file |
Boolean |
|
Modification time (days) |
Number |
|
Access time (days) |
Number |
|
Inode change time (days) |
Number |
if (-e $file) { ... } # File exists
if (-f $path && -r $path) { ... } # Regular file and readable
my $size = -s $file; # Size in bytes
if (-d $path) { ... } # Is directory
Stacked file tests: -f -w -r $file checks all three conditions.
Other Unary Operators#
Operator |
Operation |
Example |
|---|---|---|
|
Check if defined |
|
if (defined $value) { ... }
Assignment Operators#
Simple Assignment#
Operator |
Operation |
Example |
|---|---|---|
|
Assignment |
|
my $x = 10;
my ($a, $b, $c) = (1, 2, 3); # List assignment
Compound Assignment#
All binary operators have compound assignment forms:
Operator |
Equivalent |
Example |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
` |
=` |
`$x = $x |
|
|
|
|
|
|
|
|
|
|
|
|
` |
=` |
|
|
|
|
my $count = 10;
$count += 5; # 15
$count *= 2; # 30
my $path = "/home";
$path .= "/user"; # "/home/user"
$cache ||= load_data(); # Only load if $cache is false
$config //= {}; # Only assign if $config is undef
Ternary Operator#
Operator |
Syntax |
Example |
|---|---|---|
|
Conditional |
|
my $result = $x > 0 ? "positive" : "non-positive";
my $max = $a > $b ? $a : $b;
Ternary as lvalue: The ternary operator can be used as an assignment target.
($x > 0 ? $pos : $neg) = 10; # Assigns to $pos or $neg
Array/Hash Subscripts#
Syntax |
Operation |
Example |
|---|---|---|
|
Array element access |
|
|
Hash element access |
|
|
Array slice |
|
|
Hash slice |
|
my $first = $arr[0];
my $value = $hash{key};
my @subset = @arr[0, 2, 4]; # Elements 0, 2, 4
my @values = @hash{'a', 'b'}; # Values for keys 'a', 'b'
Arrow Operator#
Syntax |
Operation |
Example |
|---|---|---|
|
Array deref subscript |
|
|
Hash deref subscript |
|
|
Method call |
|
my $element = $array_ref->[5];
my $value = $hash_ref->{name};
my $result = $object->method(@args);
Comma Operators#
Operator |
Operation |
Use |
|---|---|---|
|
List separator |
|
|
Fat comma |
|
The fat comma (=>) autoquotes barewords on its left side.
my %hash = (
name => "John", # 'name' autoquoted
age => 30,
);
Operator Precedence#
Highest to lowest precedence (same as Perl 5):
Terms and list operators (left)
->(left)++--(none)**(right)!~\unary+unary-(right)=~!~(left)*/%x(left)+-.(left)<<>>(left)Named unary operators
<><=>=ltgtlege(none)==!=<=>eqnecmp~~(none) —~~smart match is partial&(left)|^(left)&&(left)||//(left).....(none)?:(right)=+=-=etc. (right),=>(left)List operators (right)
not(right)and(left)orxor(left)
Use parentheses when precedence is unclear.
Named unary operators#
Row 10 above (“named unary operators”) refers to built-ins that take
exactly one argument and sit at a specific precedence: tighter than
the comparison operators (<, >=, ==, eq, …) but looser than the
arithmetic and shift operators. The practical consequence is the
parsing rule
a named unary operator grabs its one argument greedily, and anything that would need more than one argument is not part of it.
So defined $x + 1 parses as defined($x) + 1, not as
defined($x + 1). This distinguishes them from list operators
(print, sort, push, …), which are at row 1/21 and slurp the rest
of the expression.
The named unary operators include defined, exists, ref, scalar,
length, uc, lc, ucfirst, lcfirst, chr, ord, hex, oct,
int, abs, sqrt, sin, cos, log, exp, rand, srand,
alarm, sleep, caller, wantarray, chroot, readlink,
umask, lstat, stat (when given one argument), and all the file
test operators (-e, -r, -f, -d, …).
defined $x + 1 # ( defined($x) ) + 1 — NOT defined($x+1)
length $s > 3 # ( length($s) ) > 3
-e $f && -r _ # both file tests take one arg; `&&` combines them
print "n=", $n # `print` is a list operator — slurps everything
When in doubt, write parentheses: defined($x + 1) is unambiguous.
PetaPerl-Specific Notes#
Parallelization#
Operators execute sequentially by default. PetaPerl’s parallelization applies at the loop/map/grep level, not individual operators.
Performance#
Bitwise operations compile to native machine instructions
String concatenation may allocate new memory (use
.=for efficiency)Numeric operations are optimized for integers and floats separately