Many of Parrot's core classes -- such as
ResizablePMCArray -- are written in C,
but you can also write your own classes in PIR.
PIR doesn't have the shiny syntax of high-level object-oriented languages,
but it provides the necessary features to construct well-behaved objects every bit as powerful as those of high-level object systems.
Parrot developers often use the word "PMCs" to refer to the objects defined in C classes and "objects" to refer to the objects defined in PIR. In truth, all PMCs are objects and all objects are PMCs, so the distinction is a community tradition with no official meaning.
newclass opcode defines a new class.
It takes a single argument,
the name of the class to define.
Just as with Parrot's core classes,
new opcode instantiates a new object of a named class.
In addition to a string name for the class,
new can also instantiate an object from a class object or from a keyed namespace name.
addattribute opcode defines a named attribute -- or instance variable -- in the class:
setattribute opcode sets the value of a declared attribute.
You must declare an attribute before you may set it.
The value of an attribute is always a PMC,
never an integer,
or string.Though it can be an
getattribute opcode fetches the value of a named attribute.
It takes an object and an attribute name as arguments and returns the attribute PMC:
Because PMCs are containers,
you may modify an object's attribute by retrieving the attribute PMC and modifying its value.
You don't need to call
setattribute for the change to stick:
With a created class,
we can use the
new opcode to instantiate an object of that class in the same way we can instantiate a new PMC.
Or, if we don't have the class object handy, we can do it by name too:
PMCs have two VTABLE interface functions for dealing with instantiating a new object:
The former is called when a new PMC is created,
the later is called when a new PMC is created with an initialization argument.
Methods in PIR are subroutines stored in the class object.
Define a method with the
.sub directive and the
This method returns the integer value of the
bar attribute of the object divided by two.
Notice that the code never declares the named variable
Methods always make the invocant object -- the object on which the method was invoked -- available in a local variable called
:method modifier adds the subroutine to the class object associated with the currently selected namespace,
so every class definition file must contain a
Class files for languages may also contain an
.HLL declaration to associate the namespace with the appropriate high-level language:
Method calls in PIR use a period (
.) to separate the object from the method name.
The method name is either a literal string in quotes or a string variable.
The method call looks up the method in the invocant object using the string name:
You can also pass a method object to the method call instead of looking it up by string name:
Parrot always treats a PMC used in the method position as a method object,
so you can't pass a
String PMC as the method name.
Methods can have multiple arguments and multiple return values just like subroutines:
can opcode checks whether an object has a particular method.
It returns 0 (false) or 1 (true):
subclass opcode creates a new class that inherits methods and attributes from another class.
It takes two arguments: the name of the parent class and the name of the new class:
subclass can also take a class object as the parent class instead of a class name:
addparent opcode also adds a parent class to a subclass.
This is especially useful for multiple inheritance,
subclass opcode only accepts a single parent class:
To override an inherited method in the child class,
define a method with the same name in the subclass.
This example code overrides
who_am_i method to return a more meaningful name:
Object creation for subclasses is the same as for ordinary classes:
Calls to inherited methods are just like calls to methods defined in the class:
isa opcode checks whether an object is an instance of or inherits from a particular class.
It returns 0 (false) or 1 (true):
Object PMC is a core PMC written in C that provides basic object-like behavior.
Every object instantiated from a PIR class inherits a default set of vtable functions from
but you can override them with your own PIR subroutines.
:vtable modifier marks a subroutine as a vtable override.
As it does with methods,
Parrot stores vtable overrides in the class associated with the currently selected namespace:
Subroutines acting as vtable overrides must either have the name of an actual vtable function or include the vtable function name in the
You must call methods on objects explicitly,
but Parrot calls vtable functions implicitly in multiple contexts.
creating a new object with
$P3 = new 'Foo' will call
init with the new
As an example of some of the common vtable overrides,
= operator (or
set opcode) calls
Foo's vtable function
set_integer_native when its left-hand side is a
Foo object and the argument is an integer literal or integer variable:
+ operator (or
add opcode) calls
add vtable function when it adds two
inc opcode calls
increment vtable function when it increments a
get_string vtable functions to retrieve an integer or string value from a
Classes defined in PIR using the
newclass opcode are instances of the
This PMC contains all the meta-information for the class,
such as attribute definitions,
and its inheritance hierarchy.
inspect provides a way to peek behind the curtain of encapsulation to see what makes a class tick.
When called with no arguments,
inspect returns an associative array containing data on all characteristics of the class that it chooses to reveal:
When called with a string argument,
inspect only returns the data for a specific characteristic of the class:
Table 7-1 shows the introspection characteristics supported by