Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you want is very much like a <em>trait</em> (or in Perl: a <em>role</em>).</p> <p>Traits are a relatively recent addition to object oriented systems. They are like interfaces in that they can require an inheriting class to implement certain methods, but they are like a abstract superclass in that they can provide certain methods themselves.</p> <p>The Moose object system allows roles. A class can be declared <code>with</code> a certain role. Here your example written with <code>MooseX::Declare</code>:</p> <pre><code>use MooseX::Declare; role Story::StoryTeller{ requires 'specifics'; method tellStory() { print "Once upon a time "; $self-&gt;specifics(); } } class Story::Story1 with Story::StoryTeller { method specifics() { print " there was a dragon\n"; } } class Story::Story2 with Story::StoryTeller { method specifics() { print " there was a house\n"; } } my $story1 = Story::Story1-&gt;new(); my $story2 = Story::Story2-&gt;new(); #Once upon a time there was a dragon. $story1-&gt;tellStory(); #Once upon a time there was a house. $story2-&gt;tellStory(); </code></pre> <p>This isn't inheritance: <code>$story1-&gt;isa("Story::StoryTeller")</code> is false, but it <em>does</em> this role: <code>$story1-&gt;DOES("Story::StoryTeller")</code> is true.</p> <p>For every class that <code>DOES</code> a certain role, an instance of that class <code>can</code> all methods of the role. Therefore, <code>$story1-&gt;can("tellStory")</code> is true, and in reverse for every <code>Story::StoryTeller</code> instance, <code>$instance-&gt;can("specifics")</code> will be true.</p> <p>A role cannot be instantiated on its own.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload