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:

  • extends
  • with
  • has
  • before
  • after
  • around
  • super
  • override
  • inner
  • augment
  • confess
  • blessed
  • meta

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:

  • type
  • subtype
  • class_type
  • role_type
  • maybe_type
  • duck_type
  • as
  • where
  • message
  • inline_as
  • coerce
  • from
  • via
  • enum
  • find_type_constraint
  • register_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:

  • Moose
  • Moose::Role
  • Moose::Util::TypeConstraints
  • Sub::Exporter
  • namespace::autoclean
  • namespace::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>

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