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 handles interface 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, and Bool cannot modify the attribute's value, rendering them largely useless 😭
if any of that is a problem for you, bestie, go look at Sub::HandlesVia instead β€” 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 use Sub::HandlesVia as a drop-in replacement for MooX::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:

  • Hash maps to Data::Perl::Collection::Hash::MooseLike πŸ“
  • Array maps to Data::Perl::Collection::Array::MooseLike ⭐
  • String maps to Data::Perl::String::MooseLike ✨
  • Number maps to Data::Perl::Number::MooseLike πŸ”₯
  • Bool maps to Data::Perl::Bool::MooseLike πŸ’…
  • Code maps to Data::Perl::Code 🧚

🌟 SEE ALSO 🌟

  • Moo
  • MooX::late
  • Sub::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 πŸ‘‘

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