The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Patterns::ChainOfResponsibility::Role::Handler - A Link in a Chain of Responsibility

SYNOPSIS

    package Loader::Filesystem;

    use Moose;
    with 'MyApp::Loader';
    with 'Patterns::ChainOfResponsibility::Role::Handler', {dispatcher => '::Provider'};

    sub handle {
        my ($self, @args) = @_;
        if($something_works) {
            return @returned_info;
        } else {
            return;
        }
    }

    ...

    my $io_handle = Loader::Filesystem
        ->new(%options)
        ->next_handlers(@handlers)
        ->process(@args);

    

DESCRIPTION

This is a role to be consumed by any class whose instances are links in a chain of responsibility. Classes must define a "handle" method.

PARAMETERS

This role defines the following parameters

handler_method

This is the name of the method in the target class which is handling args sent to "process". By default its 'handle' but you can override as follows:

    with 'Patterns::ChainOfResponsibility::Role::Handler', {
        handler_method => 'execute',
    };

This method should return processed args or nothing in the case of an error.

dispatcher

This is the dispatcher strategy to be used. This must be a class that consumes the role Patterns::ChainOfResponsibility::Role::Dispatcher. This distribution includes three dispatchers which should cover the standard uses, but you can write your own.

::Provider

This is probably the most 'classic' dispatcher. Processes each handler until one of them returns true. The first handler to 'handle' the job wins.

    with 'Patterns::ChainOfResponsibility::Role::Handler', {
        dispatcher => '::Provider',
    };

See Patterns::ChainOfResponsibility::Provider

::Filter

Similar to a UNIX commandline pipe. Each hander gets the return of the previous until all have been handled (and the final filtered results are returned) or when one handler returns nothing.

    with 'Patterns::ChainOfResponsibility::Role::Handler', {
        dispatcher => '::Filter',
    };

See Patterns::ChainOfResponsibility::Filter

::Broadcast

Each handler gets the original args. All handers are processed in order until the last handler is finished or one handler returns null. Good for something like a logger where you have multiply and independent loggers

    with 'Patterns::ChainOfResponsibility::Role::Handler', {
        dispatcher => '::Broadcast',
    };

See Patterns::ChainOfResponsibility::Broadcast

If you write a custom dispatcher, you should include the full namespace name in the parameter, as in:

    with 'Patterns::ChainOfResponsibility::Role::Handler', {
        dispatcher => 'MyApp::Dispatcher::MyCustomDispatcher',
    };

METHODS

This role defines the following methods

process ( @args)

The arguments you want one or more handler to process

next_handlers (@handlers)

Add more handlers to the end of the chain

handle (@args)

Gets @args sent to "process". Should return either processed arguments or a null terminated 'return' (see "SYNOPSIS".

AUTHOR

John Napiorkowski <jjnapiork@cpan.org>

LICENSE & COPYRIGHT

Copyright 2011, John Napiorkowski <jjnapiork@cpan.org>

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