package MyApp::Meta::Attribute::Trait::Labeled;
use Moose::Role;
+ Moose::Util::meta_attribute_alias('Labeled');
has label => (
is => 'rw',
predicate => 'has_label',
);
- package Moose::Meta::Attribute::Custom::Trait::Labeled;
- sub register_implementation {'MyApp::Meta::Attribute::Trait::Labeled'}
-
package MyApp::Website;
use Moose;
contains a single attribute, C<label>. Any attribute which does this trait
will now have a label.
-Next we register our trait with Moose:
+We also register our trait with Moose:
- package Moose::Meta::Attribute::Custom::Trait::Labeled;
- sub register_implementation { 'MyApp::Meta::Attribute::Trait::Labeled' }
+ Moose::Util::meta_attribute_alias('Labeled');
-Moose looks for the C<register_implementation> method in
-C<Moose::Meta::Attribute::Custom::Trait::$TRAIT_NAME> to find the full
-name of the trait.
+This allows Moose to find our trait by the short name C<Labeled> when passed
+to the C<traits> attribute option, rather than requiring the full package
+name to be specified.
Finally, we pass our trait when defining an attribute:
# in lib/MyApp/Meta/Class/Trait/HasTable.pm
package MyApp::Meta::Class::Trait::HasTable;
use Moose::Role;
+ Moose::Util::meta_class_alias('HasTable');
has table => (
is => 'rw',
isa => 'Str',
);
- package Moose::Meta::Class::Custom::Trait::HasTable;
- sub register_implementation { 'MyApp::Meta::Class::Trait::HasTable' }
-
# in lib/MyApp/User.pm
package MyApp::User;
use Moose -traits => 'HasTable';