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
hasand 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: thedelegationsfunction can be imported into Moo/Mouse/Moose classes too, in which case theattributeneeds to be the same attribute name you passed tohas. 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.
Β©οΈ COPYRIGHT AND LICENCE Β©οΈ
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
- fedora 44: perl-Sub-HandlesVia 0.053005-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms