NAME

DesignPattern::DispatchTable - Implementation of a reusable versatile dispatch table.

VERSION

Version 0.01

SYNOPSIS

This module implements the DispatchTable design pattern, for code reuse, readability, organisation, better coding style, and fun.

use DesignPattern::DispatchTable;

my $dispatch_table = DesignPattern::DispatchTable;
$dispatch_table->register( $string     => $code_ref );
$dispatch_table->register( qr{regexp?} => $code_ref );

# Or even

my $dispatch_table = DesignPattern::DispatchTable(
    $string     => $code_ref,
    qr{regexp?} => $code_ref
);

# and then you might call:
$dispatch_table->call($argument);    # calls the first matching method

EXPORT

A list of functions that can be exported. You can delete this section if you don't export anything, such as for a purely object-oriented module.

FUNCTIONS

new

Constructor. Returns a new Dispatch Table. Accepts an optional list of $condition => $code_ref pairs to be installed in the Dispatch Table.

register

Register accepts one or more pairs of $condition => $code_ref and install them in the Dispatch Table. Returns a true value if all values were installed successfuly (i.e., there is no invalid values as key and all keys have a proper code reference to go with it).

New versions of handlers (and corresponding decision code) are installed in the beginining of the list. This makes for an implicit guarantee of reverse order: I will always check first for the latest installed (decision, handlers) code pairs.

call

Call receives one or more values on which to make a decision about, and calls the first matching decision on the values. If the decision was made with a regular expression, it also arranges so the captured values are passed to the code reference registered.

TODO: in case we allow passing in subroutines as decision making processes, we should consider passing in the return value from the decision sub to the call handler.

code_for

Obtains the code for the given argument, and returns it. Used internally to fetch the code handler bound to a specific (code|string|regex) in the dispatch table.

AUTHOR

Luis Motta Campos, <lmc at cpan.org>

BUGS

Please report any bugs or feature requests to bug-designpattern-dispatchtable at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DesignPattern-DispatchTable. I will be notified, and then you' ll automatically be notified of progress on your bug as I make changes .

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc DesignPattern::DispatchTable

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT & LICENSE

Copyright 2009 Luis Motta Campos, all rights reserved.

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