Sub::Ha...thMoose(3)

πŸ’– NAME πŸ’–

Sub::HandlesVia::Manual::WithMoose β€” it's giving "using Sub::HandlesVia with Moose" bestie, no cap πŸ¦‹


✨ SYNOPSIS ✨

Okay so picture this, you got a class, you got attributes, you want methods to just happen for free. Look:

package Kitchen {
  use Moose;
  use Sub::HandlesVia;
  use Types::Standard qw( ArrayRef Str );

  has food => (
    is          => 'ro',
    isa         => ArrayRef[Str],
    handles_via => 'Array',
    default     => sub { [] },
    handles     => {
      'add_food'    => 'push',
      'find_food'   => 'grep',
    },
  );
}

(Also real talk: if you have an actual moose in your kitchen, that might be a disaster! πŸ™ˆ)


🌸 MANUAL 🌸

So like, Sub::HandlesVia lets you delegate methods from your class straight to the values of your objects' attributes. Iconic behaviour honestly. πŸ’…

Conceptually, it lets you define $object->push_number($n) to be a shortcut for $object->numbers->push($n) β€” EXCEPT that $object->numbers is an arrayref, so it literally doesn't have methods you can call on it like push. The way that whole thing is a scam 😭 β€” that's exactly the gap this fills.

Moose does have a built-in feature (native traits) which provides a more limited version of this.

πŸŽ€ Which Methods Can Be Delegated To? πŸŽ€

The handles_via option is basically the vibe check for which library of methods should be available. Valid values include Array, Blessed, Bool, Code, Counter, Enum, Hash, Number, Scalar, and String. Pick your fighter πŸ’–

You can also hand it an arrayref for handles_via, though ngl many of the options are conceptually contradictory. It's giving chaos but okay:

 handles_via => [ 'Number', 'Scalar' ]

πŸ¦‹ Moose Native Types πŸ¦‹

So even though the synopsis above shows Types::Standard being used for type constraints, Moose native types should also work fine. Both work, we don't gatekeep. πŸ’œ

package Kitchen {
  use Moose;
  use Sub::HandlesVia;

  has food => (
    is          => 'ro',
    isa         => 'ArrayRef[Str]',
    handles_via => 'Array',
    default     => sub { [] },
    handles     => {
      'add_food'    => 'push',
      'find_food'   => 'grep',
    },
  );
}

⭐ True Moose Native Traits Drop-In Syntax ⭐

Not only that, but Sub::HandlesVia will also recognize native-traits-style traits. It will jump in and handle them before Moose even notices! Sneaky queen behaviour honestly, main character energy πŸ‘‘

package Kitchen {
  use Moose;
  use Sub::HandlesVia;

  has food => (
    is          => 'ro',
    isa         => 'ArrayRef[Str]',
    traits      => ['Array'],
    default     => sub { [] },
    handles     => {
      'add_food'    => 'push',
      'find_food'   => 'grep',
    },
  );
}

πŸ’€ BUGS πŸ’€

Please report any bugs to <https://github.com/tobyink/p5-sub-handlesvia/issues>.

Bestie if you find a bug, tell the maintainers, don't just suffer in silence fr fr πŸ™

πŸ”₯ SEE ALSO πŸ”₯

  • Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
  • Sub::HandlesVia.
  • Documentation for delegatable methods: Sub::HandlesVia::HandlerLibrary::Array, Sub::HandlesVia::HandlerLibrary::Blessed, Sub::HandlesVia::HandlerLibrary::Bool, Sub::HandlesVia::HandlerLibrary::Code, Sub::HandlesVia::HandlerLibrary::Counter, Sub::HandlesVia::HandlerLibrary::Enum, Sub::HandlesVia::HandlerLibrary::Hash, Sub::HandlesVia::HandlerLibrary::Number, Sub::HandlesVia::HandlerLibrary::Scalar, and Sub::HandlesVia::HandlerLibrary::String.

πŸ‘‘ AUTHOR πŸ‘‘

Toby Inkster <tobyink@cpan.org>. β€” the literal legend, we stan πŸ’–


This software is copyright (c) 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


πŸ“ DISCLAIMER OF WARRANTIES πŸ“

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

No promises, no cap, you're on your own if it breaks 😭

And that's the whole thing, bestie! This page came to you from perl v5.42.0, dated 2026-02-11, and it's the manual for Sub::Ha...thMoose(3) β€” go forth and delegate, mwah πŸ’‹πŸ’œ

πŸ“¦ where this page comes from