develooper Front page | perl.moose | Postings from April 2013

Re: Short-circuit "before"

Thread Previous | Thread Next
Chris Prather
April 29, 2013 18:18
Re: Short-circuit "before"
Message ID:
On Mon, Apr 29, 2013 at 2:03 PM, Karen Etheridge <> wrote:

> On Mon, Apr 29, 2013 at 10:45:57AM -0700, Bill Moseley wrote:
> > What we are after is somewhat of a plugin system built with Roles where a
> > method in each Role is called in some defined order and once successful
> no
> > more are run.
> > Anyone have a better suggestion than our "before authenticate" approach?
> (in principle) roles should be able to be applied in any order and not be
> aware of/care about what other roles have been applied.  If you need method
> modifiers to be aware of each other's existence, I would suggest creating
> an attribute (via another role, that the other roles would 'requires' to be
> applied) which can share the necessary state information.
> Altneratively, you might find the augment/inner method modification pattern
> to be of use -- a method can always opt to not call inner() if it has had
> its preconditions satisfied already.  (Actually, this is probably better
> than my first suggestion.)
I'm not sure augment/inner is ever the right answer, but it's certainly
better than relying upon Role application order.

In this situation I'd argue that you want an array of Authentication
Objects that all do the same API-style role.

role_type AuthPlugin => { role => 'MyApp::AuthPlugin' };

has auth_plugins => ( isa => 'ArrayRef[AuthPlugin]', traits => ['Array'],
handles => { auth_plugins => 'elements' });

sub authenticate {
    my $self = shift;
    return if $_->authenticate(@_) for $self->auth_plugins;
    confess "Authentication Failed"

That way you can add arbitrarily many auth plugins to the list in a
specific order.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About