Sub::HandlesVia::Declare(3pm) User Contributed Perl Documentation Sub::HandlesVia::Declare(3pm)

Sub::HandlesVia::Declare - declare delegations at compile-time

 use Sub::HandlesVia::Declare( $attr, $via => %delegations );

This is roughly equivalent to the following:

 use Sub::HandlesVia qw(delegations);
 
 BEGIN {
   delegations(
     attribute    => $attr,
     handles_via  => $via,
     handles      => \%delegations,
   );
 };

Except it doesn't import the "delegations" function into your namespace.

Useful for Object::Pad and kind of nice for Class::Tiny. Basically any class builder than does its stuff at compile time.

 use Object::Pad;
 
 class Kitchen {
   has @foods;
   use Sub::HandlesVia::Declare '@foods', Array => (
     all_foods => 'all',
     add_food  => 'push',
   );
 }

If an attribute begins with a '@' or '%', $via can be omitted.

 use Object::Pad;
 
 class Kitchen {
   has @foods;
   use Sub::HandlesVia::Declare '@foods', (
     all_foods => 'all',
     add_food  => 'push',
   );
 }

 package Kitchen;
 use Class::Tiny {
   foods  => sub { [] },
   drinks => sub { [ 'water' ] },
 };
 use Sub::HandlesVia::Declare 'foods', Array => (
   all_foods  => 'all',
   add_food   => 'push',
 );
 use Sub::HandlesVia::Declare 'drinks', Array => (
   all_drinks => 'all',
   add_drink  => 'push',
 );

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

Sub::HandlesVia.

Toby Inkster <tobyink@cpan.org>.

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.

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.

2023-04-09 perl v5.36.0