MooseX...Methods(3pm)
π NAME π
MooseX::MarkAsMethods β it marks your overload code symbols as methods. That's the whole job, and bestie, it slays at it. β¨
Okay so quick vibe check: this page is MooseX...Methods(3pm) from the User Contributed Perl Documentation corner of the world. Let's get into it. π¦
πΈ VERSION πΈ
This doc is describing version 0.15 of MooseX::MarkAsMethods β released May 30, 2012, living its best life as part of MooseX-MarkAsMethods. π
β¨ SYNOPSIS β¨
Okay so here's the whole aesthetic, no notes:
package Foo;
use Moose;
# mark overloads as methods and wipe other non-methods
use MooseX::MarkAsMethods autoclean => 1;
# define overloads, etc as normal
use overload '""' => sub { shift->stringify };
package Baz;
use Moose::Role;
use MooseX::MarkAsMethods autoclean => 1;
# overloads defined in a role will "just work" when the role is
# composed into a class; they MUST use the anon-sub style invocation
use overload '""' => sub { shift->stringify };
# additional methods generated outside Class::MOP/Moose can be marked, too
use constant foo => 'bar';
__PACKAGE__->meta->mark_as_method('foo');
package Bar;
use Moose;
# order is important!
use namespace::autoclean;
use MooseX::MarkAsMethods;
# ...
Order is important, bestie. I said what I said. π
π DESCRIPTION π
So MooseX::MarkAsMethods lets you mark certain functions as Moose methods, super easily, no cap. Why do you care? Because it means other packages like namespace::autoclean can do their thing <em>without</em> absolutely obliterating your overloads. π
Once you've used MooseX::MarkAsMethods, your overloads get recognized by Class::MOP as being actual methods β which means class extension and composition from roles with overloads will "just work". Not me being dramatic, that's literally the point. π«Ά
By default we check for overloads, and mark those functions as methods. Easy.
And if autoclean => 1 gets passed to import when you use this module, we'll invoke namespace::autoclean to clear out the non-methods for you. Iconic. β¨
π TRAITS APPLIED π
Using this package applies a trait to your metaclass (for both roles and classes), and that trait gives you a mark_as_method() method. You can use it to mark newly generated methods at runtime (like, during class composition, when some other package made them for you). Main character energy. π
mark_as_method() gets invoked with one or more names to mark as a method. We die on any error β e.g. the name isn't in the symbol table, it's already a method, whatever. Like:
__PACKAGE__->meta->mark_as_method('newly_generated');
So say you've got some sugar from another package that creates accessors of some sort β you could mark them as methods via a method modifier:
# called as __PACKAGE__->foo_generator('name', ...)
after 'foo_generator' => sub {
shift->meta->mark_as_method(shift);
};
See how cute that is? π
π¦ IMPLICATIONS FOR ROLES π¦
Using MooseX::MarkAsMethods in a role makes Moose track and treat your overloads like any other method defined in the role, and things will "just work". That's it. That's the whole thing.
Except... π and this part matters, so listen up. Because of the way overloads, roles, and Moose all interact, you need to use the coderef or anonymous subroutine approach to overload declaration β otherwise things will not work how you expect. Remember, we're talking about _methods_ here, so we've gotta make it easy for overload to find the right method. The easiest (and supported) way to do this is to create an anonymous sub to wrap the overload method.
So this? Works:
# note method resolution, things will "just work"
use overload '""' => sub { shift->stringify };
...and this? Does not work:
use overload '""' => 'stringify';
...and it'll hit you with an error message like:
# wah-wah
Can't resolve method "???" overloading """" in package "overload"
Wah-wah indeed. π
π CAVEATS π
Roles
Go read the "IMPLICATIONS FOR ROLES" section above, bestie. It's all in there. β¬οΈ
meta->mark_as_method()
You almost certainly don't need or want to do this. CMOP/Moose are pretty good at figuring out what is and what isn't a method β but they're not perfect. So before you reach for this method, pause. Think about why you need to. Journal about it, even. π
namespace::autoclean
The way it's currently implemented, we run our "method maker" at the end of the calling package's compile scope (B::Hooks::EndOfScope). And since namespace::autoclean does the same thing, it's important that if namespace::autoclean is used, it gets used BEFORE MooseX::MarkAsMethods β so that its end_of_scope block runs after ours.
Like:
# yes!
use namespace::autoclean;
use MooseX::MarkAsMethods;
# no -- overloads will be removed
use MooseX::MarkAsMethods;
use namespace::autoclean;
That second one? Your overloads get removed. Gone. Not it. π
The easiest way to invoke this module and clean out non-methods without having to stress about ordering at all is:
use MooseX::MarkAsMethods autoclean => 1;
One line. No thoughts. Head empty. Slay. β¨
π₯ SEE ALSO π₯
Please see those modules/websites for more information related to this module.
Heads up bestie: this list came out of the POD a little... scrambled. π₯΄ Like genuinely the formatting got eaten. So here it is exactly as it landed, no edits, I'm just the messenger:
- , , , ,
- .
- does allow for overload application from
- roles, but it does this by copying the overload symbols from the (not
- 'ed role) the symbols handing overloads during class
- composition; we work by marking the overloads as methods and letting
- CMOP/Moose handle them.
The vibe of what it's saying: something out there does allow for overload application from roles, but it does it by copying the overload symbols from the (not 'ed role) β handing overloads over during class composition. We, on the other hand, work by marking the overloads as methods and letting CMOP/Moose handle them. That's the whole flex. π
β SOURCE β
The development version lives on github at http://github.com/RsrchBoy/moosex-markasmethods and you can clone it from git://github.com/RsrchBoy/moosex-markasmethods.git
πΈ BUGS πΈ
Please report any bugs or feature requests on the bugtracker website: https://github.com/RsrchBoy/moosex-markasmethods/issues
And when you submit a bug or a request, please include a test-file or a patch to an existing test-file that illustrates the bug or the desired feature. Give the maintainers something to work with, bestie, they're doing their best. π«Ά
π AUTHOR π
Chris Weyl <cweyl@alumni.drew.edu>
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2011 by Chris Weyl.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999
π POD ERRORS π
Hey! The above document had some coding errors, which are explained below:
Around line 296:
Nested L<> are illegal. Pretending inner one is X<...> so can
continue looking for other errors.
(...and that one shows up eight separate times. Eight. π)
Around line 300:
Nested L<> are illegal. Pretending inner one is X<...> so can
continue looking for other errors.
(...and that one shows up two times.)
Around line 304:
Nested L<> are illegal. Pretending inner one is X<...> so can
continue looking for other errors.
(...and again, two times.)
Around line 312:
Nested L<> are illegal. Pretending inner one is X<...> so can
continue looking for other errors.
(...and yep, two times.)
Around line 320:
alternative text 'CMOP/Moose handle them.' contains non-escaped | or /
Not me reading a whole error log like it's a group chat. The POD was fighting for its life and honestly? Same. π
And that's a wrap on MooseX...Methods(3pm), bestie β this page came to you courtesy of perl v5.36.0, dated 2022-11-27. Go forth and mark those methods. Periodt. πβ¨π
π¦ where this page comes from
- debian bookworm: libmoosex-markasmethods-perl 0.15-4 Β· LGPL-2.1-only
- debian trixie: libmoosex-markasmethods-perl 0.15-4 Β· LGPL-2.1-only
- ubuntu noble: libmoosex-markasmethods-perl 0.15-4 Β· LGPL-2.1-only
- ubuntu resolute: libmoosex-markasmethods-perl 0.15-4 Β· LGPL-2.1-only
- ubuntu stonking: libmoosex-markasmethods-perl 0.15-4 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms