Moose::Cookbook::Snack::Keywords - Restricted "keywords" in Moose |
Moose::Cookbook::Snack::Keywords - Restricted ``keywords'' in Moose
version 2.1605
Moose exports a number of sugar functions in order to emulate Perl built-in keywords. These can cause clashes with other user-defined functions. This document provides a list of those keywords for easy reference.
use Moose
adds a method called meta
to your class. If this
conflicts with a method or function you are using, you can rename it,
or prevent it from being installed entirely. To do this, pass the
-meta_name
option when you use Moose
. For instance:
# install it under a different name use Moose -meta_name => 'moose_meta';
# don't install it at all use Moose -meta_name => undef;
If you are using Moose or the Moose::Role manpage it is best to avoid these keywords:
If you are using the Moose::Util::TypeConstraints manpage it is best to avoid these keywords:
To remove the sugar functions Moose exports, just add no Moose
at the bottom of your code:
package Thing; use Moose;
# code here
no Moose;
This will unexport the sugar functions that Moose originally exported. The same will also work for the Moose::Role manpage and the Moose::Util::TypeConstraints manpage.
Moose, the Moose::Role manpage and the Moose::Util::TypeConstraints manpage all use the Sub::Exporter manpage to handle all their exporting needs. This means that all the features that the Sub::Exporter manpage provides are also available to them.
For instance, with the Sub::Exporter manpage you can rename keywords, like so:
package LOL::Cat; use Moose 'has' => { -as => 'i_can_haz' };
i_can_haz 'cheeseburger' => ( is => 'rw', trigger => sub { print "NOM NOM" } );
LOL::Cat->new->cheeseburger('KTHNXBYE');
See the the Sub::Exporter manpage docs for more information.
You can also use the namespace::autoclean manpage to clean up your namespace. This will remove all imported functions from your namespace. Note that if you are importing functions that are intended to be used as methods (this includes the overload manpage, due to internal implementation details), it will remove these as well.
Another option is to use the namespace::clean manpage directly, but
you must be careful not to remove meta
when doing so:
package Foo; use Moose; use namespace::clean -except => 'meta'; # ...
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Moose::Cookbook::Snack::Keywords - Restricted "keywords" in Moose |