manpagez: man pages & more
man Mojo::DynamicMethods(3)
Home | html | info | man
Mojo::DynamicMethods(3)<



NAME

       Mojo::DynamicMethods - Fast dynamic method dispatch


SYNOPSIS

         package MyClass;
         use Mojo::Base -base, -signatures;

         use Mojo::DynamicMethods -dispatch;

         sub BUILD_DYNAMIC ($class, $method, $dyn_methods) {
           return sub {...};
         }

         sub add_helper ($self, $name, $cb) {
           Mojo::DynamicMethods::register 'MyClass', $self, $name, $cb;
         }

         package main;

         # Generate methods dynamically (and hide them from "$obj->can(...)")
         my $obj = MyClass->new;
         $obj->add_helper(foo => sub { warn 'Hello Helper!' });
         $obj->foo;


DESCRIPTION

       Mojo::DynamicMethods provides dynamic method dispatch for per-object
       helper methods without requiring use of "AUTOLOAD".

       To opt your class into dynamic dispatch simply pass the "-dispatch"
       flag.

         use Mojo::DynamicMethods -dispatch;

       And then implement a "BUILD_DYNAMIC" method in your class, making sure
       that the key you use to lookup methods in $dyn_methods is the same
       thing you pass as $ref to "register".

         sub BUILD_DYNAMIC ($class, $method, $dyn_methods) {
           return sub ($self, @args) {
             my $dynamic = $dyn_methods->{$self}{$method};
             return $self->$dynamic(@args) if $dynamic;
             my $package = ref $self;
             croak qq{Can't locate object method "$method" via package "$package"};
           };
         }

       Note that this module will summon Cthulhu, use it at your own risk!


FUNCTIONS

       Mojo::DynamicMethods implements the following functions.

   register
         Mojo::DynamicMethods::register $class, $ref, $name, $cb;

       Registers the method $name as eligible for dynamic dispatch for $class,
       and sets $cb to be looked up for $name by reference $ref in a dynamic
       method constructed by "BUILD_DYNAMIC".


SEE ALSO

       Mojolicious(3), Mojolicious::Guides(3), <https://mojolicious.org>.



perl v5.32.1                      2021-12-08           Mojo::DynamicMethods(3)

mojolicious 9.260.0 - Generated Mon May 30 18:19:27 CDT 2022
© manpagez.com 2000-2025
Individual documents may contain additional copyright information.