Working with PMCs

In most of the examples we’ve shown so far, PMCs just duplicate the functionality of integers, numbers, and strings. They wouldn’t be terribly useful if that’s all they did, though. PMCs offer several advanced features, each with its own set of operations.

Aggregates

PMCs can define complex types that hold multiple values. These are commonly called " aggregates.” The most important feature added for aggregates is keyed access. Elements within an aggregate PMC can be stored and retrieved by a numeric or string key. PASM also offers a full set of operations for manipulating aggregate data types.

Since PASM is intended to implement Perl, the two most fully featured aggregates already in operation are arrays and hashes. Any aggregate defined for any language could take advantage of the features described here.

Arrays

The PerlArray PMC is an ordered aggregate with integer keys. The syntax for keyed access to a PMC puts the key in square brackets after the register name:

new P0, .PerlArray  # obtain a new array object
set P0, 2           # set its length 
set P0[0], 10       # set first element to 10
set P0[1], I31      # set second element to I31
set I0, P0[0]       # get the first element
set I1, P0          # get array length

A key on the destination register of a set operation sets a value for that key in the aggregate. A key on the source register of a set returns the value for that key. If you set P0 without a key, you set the length of the array, not one of its values.[38] And if you set an integer to ...

Get Perl 6 Essentials 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.