in reply to Class::Trait to the CPAN?

This is the definition given in the C++ Standard:

17.1.18       traits class          [defns.traits]
a class that encapsulates a set of types and functions necessary for template classes and template functions to manipulate objects of types for which they are instantiated. Traits classes defined in clauses 21, 22 and 27 are chararacter traits, which provide the character handling support needed by the string and iostream classes.

-- ISO/IEC 14882:1998(E)

In Standard C++ these are used to package the basic character operations for equality, dictionary ordering, string length, searching, movement and copying. There, they are characterized by the requirements that they present a unified interface and that each character class provides a concrete implementation of that interface. They sound very like the set of conditions you cite.

Under Perl's looser type regimen, generic (template) programming is usually regarded as automatic. An instance class's methods are assumed to provide the needed interface, and it is up to the programmer to make sure they automatically do ;-)

It seems to me that there are two fairly natural implementations available in Perl 5.

  1. By inheritance from a virtual base class. The interface functions are written there in terms of declared but undefined primitive methods which are private to the derived class, which must implement them somehow. Flattening is achieved by ordinary inheritance. The usual caveats of multiple inheritance apply.
  2. By Perl's attribute mechanism. That is notationally convenient, but is still a black art for many Perlers (myself included). Perhaps this is less an alternative than an element of #1.

One less-known aspect of C++ traits is that their methods do not throw. Is some no-error convention appropriate for Perl traits?

After Compline,
Zaxo