MouseX::AttributeTraitHelper::Merge - Extend your attribute traits interface for Mouse
This document describes MouseX::AttributeTraitHelper::Merge version 0.90.
package ClassWithTrait; use Mouse -traits => 'MouseX::AttributeTraitHelper::Merge'; has attrib => ( is => 'rw', isa => 'Int', traits => ['Trait1', 'Trait2'], ); no Mouse; __PACKAGE__->meta->make_immutable();
If you need to use many traits for attribute with overlapped field name this solution for you!
This role replace all trait for attribute by one new trait. For example:
You have two traits:
package Trait1; use Mouse::Role; has 'allow' => (isa => 'Int', default => 123); no Mouse::Role; package Trait2; use Mouse::Role; has 'allow' => (isa => 'Str', default => 'qwerty'); no Mouse::Role;
Both add fields to attribute with same name. In this case Mouse throw the exception: "We have encountered an attribute conflict with 'allow' during composition. This is fatal error and cannot be disambiguated."
Usage of a '+' before role attribute is not supported.
Solution:
In this case Trait1 and Trait2 merged in MouseX::AttributeTraitHelper::Merge::Trait1::Trait2 and applied to attribute `attrib`. The last `Trait` in the list is the highest priority and rewrite attribute fields.
In this case attribute `attrib` has field `allow` with type `Str` and default value `qwerty`.
But method `does` still work correctly: `ClassWithTrait->meta->get_attribute('attrib')->does('Trait1')` or `ClassWithTrait->meta->get_attribute('attrib')->does('Trait2')` returns true
The last may confuse the developer because `Trait1` exports the `allow` field of type `Int`, but ultimately `allow` is of type `Str`
Perl 5.8.8 or later.
Mouse
Mouse::Role
Mouse::Meta::Role
Mouse::Meta::Class
Nikolay Shulyakovskiy (nikolas) <nikolas(at)cpan.org>
Copyright (c) 2019, Nikolay Shulyakovskiy (nikolas)
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic for details.
To install MouseX::AttributeTraitHelper::Merge, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MouseX::AttributeTraitHelper::Merge
CPAN shell
perl -MCPAN -e shell install MouseX::AttributeTraitHelper::Merge
For more information on module installation, please visit the detailed CPAN module installation guide.