GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Mojo::DynamicMethods(3) User Contributed Perl Documentation Mojo::DynamicMethods(3)
 

Mojo::DynamicMethods - Fast dynamic method dispatch

  package MyClass;
  use Mojo::Base -base;
  use Mojo::DynamicMethods -dispatch;
  sub BUILD_DYNAMIC {
    my ($class, $method, $dyn_methods) = @_;
    return sub {...};
  }
  sub add_helper {
    my ($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;

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 {
    my ($class, $method, $dyn_methods) = @_;
    return sub {
      my ($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 is EXPERIMENTAL and might change without warning!

Mojo::DynamicMethods implements the following functions.

  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".

Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
2018-11-30 perl v5.28.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.