Class::Method::Delegate - Perl extension to help you add delegation to your classes
use Class::Method::Delegate; use Package::To::Delegate::To; delegate methods => [ 'hello', 'goodbye' ], to => sub { Package::To::Delegate::To->new() }; delegate methods => [ 'wave' ], to => sub { shift->{gestures} }; delegate methods => [ 'walk', 'run' ], to => sub { self->{movements} ||= Package::To::Delegate::To->new() }; delegate methods => [ 'walk', 'run' ], to => \&some_subroutine, handlers => [ 'slow', 'fast' ];
Creates methods on the current class which delegate to an object.
delegate takes a hash or hashref with the following keys.
methods
Takes an array ref of strings that represent the name of the method to be delegated.
to
a sub block that returns an object, which the method calls will be sent to.
If the object you are delegating to has a method called delegated_by, then this will be called when delegating. The $self of the package doing the delegating will be passed in, so you can then store it.
delegate
Check out Class:Delegator and Class::Delegation for alternatives.
Jonathan Taylor, <jon@stackhaus.com>
Version 1.03 contributed by James Buster
Copyright (C) 2012 by Jonathan Taylor
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.
To install Class::Method::Delegate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Method::Delegate
CPAN shell
perl -MCPAN -e shell install Class::Method::Delegate
For more information on module installation, please visit the detailed CPAN module installation guide.