NAME/НАИМЕНОВАНИЕ
perlootut - учебник Объектно-Ориентированного программирования на Perl
DATE
Этот документ был создан в феврале 2011 года, и последний крупный пересмотр был в феврале 2013 года.
Если вы читаете это в будущем, то вполне возможно, что положение дел изменилось. Мы рекомендуем вам начать чтение perlootut последнего стабильного релиза Perl вместо этой версии.
ОПИСАНИЕ
Этот документ представляет собой введение в объектно ориентированное программирование на Perl. Она начинается с краткого обзора концепции объектно-ориентированного проектирования. Затем он рассказывает о нескольких различных OO системах из CPAN , которые являются вершиной того, что может предоставить Perl.
По умолчанию встроенная в Perl OO система является минималистичной, она оставляет вам делать большую часть работы. Этот минимализм имел много смысла в 1994 году, но через годы, начиная с Perl 5.0 мы увидели появление ряда общих шаблонов в Perl OO. К счастью гибкость Perl позволяет создавать богатые и процветающие экосистемы OO Perl.
Если вы хотите знать, как Perl OO работает под капотом, в документе perlobj объясняются все мелкие детали.
Этот документ предполагает, что вы уже понимаете основы синтаксиса Perl, типы переменных, операторов и вызовов подпрограмм. Если вы не понимаете эти концепции еще, пожалуйста, прочитайте perlintro сначала. Вы также должны прочитать документы perlsyn, perlop, и perlsub.
ОСНОВЫ ОБЪЕКТНО ОРИЕНТИРОВАННОСТИ
Большинство объектных систем имеют ряд общих концепций. Вы, вероятно, ранее слышали такие термины, как "класс", "объект", "метод" и "атрибут". Понимание концепций позволяет гораздо легче читать и писать объектно ориентированный код. Если вы уже знакомы с этими терминами, то должны пролететь этот раздел, так как он объясняет каждое понятие OO Perl с точки зрения его реализации в языке.
Система OO Perl основана на классе. Классовое основание ОО систем является довольно распространенным явлением. Оно используется в Java, C++, C#, Python, Ruby и многими другими языками. Также существуют другие парадигмы объектно-ориентированности. JavaScript — это наиболее популярный язык для использования в другой парадигме. Системы OO JavaScript основаны на прототипах.
Объект
Объект — это структура данных, которая объединяет вместе данные и подпрограммы, которые обрабатывают эти данные. Данные объекта называются атрибутами, а его подпрограммы, называются методами. Объект можно рассматривать как существительное (человек, веб-служба, компьютер).
Объект представляет одну дискретных вещь. Например, объект может представлять файл. Атрибуты файла-объекта могут включать путь,содержание и последние изменения. Если мы создали объект для представления /etc/hostname на машине с именем "foo.example.com", то путь объекта будет "/etc/hostname", его содержание будет "foo\n", и время последнего изменения будет 1304974868 секунд, прошедших с начала эпохи.
Методы, связанные с файлом могут включать rename() и write().
В Perl большинство объектов являются хэшами, но в OO системах, которые мы рекомендуем, вам не будет необходимости беспокоиться об этом. На практике это лучше рассматривать, как внутреннюю непрозрачную структуру данных объекта.
Класс
Класс определяет поведение категории объектов. Класс является именем категории (например, "Файл"), и класс также определяет поведение объектов в данной категории.
Все объекты принадлежат к определенному классу. Например наш /etc/hostname объект принадлежит к классу File . Когда мы хотим создать определенный объект, мы начинаем с его класса и создаем или инициализируем (instantiate) экземпляр объекта. Конкретный объект часто именуется как экземпляр (instance) класса.
В Perl любой пакет может быть классом. Разница между пакетом, который является классом и пакетом не являющимся классом состоит в том, как используется пакет. Вот наше "объявление класса" для класса File:
package File;
В Perl Существует нет специального ключевого слова для построения объекта. Однако большинство OO модулей на CPAN использовать метод с именем new(), чтобы создать новый объект:
my $hostname = File->new(
path => '/etc/hostname',
content => "foo\n",
last_mod_time => 1304974868,
);
(Не беспокойтесь оператор -> будет объяснен позже.)
Благословение (Blessing)
Как мы уже говорили ранее, большинство Perl объектов хэши, но объект может быть экземпляром любого типа данных Perl (скалярами, массивами, и т.д.). Превращение структуру простых данных в объект осуществляется благословением blessing() данной структуры с использованием Perl функции bless.
Хотя мы настоятельно рекомендуем вам не строить объекты с нуля, вы должны знать, термин, благословлять (bless). Благословленная (<blessed>) структура данных (ака "ссылка") — это объект. Мы иногда говорим, что объект был "благословлен в класс" ("blessed into a class").
После того, как ссылка была благословлена, функция blessed из модуля ядра Scalar::Util может сказать нам имя ее класса. Эта подпрограмма возвращает объект класса, если она видит, что передали объект и ложь (false) в противном случае.
use Scalar::Util 'blessed';
print blessed($hash); # undef
print blessed($hostname); # File
Конструктор
Конструктор создает новый объект. В Perl конструктор класса — это просто еще один метод, в отличие от некоторых других языков, которые предоставляют синтаксис для конструкторов. Большинство классов Perl использовать new как имя для их конструктора:
my $file = File->new(...);
Методы
Вы уже узнали, что методом является подпрограмма, которая работает с объектом. Вы можете думать о методе как о вещи, которую объект может делать. Если объект является существительным, то методы будут его глаголами (сохранить, печатать, открывать).
В Perl методы являются просто подпрограммами, которые живут в классе пакета. Методы всегда записываются так, чтобы получить объект в качестве первого аргумента.
sub print_info {
my $self = shift;
print "This file is at ", $self->path, "\n";
}
$file->print_info;
# The file is at /etc/hostname
Что делает метод специальным так это то, как он вызывается. Оператора стрелка (->) говорит Perl, что мы вызываем метод.
Когда мы делаем вызов метода, Perl делает так, что в качестве первого аргумента передается вызыватель (invocant). вызыватель (Invocant) это причудливое название для вещи с левой стороны стрелки. Вызывателем (Invocant) может быть или имя класса или имя объекта. Мы также можем передать дополнительные аргументы в метод:
sub print_info {
my $self = shift;
my $prefix = shift // "This file is at ";
print $prefix, ", ", $self->path, "\n";
}
$file->print_info("The file is located at ");
# The file is located at /etc/hostname
Атрибуты
Каждый класс может определить свои атрибуты. Когда мы создаем экземпляр объекта, мы присваиваем значения этим атрибутам. Например каждый объект File имеет путь. Атрибуты, иногда называются свойствами (properties).
Perl не имеет специального синтаксиса для атрибутов. Под капотом атрибуты часто хранятся в виде ключей в базовом хэше объекта, но не беспокойтесь об этом.
Мы рекомендуем, что вы имели доступ к атрибутам только через методы доступа ( accessor methods). Это методы, с помощью которых можно получить или задать значение каждого атрибута. Мы видели это раньше в примере print_info(), который вызывает <$self-path >>.
Вы также можете знать термины геттер и сеттер (getter и setter). Это два типа доступа. Геттер получает значение атрибута, в то время как сеттер устанавливает его. Еще один термин для сеттера это мутатор (mutator)
Атрибуты обычно определяются как только для чтения (read-only) или для чтения и записи ( read-write). Атрибуты только для чтения(read-only) устанавливаются только, когда создается объект, в то время как атрибуты чтения-записи(read-write) могут быть изменены в любое время.
Значением атрибута может быть другой объект. Например вместо возвращения времени последней модификации (mod time) в виде числа, класс File может вернуть объект DateTime, представляющий это значение.
Это позволяет иметь класс, который не предоставляет каких-либо публично настраиваемых атрибутов. Не каждый класс имеет атрибуты и методы.
Полиморфизм
Полиморфизм -это причудливый способ сказать, что объекты из двух различных классов используют единое API. Например мы могли бы иметь классы File и WebPage , которые оба имеют метод print_content(). Этот метод может создавать разные выходные данные для каждого класса, но они имеют общий интерфейс (common interface).
В то время как два класса могут отличаться во многих отношениях, но, когда речь заходит о методе print_content(), то они одинаковы. Это означает, что мы можем попытаться вызвать метод print_content() на объекте класса, и нам не нужно знать, какому классу принадлежит объект!
Полиморфизм — одна из ключевых концепций объектно ориентированного проектирования(object-oriented design).
Наследование
Inheritance lets you create a specialized version of an existing class. Inheritance lets the new class reuse the methods and attributes of another class.
For example, we could create an File::MP3 class which inherits from File. An File::MP3 is-a more specific type of File. All mp3 files are files, but not all files are mp3 files.
We often refer to inheritance relationships as parent-child or superclass/subclass relationships. Sometimes we say that the child has an is-a relationship with its parent class.
File is a superclass of File::MP3, and File::MP3 is a subclass of File.
package File::MP3;
use parent 'File';
The parent module is one of several ways that Perl lets you define inheritance relationships.
Perl allows multiple inheritance, which means that a class can inherit from multiple parents. While this is possible, we strongly recommend against it. Generally, you can use roles to do everything you can do with multiple inheritance, but in a cleaner way.
Note that there's nothing wrong with defining multiple subclasses of a given class. This is both common and safe. For example, we might define File::MP3::FixedBitrate and File::MP3::VariableBitrate classes to distinguish between different types of mp3 file.
Overriding methods and method resolution
Inheritance allows two classes to share code. By default, every method in the parent class is also available in the child. The child can explicitly override a parent's method to provide its own implementation. For example, if we have an File::MP3 object, it has the print_info() method from File:
my $cage = File::MP3->new(
path => 'mp3s/My-Body-Is-a-Cage.mp3',
content => $mp3_data,
last_mod_time => 1304974868,
title => 'My Body Is a Cage',
);
$cage->print_info;
# The file is at mp3s/My-Body-Is-a-Cage.mp3
If we wanted to include the mp3's title in the greeting, we could override the method:
package File::MP3;
use parent 'File';
sub print_info {
my $self = shift;
print "This file is at ", $self->path, "\n";
print "Its title is ", $self->title, "\n";
}
$cage->print_info;
# The file is at mp3s/My-Body-Is-a-Cage.mp3
# Its title is My Body Is a Cage
The process of determining what method should be used is called method resolution. What Perl does is look at the object's class first (File::MP3 in this case). If that class defines the method, then that class's version of the method is called. If not, Perl looks at each parent class in turn. For File::MP3, its only parent is File. If File::MP3 does not define the method, but File does, then Perl calls the method in File.
If File inherited from DataSource, which inherited from Thing, then Perl would keep looking "up the chain" if necessary.
It is possible to explicitly call a parent method from a child:
package File::MP3;
use parent 'File';
sub print_info {
my $self = shift;
$self->SUPER::print_info();
print "Its title is ", $self->title, "\n";
}
The SUPER:: bit tells Perl to look for the print_info() in the File::MP3 class's inheritance chain. When it finds the parent class that implements this method, the method is called.
We mentioned multiple inheritance earlier. The main problem with multiple inheritance is that it greatly complicates method resolution. See perlobj for more details.
Encapsulation
Encapsulation is the idea that an object is opaque. When another developer uses your class, they don't need to know how it is implemented, they just need to know what it does.
Encapsulation is important for several reasons. First, it allows you to separate the public API from the private implementation. This means you can change that implementation without breaking the API.
Second, when classes are well encapsulated, they become easier to subclass. Ideally, a subclass uses the same APIs to access object data that its parent class uses. In reality, subclassing sometimes involves violating encapsulation, but a good API can minimize the need to do this.
We mentioned earlier that most Perl objects are implemented as hashes under the hood. The principle of encapsulation tells us that we should not rely on this. Instead, we should use accessor methods to access the data in that hash. The object systems that we recommend below all automate the generation of accessor methods. If you use one of them, you should never have to access the object as a hash directly.
Composition
In object-oriented code, we often find that one object references another object. This is called composition, or a has-a relationship.
Earlier, we mentioned that the File class's last_mod_time accessor could return a DateTime object. This is a perfect example of composition. We could go even further, and make the path and content accessors return objects as well. The File class would then be composed of several other objects.
Roles
Roles are something that a class does, rather than something that it is. Roles are relatively new to Perl, but have become rather popular. Roles are applied to classes. Sometimes we say that classes consume roles.
Roles are an alternative to inheritance for providing polymorphism. Let's assume we have two classes, Radio and Computer. Both of these things have on/off switches. We want to model that in our class definitions.
We could have both classes inherit from a common parent, like Machine, but not all machines have on/off switches. We could create a parent class called HasOnOffSwitch, but that is very artificial. Radios and computers are not specializations of this parent. This parent is really a rather ridiculous creation.
This is where roles come in. It makes a lot of sense to create a HasOnOffSwitch role and apply it to both classes. This role would define a known API like providing turn_on() and turn_off() methods.
Perl does not have any built-in way to express roles. In the past, people just bit the bullet and used multiple inheritance. Nowadays, there are several good choices on CPAN for using roles.
When to Use OO
Object Orientation is not the best solution to every problem. In Perl Best Practices (copyright 2004, Published by O'Reilly Media, Inc.), Damian Conway provides a list of criteria to use when deciding if OO is the right fit for your problem:
The system being designed is large, or is likely to become large.
The data can be aggregated into obvious structures, especially if there's a large amount of data in each aggregate.
The various types of data aggregate form a natural hierarchy that facilitates the use of inheritance and polymorphism.
You have a piece of data on which many different operations are applied.
You need to perform the same general operations on related types of data, but with slight variations depending on the specific type of data the operations are applied to.
It's likely you'll have to add new data types later.
The typical interactions between pieces of data are best represented by operators.
The implementation of individual components of the system is likely to change over time.
The system design is already object-oriented.
Large numbers of other programmers will be using your code modules.
PERL OO SYSTEMS
As we mentioned before, Perl's built-in OO system is very minimal, but also quite flexible. Over the years, many people have developed systems which build on top of Perl's built-in system to provide more features and convenience.
We strongly recommend that you use one of these systems. Even the most minimal of them eliminates a lot of repetitive boilerplate. There's really no good reason to write your classes from scratch in Perl.
If you are interested in the guts underlying these systems, check out perlobj.
Moose
Moose bills itself as a "postmodern object system for Perl 5". Don't be scared, the "postmodern" label is a callback to Larry's description of Perl as "the first postmodern computer language".
Moose provides a complete, modern OO system. Its biggest influence is the Common Lisp Object System, but it also borrows ideas from Smalltalk and several other languages. Moose was created by Stevan Little, and draws heavily from his work on the Perl 6 OO design.
Here is our File class using Moose:
package File;
use Moose;
has path => ( is => 'ro' );
has content => ( is => 'ro' );
has last_mod_time => ( is => 'ro' );
sub print_info {
my $self = shift;
print "This file is at ", $self->path, "\n";
}
Moose provides a number of features:
Declarative sugar
Mooseprovides a layer of declarative "sugar" for defining classes. That sugar is just a set of exported functions that make declaring how your class works simpler and more palatable. This lets you describe what your class is, rather than having to tell Perl how to implement your class.The
has()subroutine declares an attribute, andMooseautomatically creates accessors for these attributes. It also takes care of creating anew()method for you. This constructor knows about the attributes you declared, so you can set them when creating a newFile.Roles built-in
Mooselets you define roles the same way you define classes:package HasOnOfSwitch; use Moose::Role; has is_on => ( is => 'rw', isa => 'Bool', ); sub turn_on { my $self = shift; $self->is_on(1); } sub turn_off { my $self = shift; $self->is_on(0); }A miniature type system
In the example above, you can see that we passed
isa => 'Bool'tohas()when creating ouris_onattribute. This tellsMoosethat this attribute must be a boolean value. If we try to set it to an invalid value, our code will throw an error.Full introspection and manipulation
Perl's built-in introspection features are fairly minimal.
Moosebuilds on top of them and creates a full introspection layer for your classes. This lets you ask questions like "what methods does the File class implement?" It also lets you modify your classes programmatically.Self-hosted and extensible
Moosedescribes itself using its own introspection API. Besides being a cool trick, this means that you can extendMooseusingMooseitself.Rich ecosystem
There is a rich ecosystem of
Mooseextensions on CPAN under the MooseX namespace. In addition, many modules on CPAN already useMoose, providing you with lots of examples to learn from.Many more features
Mooseis a very powerful tool, and we can't cover all of its features here. We encourage you to learn more by reading theMoosedocumentation, starting with Moose::Manual.
Of course, Moose isn't perfect.
Moose can make your code slower to load. Moose itself is not small, and it does a lot of code generation when you define your class. This code generation means that your runtime code is as fast as it can be, but you pay for this when your modules are first loaded.
This load time hit can be a problem when startup speed is important, such as with a command-line script or a "plain vanilla" CGI script that must be loaded each time it is executed.
Before you panic, know that many people do use Moose for command-line tools and other startup-sensitive code. We encourage you to try Moose out first before worrying about startup speed.
Moose also has several dependencies on other modules. Most of these are small stand-alone modules, a number of which have been spun off from Moose. Moose itself, and some of its dependencies, require a compiler. If you need to install your software on a system without a compiler, or if having any dependencies is a problem, then Moose may not be right for you.
Moo
If you try Moose and find that one of these issues is preventing you from using Moose, we encourage you to consider Moo next. Moo implements a subset of Moose's functionality in a simpler package. For most features that it does implement, the end-user API is identical to Moose, meaning you can switch from Moo to Moose quite easily.
Moo does not implement most of Moose's introspection API, so it's often faster when loading your modules. Additionally, none of its dependencies require XS, so it can be installed on machines without a compiler.
One of Moo's most compelling features is its interoperability with Moose. When someone tries to use Moose's introspection API on a Moo class or role, it is transparently inflated into a Moose class or role. This makes it easier to incorporate Moo-using code into a Moose code base and vice versa.
For example, a Moose class can subclass a Moo class using extends or consume a Moo role using with.
The Moose authors hope that one day Moo can be made obsolete by improving Moose enough, but for now it provides a worthwhile alternative to Moose.
Class::Accessor
Class::Accessor is the polar opposite of Moose. It provides very few features, nor is it self-hosting.
It is, however, very simple, pure Perl, and it has no non-core dependencies. It also provides a "Moose-like" API on demand for the features it supports.
Even though it doesn't do much, it is still preferable to writing your own classes from scratch.
Here's our File class with Class::Accessor:
package File;
use Class::Accessor 'antlers';
has path => ( is => 'ro' );
has content => ( is => 'ro' );
has last_mod_time => ( is => 'ro' );
sub print_info {
my $self = shift;
print "This file is at ", $self->path, "\n";
}
The antlers import flag tells Class::Accessor that you want to define your attributes using Moose-like syntax. The only parameter that you can pass to has is is. We recommend that you use this Moose-like syntax if you choose Class::Accessor since it means you will have a smoother upgrade path if you later decide to move to Moose.
Like Moose, Class::Accessor generates accessor methods and a constructor for your class.
Object::Tiny
Finally, we have Object::Tiny. This module truly lives up to its name. It has an incredibly minimal API and absolutely no dependencies (core or not). Still, we think it's a lot easier to use than writing your own OO code from scratch.
Here's our File class once more:
package File;
use Object::Tiny qw( path content last_mod_time );
sub print_info {
my $self = shift;
print "This file is at ", $self->path, "\n";
}
That's it!
With Object::Tiny, all accessors are read-only. It generates a constructor for you, as well as the accessors you define.
Role::Tiny
As we mentioned before, roles provide an alternative to inheritance, but Perl does not have any built-in role support. If you choose to use Moose, it comes with a full-fledged role implementation. However, if you use one of our other recommended OO modules, you can still use roles with Role::Tiny
Role::Tiny provides some of the same features as Moose's role system, but in a much smaller package. Most notably, it doesn't support any sort of attribute declaration, so you have to do that by hand. Still, it's useful, and works well with Class::Accessor and Object::Tiny
OO System Summary
Here's a brief recap of the options we covered:
-
Mooseis the maximal option. It has a lot of features, a big ecosystem, and a thriving user base. We also covered Moo briefly.MooisMooselite, and a reasonable alternative when Moose doesn't work for your application. -
Class::Accessordoes a lot less thanMoose, and is a nice alternative if you findMooseoverwhelming. It's been around a long time and is well battle-tested. It also has a minimalMoosecompatibility mode which makes moving fromClass::AccessortoMooseeasy. -
Object::Tinyis the absolute minimal option. It has no dependencies, and almost no syntax to learn. It's a good option for a super minimal environment and for throwing something together quickly without having to worry about details. -
Use
Role::TinywithClass::AccessororObject::Tinyif you find yourself considering multiple inheritance. If you go withMoose, it comes with its own role implementation.
Other OO Systems
There are literally dozens of other OO-related modules on CPAN besides those covered here, and you're likely to run across one or more of them if you work with other people's code.
In addition, plenty of code in the wild does all of its OO "by hand", using just the Perl built-in OO features. If you need to maintain such code, you should read perlobj to understand exactly how Perl's built-in OO works.
CONCLUSION
As we said before, Perl's minimal OO system has led to a profusion of OO systems on CPAN. While you can still drop down to the bare metal and write your classes by hand, there's really no reason to do that with modern Perl.
For small systems, Object::Tiny and Class::Accessor both provide minimal object systems that take care of basic boilerplate for you.
For bigger projects, Moose provides a rich set of features that will let you focus on implementing your business logic.
We encourage you to play with and evaluate Moose, Class::Accessor, and Object::Tiny to see which OO system is right for you.
ПЕРЕВОДЧИКИ
Николай Мишин
<mi@ya.ru>