Parrot is a register-based virtual machine. It has four typed register sets -- integers, floating-point numbers, strings, and objects. All variables in PIR are one of these four types. When you work with register variables or named variables, you're actually working directly with register storage locations in the virtual machine.

If you've ever worked with an assembly language before, you may immediately jump to the conclusion that $I0 is the zeroth integer register in the register set, but Parrot is a bit smarter than that. The number of a register variable does not necessarily correspond to the register used internally; Parrot's compiler maps registers as appropriate for speed and memory considerations. The only guarantee Parrot gives you is that you'll always get the same storage location when you use $I0 in the same subroutine.


The most basic operation on a variable is assignment using the = operator:

  $I0 = 42        # set integer variable to the value 42
  $N3 = 3.14159   # set number variable to approximation of pi
  $I1 = $I0       # set $I1 to the value of $I0

The null opcode sets an integer or number variable to a zero value, and undefines a string or object.

  null $I0  # 0
  null $N0  # 0.0
  null $S0  # NULL
  null $P0  # PMCNULL

Working with Numbers

PIR has an extensive set of instructions that work with integers, floating-point numbers, and numeric PMCs. Many of these instructions have a variant that modifies the result in place:

  $I0 = $I1 + $I2
  $I0 += $I1

The first form of + stores the sum of the two arguments in the result variable, $I0. The second variant, +=, adds the single argument to $I0 and stores the sum back in $I0.

The arguments can be Parrot literals, variables, or constants. If the result is an integer type, like $I0, the arguments must also be integers. A number result, like $N0, usually requires number arguments, but many numeric instructions also allow the final argument to be an integer. Instructions with a PMC result may accept an integer, floating-point, or PMC final argument:

  $P0 = $P1 * $P2
  $P0 = $P1 * $I2
  $P0 = $P1 * $N2
  $P0 *= $P1
  $P0 *= $I1
  $P0 *= $N1

Unary numeric opcodes

Unary opcodes have a single argument. They either return a result or modify the argument in place. Some of the most common unary numeric opcodes are inc (increment), dec (decrement), abs (absolute value), neg (negate):

  $N0 = abs -5.0  # the absolute value of -5.0 is 5.0
  $I0 = 120
  inc $I1         # 120 incremented by 1 is 121

Binary numeric opcodes

Binary opcodes have two arguments and a result. Parrot provides addition (+ or add), subtraction (- or sub), multiplication (* or mul), division (/ or div), modulus (% or mod), and exponent (pow) opcodes, as well as gcd (greatest common divisor) and lcm (least common multiple).

  $I0 = 12 / 5
  $I0 = 12 % 5

Floating-point operations

The most common floating-point operations are ln (natural log), log2 (log base 2), log10 (log base 10), and exp (ex), as well as a full set of trigonometric opcodes such as sin (sine), cos (cosine), tan (tangent), sec (secant), sinh (hyperbolic sine), cosh (hyperbolic cosine), tanh (hyperbolic tangent), sech (hyperbolic secant), asin (arc sine), acos (arc cosine), atan (arc tangent), asec (arc secant), exsec (exsecant), hav (haversine), and vers (versine). All angle arguments for the trigonometric opcodes are in radians:

  .loadlib 'trans_ops'

  # ...

  $N0 = sin $N1
  $N0 = exp 2

The majority of the floating-point operations have a single argument and a single result. The arguments can generally be either an integer or number, but many of these opcodes require the result to be a number.

Logical and Bitwise Operations

The logical opcodes evaluate the truth of their arguments. They are most useful to make decisions for control flow. Integers and numeric PMCs are false if they're 0 and true otherwise. Strings are false if they're the empty string or a single character "0", and true otherwise. PMCs are true when their get_bool vtable function returns a nonzero value.

The and opcode returns the first argument if it's false and the second argument otherwise:

  $I0 = and 0, 1  # returns 0
  $I0 = and 1, 2  # returns 2

The or opcode returns the first argument if it's true and the second argument otherwise:

  .loadlib 'bit_ops'

  # ...

  $I0 = or 1, 0  # returns 1
  $I0 = or 0, 2  # returns 2

  $P0 = or $P1, $P2
Both C and C are short-circuiting ops. If they can determine what
value to return from the first argument, they'll never evaluate the second.
This is significant only for PMCs, as they might have side effects on
evaluation.The CX opcode returns the first argument if it is the only
true value, returns the second argument if it is the only true value, and
returns false if both values are true or both are false:
  $I0 = xor 1, 0  # returns 1
  $I0 = xor 0, 1  # returns 1
  $I0 = xor 1, 1  # returns 0
  $I0 = xor 0, 0  # returns 0
The CX opcode returns a true value when the argument is false and a false value if the argument is true:
  $I0 = not $I1
  $P0 = not $P1
X The bitwise opcodes operate on their values a single bit at a time. CX, CX, and CX return a value that is the logical AND, OR, or XOR of each bit in the source arguments. They each take two arguments.
  .loadlib 'bit_ops'

  # ...

  $I0 = bor $I1, $I2
  $P0 = bxor $P1, $I2
C, C, and C also have variants that modify the result in place.
  .loadlib 'bit_ops'

  # ...

  $I0 = band $I1
  $P0 = bor $P1
CX is the logical NOT of each bit in the source argument.
  .loadlib 'bit_ops'

  # ...

  $I0 = bnot $I1
X X X The logical and arithmetic shift operations shift their values by a specified number of bits:
  .loadlib 'bit_ops'

  # ...

  $I0 = shl $I1, $I2        # shift $I1 left by count $I2
  $I0 = shr $I1, $I2        # arithmetic shift right
  $P0 = lsr $P1, $P2        # logical shift right

Working with Strings

X Parrot strings are buffers of variable-sized data. The most common use of strings is to store text data. Strings can also hold binary or other non-textual data, though this is rare.N Parrot strings are flexible and powerful, to handle the complexity of human-readable (and computer-representable) text data. String operations work with string literals, variables, and constants, and with string-like PMCs.

Escape Sequences

X X Strings in double-quotes allow escape sequences using backslashes. Strings in single-quotes only allow escapes for nested quotes: $S0 = "This string is \n on two lines" $S0 = 'This is a \n one-line string with a slash in it' Table 4.1 shows the escape sequences Parrot supports in double-quoted strings.


X If you need more flexibility in defining a string, use a heredoc string literal. The CE> operator starts a heredoc. The string terminator immediately follows. All text until the terminator is part of the string. The terminator must appear on its own line, must appear at the beginning of the line, and may not have any trailing whitespace. $S2 = <<"End_Token" This is a multi-line string literal. Notice that it doesn't use quotation marks. End_Token

Concatenating strings

X<. operator> X Use the C<.> operator to concatenate strings. The following example concatenates the string "cd" onto the string "ab" and stores the result in C<$S1>.
  $S0 = "ab"
  $S1 = $S0 . "cd"  # concatenates $S0 with "cd"
  say $S1           # prints "abcd"
X<.= operator> Concatenation has a C<.=> variant to modify the result in place. In the next example, the C<.=> operation appends "xy" onto the string "abcd" in C<$S1>.
  $S1 .= "xy"       # appends "xy" to $S1
  say $S1           # prints "abcdxy"

Repeating strings

X The C opcode repeats a string a specified number of times:
  $S0 = "a"
  $S1 = repeat $S0, 5
  say $S1              # prints "aaaaa"
In this example, C generates a new string with "a" repeated five times and stores it in C<$S1>.

Length of a string

X The C opcode returns the length of a string in characters. This won't be the same as the length in I for multibyte encoded strings:
  $S0 = "abcd"
  $I0 = length $S0                # the length is 4
  say $I0
C has no equivalent for PMC strings.


The simplest version of the CX opcode takes three arguments: a source string, an offset position, and a length. It returns a substring of the original string, starting from the offset position (0 is the first character) and spanning the length:
  $S0 = substr "abcde", 1, 2        # $S0 is "bc"
This example extracts a two-character string from "abcde" at a one-character offset from the beginning of the string (starting with the second character). It generates a new string, "bc", in the destination register C<$S0>. When the offset position is negative, it counts backward from the end of the string. Thus an offset of -1 starts at the last character of the string. C no longer has a four-argument form, as in-place string operations have been removed. There is a C operator which will perform the replacement and return a new_string without modifying the old_string. The arguments are new_string, old_string, offset, count and replacement_string. The old_string is copied to the new_string with the replacement_string inserted from offset replacing the content for count characters. This example replaces the substring "bc" in C<$S1> with the string "XYZ", and returns "aXYZde" in C<$S0>, C<$S1> is not changed:
  $S1 = "abcde"
  $S0 = replace $S1, 1, 2, "XYZ"
  say $S0                        # prints "aXYZde"
  say $S1                        # prints "abcde"
When the offset position in a C is one character beyond the original string length, C appends the replacement string just like the concatenation operator. If the replacement string is an empty string, the opcode removes the characters from the original string in the new string.
  $S1 = "abcde"
  $S1 = replace $S1, 1, 2, "XYZ"
  say $S1                        # prints "aXYZde"

Converting characters

The CX opcode takes an integer value and returns the corresponding character in the ASCII character set as a one-character string. The CX opcode takes a single character string and returns the integer value of the character at the first position in the string. The integer value of the character will differ depending on the current encoding of the string:
  $S0 = chr 65              # $S0 is "A"
  $I0 = ord $S0             # $I0 is 65, if $S0 is ASCII/UTF-8
C has a two-argument variant that takes a character offset to select a single character from a multicharacter string. The offset must be within the length of the string:
  $I0 = ord "ABC", 2        # $I0 is 67
A negative offset counts backward from the end of the string, so -1 is the last character.
  $I0 = ord "ABC", -1       # $I0 is 67

Formatting strings

X The CX opcode generates a formatted string from a series of values. It takes two arguments: a string specifying the format, and an array PMC containing the values to be formatted. The format string and the result can be either strings or PMCs:
  $S0 = sprintf $S1, $P2
  $P0 = sprintf $P1, $P2
The format string is similar to C's C function with extensions for Parrot data types. Each format field in the string starts with a C<%> and ends with a character specifying the output format. Table 4.2 lists the available output format characters. Each format field supports several specifier options: R, R, R, and R. Table 4.3 lists the format flags. The R is a number defining the minimum width of the output from a field. The R is the maximum width for strings or integers, and the number of decimal places for floating-point fields. If either R or R is an asterisk (C<*>), it takes its value from the next argument in the PMC. The R modifier defines the type of the argument the field takes. Table 4.4 lists the size flags. The values in the aggregate PMC must have a type compatible with the specified R.
  $S0 = sprintf "int %#Px num %+2.3Pf\n", $P2
  say $S0       # prints "int 0x2a num +10.000"
The format string of this C example has two format fields. The first, C<%#Px>, extracts a PMC argument (C

) from the aggregate C<$P2> and formats it as a hexadecimal integer (C) with a leading 0x (C<#>). The second format field, C<%+2.3Pf>, takes a PMC argument (C

) and formats it as a floating-point number (C) with a minimum of two whole digits and a maximum of three decimal places (C<2.3>) and a leading sign (C<+>). The test files F and F have many more examples of format strings.

Joining strings

The CX opcode joins the elements of an array PMC into a single string. The first argument separates the individual elements of the PMC in the final string result.
  $P0 = new "ResizablePMCArray"
  push $P0, "hi"
  push $P0, 0
  push $P0, 1
  push $P0, 0
  push $P0, "parrot"
  $S0 = join "__", $P0
  say $S0                # prints "hi__0__1__0__parrot"
This example builds a C in C<$P0> with the values C<"hi">, C<0>, C<1>, C<0>, and C<"parrot">. It then joins those values (separated by the string C<"__">) into a single string stored in C<$S0>.

Splitting strings

Splitting a string yields a new array containing the resulting substrings of the original string. This example splits the string "abc" into individual characters and stores them in an array in C<$P0>. It then prints out the first and third elements of the array.
  $P0 = split "", "abc"
  $P1 = $P0[0]
  say $P1                # 'a'
  $P1 = $P0[2]
  say $P1                # 'c'

Testing for substrings

The CX opcode searches for a substring within a string. If it finds the substring, it returns the position where the substring was found as a character offset from the beginning of the string. If it fails to find the substring, it returns -1:
  $I0 = index "Beeblebrox", "eb"
  say $I0                           # prints 2
  $I0 = index "Beeblebrox", "Ford"
  say $I0                           # prints -1
C also has a three-argument version, where the final argument defines an offset position for starting the search.
  $I0 = index "Beeblebrox", "eb", 3
  say $I0                           # prints 5
This example finds the second "eb" in "Beeblebrox" instead of the first, because the search skips the first three characters in the string.

Bitwise Operations

The numeric bitwise opcodes also have string variants for AND, OR, and XOR: CX, CX, and CX. These take string or string-like PMC arguments and perform the logical operation on each byte of the strings to produce the result string. Remember that in-place string operations are no longer available.
  .loadlib 'bit_ops'

  # ...

  $P0 = bors $P1
  $P0 = bands $P1
  $S0 = bors $S1, $S2
  $P0 = bxors $P1, $S2
The bitwise string opcodes produce meaningful results only when used with simple ASCII strings, because Parrot performs bitwise operations per byte.


Strings use copy-on-write (COW)XX optimizations. A call to C<$S1 = $S0> doesn't immediately make a copy of C<$S0>, it only makes both variables point to the same string. Parrot doesn't make a copy of the string until one of two strings is modified.
  $S0 = "Ford"
  $S1 = $S0
  $S1 = "Zaphod"
  say $S0                # prints "Ford"
  say $S1                # prints "Zaphod"
Modifying one of the two variables causes Parrot to create a new string. This example preserves the existing value in C<$S0> and assigns the new value to the new string in C<$S1>. The benefit of copy-on-write is avoiding the cost of copying strings until the copies are necessary.

Encodings and Charsets

X X X Years ago, strings only needed to support the ASCII character set (or charset), a mapping of 128 bit patterns to symbols and English-language characters. This worked as long as everyone using a computer read and wrote English and only used a small handful of punctuation symbols. In other words, it was woefully insufficient. A modern string system must manage charsets in order to make sense out of all the string data in the world. A modern string system must also handle different encodings -- ways to represent various charsets in memory and on disk. Every string in Parrot has an associated encoding and character set. The default charset is 8-bit ASCII, which is almost universally supported. Double-quoted string constants can have an optional prefix specifying the string's encoding and charset.N Parrot tracks information about encoding and charset internally, and automatically converts strings when necessary to preserve these characteristics. Strings constants may have prefixes of the form C.
  $S0 = utf8:unicode:"Hello UTF-8 Unicode World!"
  $S1 = utf16:unicode:"Hello UTF-16 Unicode World!"
  $S2 = ascii:"This is 8-bit ASCII"
  $S3 = binary:"This is raw, unformatted binary data"
X X X X X Parrot supports the character sets C, C, C (Latin 1), and C and the encodings C, C, C, and C. The C charset treats the string as a buffer of raw unformatted binary data. It isn't really a string per se, because binary data contains no readable characters. This exists to support libraries which manipulate binary data that doesn't easily fit into any other primitive data type. When Parrot operates on two strings (as in concatenation or comparison), they must both use the same character set and encoding. Parrot will automatically upgrade one or both of the strings to the next highest compatible format as necessary. ASCII strings will automatically upgrade to UTF-8 strings if needed, and UTF-8 will upgrade to UTF-16. All of these conversions happen inside Parrot, so the programmer doesn't need to worry about the details.

Working with PMCs

X X Polymorphic Containers (PMCs) are the basis for complex data types and object-oriented behavior in Parrot. In PIR, any variable that isn't a low-level integer, number, or string is a PMC. PMC variables act much like the low-level variables, but you have to instantiate a new PMC object before you use it. The C opcode creates a new PMC object of the specified type.
  $P0 = new 'String'
  $P0 = "That's a bollard and not a parrot"
  say $P0
This example creates a C object, stores it in the PMC register variable C<$P0>, assigns it the value "That's a bollard and not a parrot", and prints it. Every PMC has a type that indicates what data it can store and what behavior it supports. The CX opcode reports the type of a PMC. When the result is a string variable, C returns the name of the type:
  $P0 = new "String"
  $S0 = typeof $P0               # $S0 is "String"
  say $S0                        # prints "String"
When the result is a PMC variable, C returns the C PMC for that object type.


X X In most of the examples shown so far, PMCs duplicate the behavior of integers, numbers, and strings. Parrot provides a set of PMCs for this exact purpose. C, C, and C are thin overlays on Parrot's low-level integers, numbers, and strings. A previous example showed a string literal assigned to a PMC variable of type C. Direct assignment of a literal to a PMC works for all the low-level types and their PMC equivalents:
  $P0 = new 'Integer'
  $P0 = 5

  $P1 = new 'String'
  $P1 = "5 birds"

  $P2 = new 'Float'
  $P2 = 3.14
X You may also assign non-constant low-level integer, number, or string registers directly to a PMC. The PMC handles the conversion from the low-level type to its own internal storage.N
  $I0 = 5
  $P0 = new 'Integer'
  $P0 = $I0

  $S1 = "5 birds"
  $P1 = new 'String'
  $P1 = $S1

  $N2 = 3.14
  $P2 = new 'Float'
  $P2 = $N2
The C opcode is a handy shortcut to create the appropriate PMC object from an integer, number, or string literal or variable.
  $P0 = box 3    # $P0 is an "Integer"

  $P1 = box $S1  # $P1 is a "String"

  $P2 = box 3.14 # $P2 is a "Float"
X In the reverse situation, when assigning a PMC to an integer, number, or string variable, the PMC also has the ability to convert its value to the low-level type.N
  $P0 = box 5
  $S0 = $P0           # the string "5"
  $N0 = $P0           # the number 5.0
  $I0 = $P0           # the integer 5

  $P1 = box "5 birds"
  $S1 = $P1           # the string "5 birds"
  $I1 = $P1           # the integer 5
  $N1 = $P1           # the number 5.0

  $P2 = box 3.14
  $S2 = $P2           # the string "3.14"
  $I2 = $P2           # the integer 3
  $N2 = $P2           # the number 3.14
This example creates CX, CX, and CX PMCs, and shows the effect of assigning each one back to a low-level type. Converting a string to an integer or number only makes sense when the contents of the string are a number. The C PMC will attempt to extract a number from the beginning of the string, but otherwise will return a false value.


X X PMCs can define complex types that hold multiple values, commonly called aggregates. Two basic aggregate types are ordered arrays and associative arrays. The primary difference between these is that ordered arrays use integer keys for indexes and associative arrays use string keys. Aggregate PMCs support the use of numeric or string keys. PIR also offers a extensive set of operations for manipulating aggregate data types.

Ordered Arrays

X X Parrot provides several ordered array PMCs, differentiated by whether the array should store booleans, integers, numbers, strings, or other PMCs, and whether the array should maintain a fixed size or dynamically resize for the number of elements it stores. The core array types are C, C, C, C, C, C, C, C, C, and C. The array types that start with "Fixed" have a fixed size and do not allow elements to be added outside their allocated size. The "Resizable" variants automatically extend themselves as more elements are added.N The array types that include "String", "Integer", or "Boolean" in the name use alternate packing methods for greater memory efficiency. Parrot's core ordered array PMCs all have zero-based integer keys. Extracting or inserting an element into the array uses PIR's standard key syntax, with the key in square brackets after the variable name. An lvalue key sets the value for that key. An rvalue key extracts the value for that key in the aggregate to use as the argument value:
  $P0    = new "ResizablePMCArray" # create a new array object
  $P0[0] = 10                      # set first element to 10
  $P0[1] = $I31                    # set second element to $I31
  $I0    = $P0[0]                  # get the first element
Setting the array to an integer value directly (without a key) sets the number of elements of the array. Assigning an array directly to an integer retrieves the number of elements of the array.
  $P0 = 2    # set array size
  $I1 = $P0  # get array size
This is equivalent to using the C opcode to retrieve the number of items currently in an array:
  elements $I0, $P0 # get element count
Some other useful instructions for working with ordered arrays are C, C, C, and C, to add or remove elements. C and C work on the end of the array, the highest numbered index. C and C work on the start of the array, adding or removing the zeroth element, and renumbering all the following elements.
  push $P0, 'banana' # add to end
  $S0 = pop $P0      # fetch from end

  unshift $P0, 74    # add to start
  $I0 = shift $P0    # fetch from start

Associative Arrays

X X X An associative array is an unordered aggregate that uses string keys to identify elements. You may know them as "hash tables", "hashes", "maps", or "dictionaries". Parrot provides one core associative array PMC, called C. String keys work very much like integer keys. An lvalue key sets the value of an element, and an rvalue key extracts the value of an element. The string in the key must always be in single or double quotes.
  new $P1, "Hash"          # create a new associative array
  $P1["key"] = 10          # set key and value
  $I0        = $P1["key"]  # get value for key
Assigning a CX PMC (without a key) to an integer result fetches the number of elements in the hash.N PMC directly to an integer value.>
  $I1 = $P1         # number of entries
The CX opcode tests whether a keyed value exists in an aggregate. It returns 1 if it finds the key in the aggregate and 0 otherwise. It doesn't care if the value itself is true or false, only that an entry exists for that key:
  new $P0, "Hash"
  $P0["key"] = 0
  exists $I0, $P0["key"] # does a value exist at "key"?
  say $I0                # prints 1
The CX opcode removes an element from an associative array:
  delete $P0["key"]


X X An iterator extracts values from an aggregate PMC one at a time. Iterators are most useful in loops which perform an action on every element in an aggregate. The C opcode creates a new iterator from an aggregate PMC. It takes one argument, the PMC over which to iterate:
  $P1 = iter $P2
The CX opcode extracts the next value from the iterator.
      $P5 = shift $P1
Evaluating the iterator PMC as a boolean returns whether the iterator has reached the end of the aggregate:
      if $P1 goto iter_repeat
Parrot provides predefined constants for working with iterators. C<.ITERATE_FROM_START> and C<.ITERATE_FROM_END> constants select whether an ordered array iterator starts from the beginning or end of the array. These two constants have no effect on associative array iterators, as their elements are unordered. Load the iterator constants with the C<.include>X<.include directive> directive to include the file F. To use them, set the iterator PMC to the value of the constant:
      .include "iterator.pasm"

      # ...

With all of those separate pieces in one place, this example loads the iterator constants, creates an ordered array of "a", "b", "c", creates an iterator from that array, and then loops over the iterator using a conditional C to checks the boolean value of the iterator and another unconditional C:
      .include "iterator.pasm"
      $P2 = new "ResizablePMCArray"
      push $P2, "a"
      push $P2, "b"
      push $P2, "c"

      $P1 = iter $P2

      unless $P1 goto iter_end
      $P5 = shift $P1
      say $P5                        # prints "a", "b", "c"
      goto iter_loop
Associative array iterators work similarly to ordered array iterators. When iterating over associative arrays, the C opcode extracts keys instead of values. The key looks up the value in the original hash PMC.
      $P2      = new "Hash"
      $P2["a"] = 10
      $P2["b"] = 20
      $P2["c"] = 30

      $P1      = iter $P2

      unless $P1 goto iter_end
      $S5 = shift $P1          # the key "a", "b", or "c"
      $I9 = $P2[$S5]           # the value 10, 20, or 30
      say $I9
      goto iter_loop
This example creates an associative array C<$P2> that contains three keys "a", "b", and "c", assigning them the values 10, 20, and 30. It creates an iterator (C<$P1>) from the associative array using the C opcode, and then starts a loop over the iterator. At the start of each loop, the C instruction checks whether the iterator has any more elements. If there are no more elements, C jumps to the end of the loop, marked by the label C. If there are more elements, the C opcode extracts the next key. Keyed assignment stores the integer value of the element indexed by the key in C<$I9>. After printing the integer value, C jumps back to the start of the loop, marked by C.

Multi-level Keys

X X Aggregates can hold any data type, including other aggregates. Accessing elements deep within nested data structures is a common operation, so PIR provides a way to do it in a single instruction. Complex keys specify a series of nested data structures, with each individual key separated by a semicolon.
  $P0           = new "Hash"
  $P1           = new "ResizablePMCArray"
  $P1[2]        = 42
  $P0["answer"] = $P1

  $I1 = 2
  $I0 = $P0["answer";$I1]
  say $I0
This example builds up a data structure of an associative array containing an ordered array. The complex key C<["answer"; $I1]> retrieves an element of the array within the hash. You can also set a value using a complex key:
  $P0["answer";0] = 5
The individual keys are integer or string literals, or variables with integer or string values.

Copying and Cloning

X PMC registers don't directly store the data for a PMC, they only store a pointer to the structure that stores the data. As a result, the C<=> operator doesn't copy the entire PMC, it only copies the pointer to the PMC data. If you later modify the copy of the variable, it will also modify the original.
  $P0 = new "String"
  $P0 = "Ford"
  $P1 = $P0
  $P1 = "Zaphod"
  say $P0                # prints "Zaphod"
  say $P1                # prints "Zaphod"
In this example, C<$P0> and C<$P1> are both pointers to the same internal data structure. Setting C<$P1> to the string literal "Zaphod", it overwrites the previous value "Ford". Both C<$P0> and C<$P1> refer to the C PMC "Zaphod". The C X opcode makes a deep copy of a PMC, instead of copying the pointer like C<=>X<= operator> does.
  $P0 = new "String"
  $P0 = "Ford"
  $P1 = clone $P0
  $P0 = "Zaphod"
  say $P0        # prints "Zaphod"
  say $P1        # prints "Ford"
This example creates an identical, independent clone of the PMC in C<$P0> and puts it in C<$P1>. Later changes to C<$P0> have no effect on the PMC in C<$P1>.N are copy-on-writeX exactly the same as the copy created by C<=>.> To assign the I of one PMC to another PMC that already exists, use the CX opcode:
  $P0 = new "Integer"
  $P1 = new "Integer"
  $P0 = 42
  assign $P1, $P0    # note: $P1 must exist already
  inc $P0
  say $P0            # prints 43
  say $P1            # prints 42
This example creates two C PMCs, C<$P1> and C<$P2>, and gives the first one the value 42. It then uses C to pass the same integer value on to C<$P1>. Though C<$P0> increments, C<$P1> doesn't change. The result for C must have an existing object of the right type in it, because C neither creates a new duplicate object (as does C) or reuses the source object (as does C<=>).


X X PMCs can have additional values attached to them as "properties" of the PMC. Most properties hold extra metadata about the PMC. The CX opcode sets the value of a named property on a PMC. It takes three arguments: the PMC on which to set a property, the name of the property, and a PMC containing the value of the property.
  setprop $P0, "name", $P1
The CX opcode returns the value of a property. It takes two arguments: the name of the property and the PMC from which to retrieve the property value.
  $P2 = getprop "name", $P0
This example creates a C object in C<$P0> and an C object with the value 1 in C<$P1>. C sets a property named "eric" on the object in C<$P0> and gives the property the value of C<$P1>. C retrieves the value of the property "eric" on C<$P0> and stores it in C<$P2>.
  $P0 = new "String"
  $P0 = "Half-a-Bee"
  $P1 = new "Integer"
  $P1 = 1

  setprop $P0, "eric", $P1  # set a property on $P0
  $P2 = getprop "eric", $P0 # retrieve a property from $P0

  say $P2                   # prints 1
Parrot stores PMC properties in an associative array where the name of the property is the key. CX deletes a property from a PMC.
  delprop $P1, "constant" # delete property
You can fetch a complete hash of all properties on a PMC with CX:
  $P0 = prophash $P1 # set $P0 to the property hash of $P1
Fetching the value of a non-existent property returns an C PMC.

Vtable Functions

X You may have noticed that a simple operation sometimes has a different effect on different PMCs. Assigning a low-level integer value to a C PMC sets its integer value of the PMC, but assigning that same integer to an ordered array sets the size of the array. Every PMC defines a standard set of low-level operations called vtable functions. When you perform an assignment like: $P0 = 5 ... Parrot calls the C vtable function on the PMC referred to by register C<$P0>. X Parrot has a fixed set of vtable functions, so that any PMC can stand in for any other PMC; they're polymorphic.N Every PMC defines some behavior for every vtable function. The default behavior is to throw an exception reporting that the PMC doesn't implement that vtable function. The full set of vtable functions for a PMC defines the PMC's basic interface, but PMCs may also define methods to extend their behavior beyond the vtable set.


X X Parrot performs operations on variables stored in small register sets local to each subroutine. For more complex tasks,N<...and for most high-level languages that Parrot supports.> it's also useful to have variables that live beyond the scope of a single subroutine. These variables may be global to the entire program or restricted to a particular library. Parrot stores long-lived variables in a hierarchy of namespaces. The opcodes CX and CX store and fetch a variable in a namespace:
  $P0 = new "String"
  $P0 = "buzz, buzz"
  set_global "bee", $P0
  # ...
  $P1 = get_global "bee"
  say $P1                        # prints "buzz, buzz"
The first two statements in this example create a C PMC in C<$P0> and assign it a value. In the third statement, C stores that PMC as the named global variable C. At some later point in the program, C retrieves the global variable by name, and stores it in C<$P1> to print. Namespaces can only store PMC variables. Parrot boxes all primitive integer, number, or string values into the corresponding PMCs before storing them in a namespace. The name of every variable stored in a particular namespace must be unique. You can't have store both an C PMC and an array PMC both named "bee", stored in the same namespace.N, C<@bee>....>

Namespace Hierarchy

X X A single global namespace would be far too limiting for most languages or applications. The risk of accidental collisions -- where two libraries try to use the same name for some variable -- would be quite high for larger code bases. Parrot maintains a collection of namespaces arranged as a tree, with the C namespace as the root. Every namespace you declare is a child of the C namespace (or a child of a child....). The C and C opcodes both have alternate forms that take a key name to access a variable in a particular namespace within the tree. This code example stores a variable as C in the Duck namespace and retrieves it again:
  set_global ["Duck"], "bill", $P0
  $P1 = get_global ["Duck"], "bill"
The key name for the namespace can have multiple levels, which correspond to levels in the namespace hierarchy. This example stores a variable as C in the Electric namespace under the General namespace in the hierarchy.
  set_global ["General";"Electric"], "bill", $P0
  $P1 = get_global ["General";"Electric"], "bill"
X X The C and C opcode operate on the currently selected namespace. The default top-level namespace is the "root" namespace. The C<.namespace>X<.namespace directive> directive allows you to declare any namespace for subsequent code. If you select the General Electric namespace, then store or retrieve the C variable without specifying a namespace, you will work with the General Electric bill, not the Duck bill. .namespace ["General";"Electric"] #... set_global "bill", $P0 $P1 = get_global "bill" Passing an empty key to the C<.namespace> directive resets the selected namespace to the root namespace. The brackets are required even when the key is empty. .namespace [ ] When you need to be absolutely sure you're working with the root namespace regardless of what namespace is currently active, use the CX and CX opcodes instead of C and C. This example sets and retrieves the variable C in the Dollar namespace, which is directly under the root namespace:
  set_root_global ["Dollar"], "bill", $P0
  $P1 = get_root_global ["Dollar"], "bill"
X X To prevent further collisions, each high-level language running on Parrot operates within its own virtual namespace root. The default virtual root is C, and the C<.HLL>X<.HLL directive> directive (for Iigh-Ievel Ianguage) selects an alternate virtual root for a particular high-level language: .HLL 'ruby' The CX and CX opcodes are like C and C, except they always operate on the virtual root for the currently selected HLL. This example stores and retrieves a C variable in the Euro namespace, under the Dutch HLL namespace root: .HLL 'Dutch' #... set_hll_global ["Euro"], "bill", $P0 $P1 = get_hll_global ["Euro"], "bill"

NameSpace PMC

X Namespaces are just PMCs. They implement the standard vtable functions and a few extra methods. The CX opcode retrieves the currently selected namespace as a PMC object: $P0 = get_namespace The CX opcode retrieves the namespace object for the root namespace. The CX opcode retrieves the virtual root for the currently selected HLL. $P0 = get_root_namespace $P0 = get_hll_namespace Each of these three opcodes can take a key argument to retrieve a namespace under the currenly selected namespace, root namespace, or HLL root namespace: $P0 = get_namespace ["Duck"] $P0 = get_root_namespace ["General";"Electric"] $P0 = get_hll_namespace ["Euro"] Once you have a namespace object you can use it to retrieve variables from the namespace instead of using a keyed lookup. This example first looks up the Euro namespace in the currently selected HLL, then retrieves the C variable from that namespace: $P0 = get_hll_namespace ["Euro"] $P1 = get_global $P0, "bill" Namespaces also provide a set of methods to provide more complex behavior than the standard vtable functions allow. The CX method returns the name of the namespace as a C: $P3 = $P0.'get_name'() The CX method retrieves a namespace object for the parent namespace that contains this one: $P5 = $P0.'get_parent'() The CX method retrieves any Class PMC associated with the namespace: $P6 = $P0.'get_class'() The CX and CX methods store and retrieve variables in a namespace in a language-neutral way: $P0.'add_var'("bee", $P3) $P1 = $P0.'find_var'("bee") The CX method looks up a namespace, just like the C opcode: $P1 = $P0.'find_namespace'("Duck") The CX method adds a new namespace as a child of the namespace object: $P0.'add_namespace'($P1) The CX method looks up a namespace as a child of the namespace object and returns it. If the requested namespace doesn't exist, C creates a new one and adds it under that name: $P1 = $P0.'make_namespace'("Duck")


X Just like regular assignment, the various operations to store a variable in a namespace only store a pointer to the PMC. If you modify the local PMC after storing in a namespace, those changes will also appear in the stored global. To store a true copy of the PMC, C it before you store it. Leaving the global variable as an alias for a local variable has its advantages. If you retrieve a stored global into a register and modify it:
  $P1 = get_global "feather"
  inc $P1
... you modify the value of the stored global, so you don't need to call C again.