Chapter 6. Objects

Objects are encapsulated chunks of data and functionality. Over the years a host of concepts have sprung up around objects, such as data abstraction, code reuse, encapsulation, single and multiple inheritance, composition, delegation, mixins, polymorphism, and multiple dispatch. Every language with object-oriented syntax selects a subset of these ideas and combines them in different ways. With Perl 6 we want to provide a cleaner and simpler syntax for working with objects, but we also want to support a larger set of object-oriented ideas. Does that sound impossible? The solution is a classically Perl-ish one: make the easy things easy and the hard things possible.

With the release of Apocalypse 12, the syntax in this chapter is fairly solid, though it’s still likely to change somewhat before the final implementation. This chapter is only an introduction to the concepts. For complete coverage you should read the Apocalypse itself.

Using Objects

You can declare a class in one of two ways. The most familiar syntax for class declaration ends in a semicolon. The rest of the file after the declaration defines the class. With this syntax there can be only one class or module declaration in a file.

class Heart::Gold;
# class definition follows
 . . .

The other syntax for class declaration wraps the definition in a block. You can have as many of these as you like in a file, and even embed one class within the block of another.

class Heart::Gold { # class definition enclosed ...

Get Perl 6 and Parrot Essentials, Second Edition 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.