This post originated from an RSS feed registered with Ruby Buzz
by Jim Weirich.
Original Post: Perl 6 -- Apocalypse 12
Feed Title: { | one, step, back | }
Feed URL: http://onestepback.org/index.cgi/synopsis.rss
Feed Description: Jim Weirich's Blog on Software Development, Ruby, and whatever else sparks his interest.
In February 2001, Larry Wall came to Cincinnati to (amoung other things) give a talk on
the future directions that the Perl language would be taking in version 6.
I had switched to using Ruby (from Perl) about 6 months earlier at that
time, and I had a chance to talk to Larry about the changes in OO support
that he wanted in Perl. I remember him commenting that Perl5 made OO
possible, but not necessarily convenient.
In his Apocalypse series, Larry describes the direction Perl 6 will be
taking in several areas. Three years after that talk, Apocalypse
12 is available, and it addresses the details of Object Orientation in
Perl 6.
I just want to point out that all the examples in this blog entry are
directly from the apocalypse.
Objects in Perl 6
I skimmed the article last night and jotted down some notes. You will need
to read the apocalypse yourself for any substantial details (its a long
article, 20 pages in all). I’m just going touch on some highlights
and first impressions.
Objects in Perl 5 were built out of the existing language framework.
Blessing was the only new piece added to Perl to support objects, hash,
packages and namespaces were all existing Perl concepts. And although it
worked, it left the Object Model of Perl very open to variations.
Perl 6 adds more language support for direct support of objects, and leave
less to programmer choice. Here is a point object in Perl 6:
class Point {
has $.x;
has $.y is rw;
method clear () { $.x = 0; $.y = 0; }
}
Notice the keywords for class and the declaration of attributes $.x and
$.y. The $.x attribute is publicly readable, and the $.y is publically
writable as well. Private attributes can be declared with a colon instead
of a period (e.g. $:z).
Methods are part of the class declaration now and are syntactically
differentiated from normal subroutines by using the "method"
keyword.
ISA vs HASA
In OO circles it is said that HASA relationships should be modeled by
composition and ISA relations by inheritance. Although I find that saying
to be a bit simplistic, it is cool that Larry makes that connection
explicit by his choice of keywords. Did you notice in the above example
that the attributes were introduced by the keyword has?
So its not surprising that inheritance is introduced with the is
keyword. Here’s another simple example building on the previous one:
class Point3d is Point {
has $:z = 123;
method clear () { $:z = 0; next; }
}
The is keyword is overloaded (this is Perl after all).
Traits are also declared using the is keyword. That leads to
interesting code like:
class Moose is Mammal is stuffed is really(Hatrack) is spy(Russian) {...}
Roles
Although Perl 6 will support inheritance (single or multiple), it also
contains the idea of Roles. My impression of a Role is that is a
cross between Ruby modules and Java interfaces.
Roles look a lot like classes, but they cannot be instantiated. Roles
cannot inherit from classes, but they can be composed with other roles.
Here is an example role. Notice that the method feed depends upon
the method call should should be defined in the final objects.
role Pet {
method feed ($food) {
$food.open_can();
$food.put_in_bowl();
.call();
}
}
Roles are attached to classes with the does keyword:
class Dog {
is Mammal;
does Pet;
does Servant;
does Best::Friend[Man];
does Drool;
...
}
So far Roles look very much like Ruby modules. However, here is a twist. If
the methods in roles are only declared instead of defined, then the Role
specifies the methods expected to be implemented in the class. One final
example.
role Pet {
method feed ($food) {...}
method groom () {...}
method scratch (+$where) {...}
}
The {…} construction means that the body of the method is
defined elsewhere.
Stuff I Left Out
This Apocalypse runs 20 pages. Its obvious that I skipped over a lot of
material. It seems like the OO support in Perl 6 is much better than Perl
5. The object module is very flexible (a good thing), but also rather
complex (a not so good thing). Larry addresses some really interesting
problems (such as multiple dispatch), and it will be interesting to watch
to see if the features really work together or not.
Am I switching? No, not really. I really much prefer the much simplier
object model (and syntax!) provide by Ruby. But I will be watching.
Some Great Quotes
Larry is great for generating good quotes. I’ll end this entry with a
few quotes that struck me.
It has often been claimed that Perl 5 OO was "bolted on", but
that’s inaccurate. It was "bolted through", at right angles
to all other reference types, such that any reference could be blessed into
being an object. That’s way cool, but it’s often a little too
cool.
Here’s another one:
Some people will be surprised to hear it, but Perl is a minimalist
language at heart. It’s just minimalistic about weird things compared
to your average language.
And speaking of chosing a keyword to distinguish typing from subtyping:
On the other hand, a bit confusingly, it looks like subtyping will be
done with the "type" keyword, since we aren’t using that
word yet.