Symbol Operators

You probably noticed the = assignment operator in some of the earlier examples:

$S2000 = "Hello, Polly.\n"
print $S2000

Standing alone, it’s the same as the PASM set opcode. In fact, if you run parrot in bytecode debugging mode (as in Section 11.4.2 in Chapter 11), you’ll see it really is just a set opcode underneath.

PIR has many other symbol operators: arithmetic, concatenation, comparison, bitwise, and logical. Many of these combine with assignment to produce the equivalent of a PASM opcode:

.local int sum
sum = $I42 + 5
print sum
print "\n"

The statement sum = $I42 + 5 translates to something like add I16, I17, 5.

PIR also provides +=, -=, >>=, . . . that map to the two-argument forms like add I16, I17.

Many PASM opcodes that return a single value also have an alternate syntax in PIR with the assignment operator:

$I0 = length str               # length $I0, str
$I0 = isa PerlInt, "scalar"    # isa $I0, PerlInt, "scalar"
$I0 = exists hash["key"]       # exists $I0, hash["key"]
$N0 = sin $N1
$N0 = atan $N1, $N2
$S0 = repeat "x", 20
$P0 = newclass "Foo"
 . . .

A complete list of PIR operators is available in Chapter 11. We’ll discuss the comparison operators in Section 10.5 later in this chapter.

Get Perl 6 and Parrot Essentials, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.