Sub::Rate::NoMaxRate - auto calculate max_rate
my $rate = Sub::Rate::NoMaxRate->new; $rate->add( 10 => sub { ... } ); # sub1 $rate->add( 20 => sub { ... } ); # sub2 my $func = $rate->generate; # Calling this $func then: # sub1 will be called by rate about 10/30 # sub2 will be called by rate about 20/30 $func->();
Sub::Rate::NoMaxRate is a subclass of Sub::Rate.
This module has no max_rate option and calculate it automatically.
max_rate
my $obj = Sub::Rate->new;
Create Sub::Rate object.
Available options are:
rand_func => 'CodeRef'
Random calculate function. Default is:
sub { CORE::rand($_[0]); };
You can change random function to your own implementation by this option. max_rate is passed as $_[0] to this function.
$_[0]
$obj->add( 10, sub { ... } ); $obj->add( 20, sub { ... } ); $obj->add( 'default', sub { ... } );
Add $sub to internal sublist rate by $rate.
$sub
$rate
If $rate is not number but "default", then $sub is registered as default sub. If default sub is already registered, it will be replaced.
my $sub = $obj->generate;
Create a new sub that dispatch functions by its rates.
$obj->clear;
Clear all registered functions and default function.
Daisuke Murase <typester@cpan.org>
Copyright (c) 2012 KAYAC Inc. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
To install Sub::Rate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sub::Rate
CPAN shell
perl -MCPAN -e shell install Sub::Rate
For more information on module installation, please visit the detailed CPAN module installation guide.