Mojo::D...Methods(3)
π NAME π
Okay so this is the Mojo::D...Methods(3) page from User Contributed Perl Documentation, and the main character is 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. Itβs giving helper magic, but make it fast. π
To opt your class into dynamic dispatch, simply pass the -dispatch flag:
use Mojo::DynamicMethods -dispatch;
Then implement a BUILD_DYNAMIC method in your class, and make 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. Thatβs the whole function, bestie. π«Ά
π SEE ALSO π
MojoliciousMojolicious::Guides<https://mojolicious.org>
Brought to you by perl v5.42.0, dated 2026-07-15. Stay iconic. π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms