O'Reilly logo

Perl 6 Essentials by Leopold Tötsch, Dan Sugalski, Allison Randal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required