Moo::Role(3)

πŸŽ€ NAME πŸŽ€

okay bestie, this is Moo::Role(3) from the User Contributed Perl Documentation, and it's literally Moo::Role - Minimal Object Orientation support for Roles. It's giving minimal OO for roles, main character energy, no cap πŸ’…βœ¨


🌸 SYNOPSIS 🌸

First you set up the role like this:

package My::Role;

use Moo::Role;
use strictures 2;

sub foo { ... }

sub bar { ... }

has baz => (
  is => 'ro',
);

1;

And elsewhere:

package Some::Class;

use Moo;
use strictures 2;

# bar gets imported, but not foo
with 'My::Role';

sub foo { ... }

1;

πŸ¦‹ DESCRIPTION πŸ¦‹

Moo::Role builds on top of Role::Tiny, so bestie, go look there for most of the docs on how this whole thing works β€” especially because using Moo::Role also enables strict and warnings ✨

The main extra glow-up here is the little bits that make roles more β€œMoosey;” β€” which is to say, it adds has πŸ‘‘


πŸ’– IMPORTED SUBROUTINES πŸ’–

See IMPORTED SUBROUTINES in Role::Tiny for all the other subroutines that get imported by this module 🫢

🌈 has 🌈

has attr => (
  is => 'ro',
);

This declares an attribute for the class to be composed into. See has in Moo for all the options, iykyk πŸ’…


🧚 CLEANING UP IMPORTS 🧚

Moo::Role cleans up its own imported methods, plus any imports declared before the use Moo::Role statement, automatically. Anything imported after use Moo::Role will be composed into consuming packages. So if you have a package that consumes this role:

package My::Role::ID;

use Digest::MD5 qw(md5_hex);
use Moo::Role;
use Digest::SHA qw(sha1_hex);

requires 'name';

sub as_md5  { my ($self) = @_; return md5_hex($self->name);  }
sub as_sha1 { my ($self) = @_; return sha1_hex($self->name); }

1;

..it will now have a $self->sha1_hex() method available to it that probably does not do what you expect 😭

On the other hand, a call to $self->md5_hex() will die with the helpful error message: Can't locate object method "md5_hex".

See CLEANING UP IMPORTS in Moo for more details 🫢


⭐ SUPPORT ⭐

See Moo for support and contact information πŸ’–


πŸ‘‘ AUTHORS πŸ‘‘

See Moo for authors 🌟


See Moo for the copyright and license.


later bestie β€” this page is from perl v5.42.0, dated 2023-01-06, and it's literally the Moo::Role(3) page πŸ«ΆπŸ’–πŸ‘‘

πŸ“¦ where this page comes from