Hi,

The only drawback is performance : there is an additional indirection, and code dereferencing might be costly. The optimization of the route caching stops wher your route code starts, so it won't be optimized. You don't get performance hits ( or less, at least I think) with attributes.

Hm, it could be solved by calling  got &$code actually. But be careful about argument passing.


On 11 December 2012 13:39, David Precious <davidp@preshweb.co.uk> wrote:
On Tue, 11 Dec 2012 11:40:29 +0000
Daniel Perrett <dperrett@cambridge.org> wrote:

> Perhaps I'm missing something but...
>
> "how requires_login/requires_role would store the fact that the
> provided sub requires auth"
>
> Simple implementation:
>
> # given has_role is calculable
>
> sub requires_role {
>         my ($role, $code, $handle_exception) = @_;
>         return sub {
>                 if (has_role($role)){
>                         $code->(@_);
>                 }
>                 else{
>                         if (defined $handle_exception){
>                                 $handle_exception->($role, @_)
>                         }
>                         else {
>                                 default_403_no_role($role, @_)
>                         }
>                 }
>         }
> }

Ah, yes - that's pretty clever.  I think like that approach - it's
simple and solid.

Thanks!