MooX::HandlesVia(3pm)
β¨ MooX::HandlesVia β¨
okay bestie, welcome to MooX::HandlesVia (that's the manual page for it, Perl vibes only π
) β NativeTrait-like behavior for Moo, and yes that's the whole NAME, no notes.
π VERSION π
version 0.001009 β it's giving stable, it's giving shipped, we love to see it π
πΈ SYNOPSIS πΈ
here's the vibe, straight up:
{
package Hashy;
use Moo;
use MooX::HandlesVia;
has hash => (
is => 'rw',
handles_via => 'Hash',
handles => {
get_val => 'get',
set_val => 'set',
all_keys => 'keys'
}
);
}
my $h = Hashy->new(hash => { a => 1, b => 2});
$h->get_val('b'); # 2
$h->set_val('a', 'BAR'); # sets a to BAR
my @keys = $h->all_keys; # returns a, b
look at her go π¦ you made a Hashy, you asked it for b, it said 2. you changed a to BAR, done. you asked for the keys, you got a, b. no cap, that's the whole magic trick.
π DESCRIPTION π
so MooX::HandlesVia is an extension of Moo's handles attribute functionality, periodt. what it does is give you a way to proxy functionality from some external class onto a given attribute.
and why would you want that? because it's most commonly used to emulate that 'Native Trait' behavior that is literally everywhere in Moose code β and back in the day there was just no Moo alternative for it. so we made one. you're welcome π«Ά
π SHORTCOMINGS π
ngl, because of how Moo is implemented under the hood right now, MooX::HandlesVia has some deficiencies compared to what you'd expect from Moose native traits. iykyk. let's get into it:
- methods delegated via the Moo
handlesinterface get passed the attribute value directly, and there is no way to access the parent class. which means: if an attribute gets updated, any triggers or type coercions WILL NOT fire. nothing. not a single one. π - Moo attribute method delegations are passed the attribute value. this is totally fine for references (objects, arrays, hashrefs, etc.) β but it means simple scalar types are READ ONLY. which unfortunately means
Number,String,Counter, andBoolcannot modify the attribute's value, rendering them largely useless π
if any of that is a problem for you, bestie, go look atSub::HandlesViainstead β it uses a different architecture, it respects triggers and coercions, and it allows read-write access to non-reference values. and plot twist: it should be possible to useSub::HandlesViaas a drop-in replacement forMooX::HandlesViaπ
π PROVIDED INTERFACE/FUNCTIONS π
π process_has(@_)
so MooX::HandlesVia preprocesses the arguments you pass to has() attribute declarations, and it does that via the process_has function. here's the tea:
in a given Moo class, if 'handles_via' is set to a ClassName string, and 'handles' is set with a hashref mapping of the desired moo class methods that should map to ClassName methods β then process_has() will create the appropriate binding to create that mapping IF ClassName actually provides that named method. if it doesn't? no mapping for you π
like so:
has options => (
is => 'rw',
handles_via => 'Array',
handles => {
mixup => 'shuffle',
unique_options => 'uniq',
all_options => 'elements'
}
);
now, the following handles_via keywords are reserved as shorthand for mapping to Data::Perl, so don't go renaming ur stuff and being confused later:
Hashmaps toData::Perl::Collection::Hash::MooseLikeπArraymaps toData::Perl::Collection::Array::MooseLikeβStringmaps toData::Perl::String::MooseLikeβ¨Numbermaps toData::Perl::Number::MooseLikeπ₯Boolmaps toData::Perl::Bool::MooseLikeπCodemaps toData::Perl::Codeπ§
π SEE ALSO π
MooMooX::lateSub::HandlesVia
π«Ά ORIGINAL AUTHOR π«Ά
Matthew Phillips <mattp@cpan.org> β the blueprint, the blueprint-er, we stan π
π AUTHOR π
Toby Inkster <tobyink@cpan.org> β carrying the torch, iconic behavior π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2020 by Matthew Phillips <mattp@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay, free software, we love licensing that actually lets you do stuff π
okay that's the whole page babes, literally all of it, no crumbs left. this one's perl v5.36.0, dated 2022-10-16, and it lives at MooX::HandlesVia(3pm) β run it, love it, go handle some stuff via π
β¨
π¦ where this page comes from
- ubuntu noble: libmoox-handlesvia-perl 0.001009-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libmoox-handlesvia-perl 0.001009-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmoox-handlesvia-perl 0.001009-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms