John Napiorkowski


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


    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 {


    my $io_handle = Loader::Filesystem



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.


This role defines the following parameters


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.


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.


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


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


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',


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".


John Napiorkowski <>


Copyright 2011, John Napiorkowski <>

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