[Pdx-pm] Poll: traits

Ovid publiustemp-pdxpm at yahoo.com
Sat Nov 19 12:45:57 PST 2005

--- Eric Wilhelm <scratchcomputing at gmail.com> wrote:

> >� package FamilyMember;
> >� use Class::Trait
> >� � TSpouse => { exclude => ['fuse'] },
> >� � TBomb � => { exclude => ['explode'] };
> >
> >And now FamilyMember->can('explode') (from TSpouse) and
> >FamilyMember->can('fuse') (from TBomb).
> So, in this case, it's the same as
>   use TSpouse qw(explode);
>   use TBomb qw(fuse);
> Right?

Almost.  In fact, in many cases it is.  However, in this case, let's
imagine that the TBomb::fuse() method is defined like this:

  sub fuse {
    my ($self, $time) = @_;

Where does the set_igniter() method come from?  It might be provided in
the TBomb trait, but if you want to have a variety of applicable
igniters, you can add this:

  our @REQUIRES = qw(set_igniter);

Now, TBomb does not have to provide its own implementation, but you
can't use it unless *something* provides that.  Again, it fails at
compile time.

This sounds all theoretical and abstract, but we're really using this
in a production system.  We have traits which generate XML and XHTML. 
There's also a "data store" trait.  The data store can provide test
objects and a variety of our test class generate test objects using the
Store trait.  When a test class needs to generate XML, the XML is based
upon objects and the trait has this line:

  our @REQUIRES = qw(test_objects);

(It actually has more than that).

If I forget to use the Store trait but I use the XML trait, the tests
won't compile and I don't have to waste time tracking down potentially
confusing test failures.


If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/

More information about the Pdx-pm-list mailing list