Moose::...eywords(3)
π NAME π
Moose::Cookbook::Snack::Keywords β restricted "keywords" in Moose, bestie. Yes it's a snack, and yes it's about name collisions. Sit down, we're doing this. π
This page lives in the User Contributed Perl Documentation collection, and it's filed under Moose::...eywords(3) if you're out here grepping man pages like a detective. π
β¨ VERSION β¨
version 2.4000 β that's the one, no cap.
π¦ DESCRIPTION π¦
Okay so. Moose exports a whole bunch of sugar functions, right? And the whole point of them is to emulate Perl built-in keywords. Which is iconic of Moose, genuinely. But it also means they can clash with functions you or your coworkers (or some random CPAN module, let's be honest) already defined. π
This document is literally just a list of those keywords so you can peek at them for easy reference. That's it. That's the whole page. But it's a useful list, okay? π
πΈ The 'meta' keyword πΈ
use Moose sneaks a method called meta onto your class. Cute, right? But if that name is already doing something in your code β like, if it's beefing with a method or function you're using β you've got two options:
- rename it, or
- stop it from being installed at all.
To do either one, you pass the -meta_name option when you use Moose. Watch:
# install it under a different name
use Moose -meta_name => 'moose_meta';
# don't install it at all
use Moose -meta_name => undef;
See? -meta_name takes a string to rename it, or undef to just... not. Periodt. π
π Moose Keywords π
If you're using Moose or Moose::Role, it is best to avoid these keywords. Like, avoid avoid:
extendswithhasbeforeafteraroundsuperoverrideinneraugmentconfessblessedmeta
That's the whole list. Don't name your stuff any of these. Trust. π
π§ Moose::Util::TypeConstraints Keywords π§
And if you're using Moose::Util::TypeConstraints, it's best to avoid these keywords too:
typesubtypeclass_typerole_typemaybe_typeduck_typeaswheremessageinline_ascoercefromviaenumfind_type_constraintregister_type_constraint
Sixteen of them, all listed, all avoiding. You're welcome. π«Ά
π Avoiding collisions π
This is the part where we actually fix the problem instead of just being aware of it. Growth. π±
π Turning off Moose
Want 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. And guess what β the exact same trick works for Moose::Role and Moose::Util::TypeConstraints. Slay. β¨
π Sub::Exporter features
Moose, Moose::Role and Moose::Util::TypeConstraints all use Sub::Exporter to handle all their exporting needs. Which means every single feature Sub::Exporter provides is also available to them. Free real estate. π
For instance, with Sub::Exporter 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');
Not me making has go by i_can_haz. π Anyway β see the Sub::Exporter docs for more information.
π¦ namespace::autoclean and namespace::clean π¦
You can also use namespace::autoclean to clean up your namespace. This will remove all imported functions from your namespace.
Heads up bestie: if you're importing functions that are intended to be used as methods (and yes, this includes overload, due to internal implementation details), it will remove these as well. So like, know that before you vibe check your namespace. π
Another option is to use namespace::clean directly β but you must be careful not to remove meta when doing so:
package Foo;
use Moose;
use namespace::clean -except => 'meta';
# ...
Note the -except => 'meta'. Do not forget it. I'm serious. π
β SEE ALSO β
Go read these, they're all part of the lore:
MooseMoose::RoleMoose::Util::TypeConstraintsSub::Exporternamespace::autocleannamespace::clean
π AUTHORS π
Look at this roster. Absolute legends, every single one:
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Jesse Luehrs <doy@cpan.org>
- Shawn M Moore <sartak@cpan.org>
- ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>
- Karen Etheridge <ether@cpan.org>
- Florian Ragwitz <rafl@debian.org>
- Hans Dieter Pearcey <hdp@cpan.org>
- Chris Prather <chris@prather.org>
- Matt S Trout <mstrout@cpan.org>
π COPYRIGHT AND LICENSE π
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. π
and that's the whole page, bestie π«Ά β version 2.4000, dated 2025-07-25, sitting pretty under perl v5.42.0. go forth and don't name your methods has. mwah π
β¨
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms