Over in my use.perl journal, I discussed a module I've been writing named Class::Trait. This module attempts to implement traits (pdf) in Perl. chromatic has Class::Roles on the CPAN and this is an attempt to implement Perl 6 roles in Perl 5, but he's decided to hold off further development until Apocalypse 12 comes out as that is the document that will fully explain the intended implementation of roles. Roles, incidentally, are essentially the same thing as "traits" in the paper above, but the word "trait" (compile time properties) is already used in Perl 6.

I've considered uploading my module to the CPAN, but I've held off for a couple of reasons. First, I'm concerned that this might cause confusion with Perl 6 traits/roles (though this should be less of a concern if I document it well). The other reason is pretty straightforward: people don't know much about traits and if no one is likely to use the module, I don't see much sense in adding to the terminology glut. If you think that traits are something you would be interested in using, please let me know.

My design considerations:

Since Perl 6 roles are not completely defined, I ignored them and used the original traits paper for guidance.

Here's my first pass (this code works, but I don't have a tarball uploaded anywhere), borrowing heavily from chromatic's Class::Roles example (because my examples were so politically incorrect that even I was a bit put off by them).

We have a person who wishes to have a LifeGuard trait, but only wants to dog paddle. Thus, even though a lifeguard can swim, we want the dog's &swim method. However, a dog might not swim without a doggie treat and the person doesn't have a &doggie_treat method, but it will AUTOLOAD one on demand.

package Person; use Class::Trait 'LifeGuard'; use Class::Trait 'Dog' => { explicit => 'swim' }; Class::Trait->promise('doggie_treat'); Class::Trait->assemble; # this flattens the traits into this class sub AUTOLOAD { # we'll make &doggie_treat, if needed } 1;

The traits look like this:

package Trait::LifeGuard; sub swim { return Trait::LifeGuard::_swim(); } sub save_drowning_swimmer { my (undef, $swimmer) = @_; # save the swimmer } 1; package Trait::Dog; Class::Trait->expects('doggie_treat'); sub swim { # $class might actually be an instance, but we should assume # nothing about the internals of the object my ($class, $target) = @_; Trait::Dog::_eat($class->doggie_treat); # swim to $target } sub _eat { # this must be called via a fully qualified name because private # methods are not flattened into the primary class } 1;

The way this works is simple (in theory, not in code). As traits are added, promises made and expectations listed, I build a composite interface (similar to a Java interface). When Class::Trait->assemble (should I call that 'flatten'?) is called, the interface is validated and conflicts are resolved. Unresolved conflicts are fatal, as are unmet expectations.

Questions:

Cheers,
Ovid

New address of my CGI Course.


In reply to Class::Trait to the CPAN? by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.