I've been following the discussion of "traits" on the Perl 6 lists and here in the monastery with a fair deal of interest, but also a vague feeling of uncertainty -- I didn't feel like I had a firm grip on exactly what the core idea was.

Then earlier today, a very Perlish analogue struck me: on some level, "trait classes" are "method exporters".

For example, here's a hypothetical trivial trait:

package Trait::SelfDumping; require Carp; require Exporter; require Data::Dumper; @EXPORT = qw( dump_self ); sub import { goto &Exporter::import } sub dump_self { my $self = shift; Carp::carp("Self is " . Data::Dumper::Dumper($self) ) } 1;

Now I can add the trait of being self-dumpable to any other class I build:

package My::FooBar; use Trait::SelfDumping ':default'; sub new { ... package main; my $foo = My::FooBar->new(); $foo->dump_self();

A proper traits mechanism should also provide for aliasing method names and for double-checking that other methods that yours depends on have been provided, but those are merely elaborations.

I can imagine an Exporter::Traits module that functioned like Exporter and incorporated the aliasing and missing-methods checks. (Exporter::Renaming already touches on the aliasing aspect. Checking for missing methods is harder, because they may be being provided by another trait, but I presume it can be done.)

Parallel to how Class::Trait works, packages that were going to implement a trait would "use Exporter::Traits" and declare various package variables and subroutines. However, on the caller's side, the interface would follow Exporter, so you could "use" the trait package directly, rather than having to know that it was a trait-driven package.

Have I overlooked something vital in reducing the traits model to a gussied-up Exporter?

And am I right in thinking that viewing this as an Exporter trick makes the concept more accessible to Perl geeks?


In reply to Traits as Method Exporters by simonm

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.