docs/user/pir/objects.pod - Using Objects in Parrot.
This document covers object oriented programming in PIR.
Before I begin talking about how to create classes and instantiate objects, I first need to talk about an intimately related subject: namespaces. Namespaces serve a twofold purpose, they allow you to group related routines together and they allow you to give several subroutines the same name but different, domain specific, implementations. These characteristics are, oddly enough, similar to the basic requirements for a class.
you may put all of your subroutines dealing with people in a
Person namespace and all of your subroutines dealing with computer programs in the
Both namespaces may have a subroutine called
run() but with radically different implementations.
Below is some code to illustrate this example:
As you might guess,
.namespace directive tells Parrot what namespace to group subroutines under.
A namespace ends when another
.namespace directive changes the namespace or when the end of the file is reached.
.namespace directive with no names in the brackets changes back to the root namespace.
Perl programmers will recognize that Parrot
.namespace declarations are just like Perl
albeit with different syntax.
But there are a few other differences.
I'll talk more about how Parrot uses namespaces and classes together in just a minute.
Creating classes in Parrot is relatively easy.
There are opcodes for it.
The easiest to start with is
newclass; just say
$P0 = newclass 'Foo' where $P0 is a PMC register,
and 'Foo' is the name of the class you want to create.
When you wish to instantiate objects that belong to the class you've created,
it's equally simple.
myobj = new "Foo" where
myobj is a PMC and "Foo" is the classname you've created with
Here's a simple example:
You may notice that I didn't use the return value of
That's only because this is a simple example.
:-) I'll talk about what to do with the return value of
newclass a little later.
let's talk about methods.
So now that I've created a
how do I add methods to it?
Remember before when I talked about namespaces?
that's the answer.
To add methods to a class,
you create a namespace with the same name as the class and then put your subroutines in that namespace.
PIR also provides a syntactic marker to let everyone know these subroutines are methods.
When declaring the subroutine,
:method modifier after the subroutine name.
Here's a familiar example to anyone who has read perlboot.
It's important to note that even though I've declared the namespaces and put subroutines in them,
this does not automatically create classes.
newclass declarations tell Parrot to create a class and as a side effect,
namespaces with the same name as the class may be used to store methods for that class.
One thing you may notice about method calls is that the method names are quoted.
Why is that?
If you would have left out the quotes,
then the identifier is assumed to be a declared
instead of writing:
you could also have written:
Another example of this is shown below.
So far I've talked about namespaces and creating classes and associating methods with those classes,
but what about storing data in the class?
Remember how the
newclass opcode returned a PMC that I didn't do anything to/with?
here's where it's used.
The PMC returned from
newclass is the handle by which you manipulate the class.
One such manipulation involves class "attributes".
Attributes are where you store your class-specific data.
Parrot has several opcodes for manipulating attributes; they are:
addattribute opcode lets you add a spot in the class for storing a particular value which may be get and set with
The only restriction on these values is that currently all attributes must be PMCs.
So, say I wanted to give my barnyard animals names (I'll illustrate with just one animal and you can infer how to do the same for the rest):
Whew! There's a lot of new stuff in this code. I'll take them starting from the top of the program and working towards the bottom.
One of the benefits of tagging your subroutines as methods is that they get a PMC named
self that represents the object they are acting on behalf of.
name method takes advantage of this to retrieve the attribute called "name" from the
self PMC and print it.
Immediately after I create the class called "Dog",
I use the PMC handle returned from
newclass to add an attribute called "name" to the class.
This just allocates a slot in the class for the value,
it does nothing more.
I create a new Dog and give it a name.
Because attributes may only be PMCs,
in order to give the Dog a name,
I first have to create a new String PMC (this is one of the PMCs builtin to Parrot) and assign the name I wish to give the dog to this PMC.
Then I can pass this PMC as a parameter to
setattribute to give my Dog a name.
Seems kind of complicated,
Especially when you think about doing this for each animal.
Each animal namespace would have an identical version of the
For each call to
newclass I'd need to also call
addattribute so that all of the animals may have a name.
Each time I wish to assign a name to an animal,
I'd first need to create a
String and call
setattribute on it.
Surely there's a better way?!? There is ...
You saw it coming didn't you?
What's object oriented programming without inheritance?
Parrot has an opcode
subclass that lets you inherit data and methods from an existing class.
We can use this ability to create a base class called "Animal" that contains the "name" attribute and two methods that are common to all animals:
to create new animals,
I just inherit from the Animal base class like so:
Each subclass will contain an attribute called "name" that can be used to store the name of the animal.
setname method abstracts out the process of creating a
String PMC and calling
setattribute on it.
And finally the
getname method becomes a wrapper around
I hope this gives you an idea of how to do object oriented programming using Parrot. The opcodes illustrated here are what any language implementor that targets Parrot would use to implement object oriented features in their language. Of course there are more opcodes for richer object oriented behavior available in Parrot. This article only covers the basics. For more information see parrot/docs/pdds/pdd15_objects.pod.
At the end of this article is a more complete listing of the program that gives my barnyard animals voices. There are many improvements that can be made to this code so take this opportunity to read and experiment and learn more about OOP in Parrot.
* Thanks to Randal Schwartz for providing a neat set of examples in perlboot from which this article shamelessly borrows. * Thanks to the Parrot people for feedback
Jonathan Scott Duff