Spec-Zone .ru
спецификации, руководства, описания, API
Spec-Zone .ru
спецификации, руководства, описания, API
Библиотека разработчика Mac Разработчик
Поиск

 

Эта страница руководства для  версии 10.9 Mac OS X

Если Вы выполняете различную версию  Mac OS X, просматриваете документацию локально:

Читать страницы руководства

Страницы руководства предназначаются как справочник для людей, уже понимающих технологию.

  • Чтобы изучить, как руководство организовано или узнать о синтаксисе команды, прочитайте страницу руководства для страниц справочника (5).

  • Для получения дополнительной информации об этой технологии, ищите другую документацию в Библиотеке Разработчика Apple.

  • Для получения общей информации о записи сценариев оболочки, считайте Shell, Пишущий сценарий Учебника для начинающих.



Moose::Cookbook::Roles::Restartable_AdvancedComposition(3)oDocumentation::Restartable_AdvancedComposition(3)



NAME
       Moose::Cookbook::Roles::Restartable_AdvancedComposition - Advanced Role Composition - method
       exclusion and aliasing

VERSION
       version 2.0604

SYNOPSIS
         package Restartable;
         use Moose::Role;

         has 'is_paused' => (
             is      => 'rw',
             isa     => 'Bool',
             default => 0,
         );

         requires 'save_state', 'load_state';

         sub stop { 1 }

         sub start { 1 }

         package Restartable::ButUnreliable;
         use Moose::Role;

         with 'Restartable' => {
             -alias => {
                 stop  => '_stop',
                 start => '_start'
             },
             -excludes => [ 'stop', 'start' ],
         };

         sub stop {
             my $self = shift;

             $self->explode() if rand(1) > .5;

             $self->_stop();
         }

         sub start {
             my $self = shift;

             $self->explode() if rand(1) > .5;

             $self->_start();
         }

         package Restartable::ButBroken;
         use Moose::Role;

         with 'Restartable' => { -excludes => [ 'stop', 'start' ] };

         sub stop {
             my $self = shift;

             $self->explode();
         }

         sub start {
             my $self = shift;

             $self->explode();
         }

DESCRIPTION
       In this example, we demonstrate how to exercise fine-grained control over what methods we consume
       from a role. We have a "Restartable" role which provides an "is_paused" attribute, and two methods,
       "stop" and "start".

       Then we have two more roles which implement the same interface, each putting their own spin on the
       "stop" and "start" methods.

       In the "Restartable::ButUnreliable" role, we want to provide a new implementation of "stop" and
       "start", but still have access to the original implementation. To do this, we alias the methods from
       "Restartable" to private methods, and provide wrappers around the originals (1).

       Note that aliasing simply adds a name, so we also need to exclude the methods with their original
       names.

         with 'Restartable' => {
             -alias => {
                 stop  => '_stop',
                 start => '_start'
             },
             -excludes => [ 'stop', 'start' ],
         };

       In the "Restartable::ButBroken" role, we want to provide an entirely new behavior for "stop" and
       "start". We exclude them entirely when composing the "Restartable" role into
       "Restartable::ButBroken".

       It's worth noting that the "-excludes" parameter also accepts a single string as an argument if you
       just want to exclude one method.

         with 'Restartable' => { -excludes => [ 'stop', 'start' ] };

CONCLUSION
       Exclusion and renaming are a power tool that can be handy, especially when building roles out of
       other roles. In this example, all of our roles implement the "Restartable" role. Each role provides
       same API, but each has a different implementation under the hood.

       You can also use the method aliasing and excluding features when composing a role into a class.

FOOTNOTES
       (1) The mention of wrapper should tell you that we could do the same thing using method modifiers,
           but for the sake of this example, we don't.

AUTHOR
       Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in
       Moose and "CONTRIBUTORS" in Moose for details.

COPYRIGHT AND LICENSE
       This software is copyright (c) 2012 by Infinity Interactive, Inc..

       This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5
       programming language system itself.



perl v5.16.2                                     2012-09-19okbook::Roles::Restartable_AdvancedComposition(3)

Сообщение о проблемах

Способ сообщить о проблеме с этой страницей руководства зависит от типа проблемы:

Ошибки содержания
Ошибки отчета в содержании этой документации со ссылками на отзыв ниже.
Отчеты об ошибках
Сообщите об ошибках в функциональности описанного инструмента или API через Генератор отчетов Ошибки.
Форматирование проблем
Отчет, форматирующий ошибки в интерактивной версии этих страниц со ссылками на отзыв ниже.