Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Applying several traits to an instance should not do anything different than applying one. So for instance this (in pseudo Perl 6):

    $fido does Tricks does Treats;

    Would become this (in Perl 5 Traits):


    Basically apply should return the reblessed instance, and you just keep chaining them.

    Now, this may not be the most efficient approach, but I am not really sure that would matter since runtime application of multiple traits will probably be a rare occuranc

    • The better way to accomplish this (IMO of course) is to compose a third Trait out of the multiple traits you have applied, then apply that third trait to your instance.

      I think that’s the key to a bearable syntax: have a Class::Traits::compose, returning an anonymous trait which is a composition of the given traits, on which you then simply invoke apply:

      Class::Traits->compose( "Tricks", "Treats" )->apply( $fido );