[Dancer-users] [RFC] Change in prefix / before functionality

Alessandro Ranellucci aar at cpan.org
Fri Apr 1 10:38:51 CEST 2011

On 1-04-2011 at 10:21, Richard Huxton wrote:

  >I've tried to do something similar to Simon too and been disappointed.
  >I was trying:
  >load_app 'MyApp::Secret', prefix =>'/secret', before => &admin_only;
  >I think the surprising thing is the fact that "before" spans packages.
  >Within a package, I'm happy with multiple occurrences 
chaining, but it
  >seems wrong for an included module to change global behaviour.

You're close. This will work as you need:

     package MyApp;
     before sub { ... };    # do something

     package MyApp::Secret;
     before sub { ... };    # do something else

     package main;
     load_app 'MyApp';
     load_app 'MyApp::Secret', prefix => '/secret';

'before' hooks are scoped to a single Dancer app. Note that 
splitting your application in packages is not sufficient to have 
Dancer see them as different 'apps': you have to load them using 
load_app(). Everything (well, almost) that happens during that 
load_app() call is scoped to that particular app and not to the 
global application.

Note that while this works correctly for the example you 
provided, there are some issues that you may be interested in:



     - al.

More information about the Dancer-users mailing list