logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Author

       Toby Inkster <tobyink@cpan.org>.

Bugs

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

Description

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

   Object::Pad
        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',
          );
        }

   Class::Tiny
        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',
        );

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.

perl v5.40.1                                       2025-04-01                      Sub::HandlesVia::Declare(3pm)

Name

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

See Also

       Sub::HandlesVia.

Synopsis

        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.

See Also