Sub::Ha...Generic(3)

πŸ’– Sub::Ha...Generic(3) β€” the user-contributed Perl docs glow-up

okay bestie, welcome to Sub::HandlesVia::Manual::WithGeneric (from the User Contributed Perl Documentation crew), aka the page that teaches you how to use Sub::HandlesVia with your plain old generic Perl classes. no Moose metaobject magic required, we're doing this by hand and honestly? iconic.


✨ NAME ✨

Sub::HandlesVia::Manual::WithGeneric β€” using Sub::HandlesVia with generic Perl classes. periodt.


πŸŽ€ MANUAL πŸŽ€

Sub::HandlesVia lets you delegate methods from your class to the values of your objects' attributes. like, the whole vibe is handing off work to your attributes so you don't have to write the same little method forty times.

conceptually, it lets you define $object->push_number($n) as a shortcut for $object->numbers->push($n). except β€” plot twist β€” $object->numbers is an arrayref, so it doesn't have methods you can call on it like push. the audacity. πŸ’€

so here's the tea on how it hooks in, depending on your class system:

  • For Moose and Mouse, Sub::HandlesVia can use their metaobject protocols to grab an attribute's definition and install the methods it needs to.
  • For Moo, it can wrap has and do its stuff that way.
  • For other classes, you need to be more explicit and tell it what methods to delegate to what attributes.

and here's what that explicit life looks like:

 package Kitchen {

  # constructor
  sub new {
    my ( $class, %arg ) = @_;
    $arg{food} ||= [];
    return bless( \%arg, $class );
  }

  # getter/setter for `food`
  sub food {
    (@_ == 1) ? $_[0]{food} : ( $_[0]{food} = $_[1] );
  }

  use Sub::HandlesVia qw( delegations );

  delegations(
    attribute   => 'food'
    handles_via => 'Array',
    handles     => {
      'add_food'    => 'push',
      'find_food'   => 'grep',
    },
  );
}

setting attribute to food means that when Sub::HandlesVia needs to get the food list, it will call $kitchen->food, and when it needs to set the food list, it will call $kitchen->food($value). simple, clean, chef's kiss. πŸ§‘πŸ³

got separate getter and setter methods? no problem, just do:

    attribute   => [ 'get_food', 'set_food' ],

or if you don't have any accessors at all and want Sub::HandlesVia to directly access the underlying hashref:

    attribute   => '{food}',

or maybe you do have a setter, but want to use hashref access for the getter:

    attribute   => [ '{food}', 'set_food' ],

or maybe you still want direct access for the getter, but your object is a blessed arrayref instead of a blessed hashref:

    attribute   => [ '[7]', 'set_food' ],

or maybe your needs are crazy unique:

    attribute   => [ \&getter, \&setter ],

the coderefs are passed the instance as their first argument, and the setter is also passed a value to set. slay.

really, I don't think there's any object system that this won't work for! πŸ’…

if you supply an arrayref with a getter and setter, it's also possible to supply a third argument which is a coderef or string which will be called as a method if needing to "reset" the value. this can be thought of like a default or builder. 🎁
heads up bestie: the delegations function can be imported into Moo/Mouse/Moose classes too, in which case the attribute needs to be the same attribute name you passed to has. you cannot use a arrayref, coderef, hash key, or array index. that's the rule, no cap.

πŸ› BUGS πŸ›

found a bug? not me manifesting problems, but if you did, please report any bugs to <https://github.com/tobyink/p5-sub-handlesvia/issues>. 🫢


πŸ‘€ 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 icon who wrote this.


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. so generous fr fr.


🚫 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.

like, no promises, bestie. it is what it is. 😭


and that's the whole page!! this was perl v5.42.0, dated 2026-02-11, brought to you by Sub::Ha...Generic(3) β€” stay hydrated, write your tests, and go delegate some methods. mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from