Class-Delegator

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


    In this example, the "accelerate" method will be delegated to the
    "start" method of the "brakes" attribute and the "decelerate" method
    will be delegated to the "stop" method of the "brakes" attribute.

  Delegation to multiple attributes in parallel
    An array reference can be used as the value of the "to" parameter to
    specify the a list of attributes, *all of which* are delegated to--in
    the same order as they appear in the array. In this case, the "send"
    parameter must be a scalar value, not an array of methods to delegate.

    For example, to distribute invocations of "$self->drive(...)" to both
    "$self->{left_rear_wheel}->drive(...)" and
    "$self->{right_rear_wheel}->drive(...)":

      use Class::Delegator
          send => 'drive',
            to => ["{left_rear_wheel}", "{right_rear_wheel}"]
      ;

    Note that using an array to specify parallel delegation has an effect on
    the return value of the delegation method specified by the "send"
    parameter. In a scalar context, the original call returns a reference to
    an array containing the (scalar context) return values of each of the
    calls. In a list context, the original call returns a list of array
    references containing references to the individual (list context) return
    lists of the calls. So, for example, if the "cost" method of a class
    were delegated like so:

      use Class::Delegator
          send => 'cost',
            to => ['supplier', 'manufacturer', 'distributor']
      ;

    then the total cost could be calculated like this:

      use List::Util 'sum';
      my $total = sum @{$obj->cost()};

    If both the "to" key and the "as" parameters specify multiple values,
    then each attribute and method name form a pair, which is invoked. For
    example:

      use Class::Delegator
          send => 'escape',
            to => ['{flywheel}', '{smokescreen}'],
            as => ['engage',   'release'],
      ;

    would sequentially call, within the "escape()" delegation method:

      $self->{flywheel}->engage(...);
      $self->{smokescreen}->release(...);

Benchmarks
    I whipped up a quick script to compare the performance of
    Class::Delegator to Class::Delegation and a manually-installed
    delegation method (the control). I'll let the numbers speak for
    themselves:

      Benchmark: timing 1000000 iterations of Class::Delegation, Class::Delegator, Manually...
      Class::Delegation: 106 wallclock secs (89.03 usr +  2.09 sys = 91.12 CPU) @ 10974.54/s  (n=1000000)
      Class::Delegator:    3 wallclock secs ( 3.44 usr +  0.02 sys =  3.46 CPU) @ 289017.34/s (n=1000000)
               Control:    3 wallclock secs ( 3.01 usr +  0.02 sys =  3.03 CPU) @ 330033.00/s (n=1000000)

Bugs
    Please send bug reports to <bug-class-delegator@rt.cpan.org> or report
    them via the CPAN Request Tracker at
    <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-Delegator>.

Author
    David Wheeler <david@kineticode.com>

See Also
    Class::Delegation
        Damian Conway's brilliant module does ten times what this one
        does--and does it ten times slower.

    Class::Delegate
        Kurt Starsinic's module uses inheritance to manage delegation, and
        has a somewhat more complex interface.

    Class::HasA
        Simon Cozen's delegation module takes the same approach as this
        module, but provides no method for resolving method name clashes the
        way this module's "as" parameter does.

Copyright and License
    Copyright (c) 2005-2008 David Wheeler. Some Rights Reserved.

    This module is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.



( run in 1.004 second using v1.01-cache-2.11-cpan-71847e10f99 )