[Dancer-users] applying the same setting to different routes in different packages

Alexis Sukrieh sukria at sukria.net
Tue Oct 5 08:54:04 CEST 2010


On 05/10/2010 00:04, P Kishor wrote:

 > While some of the "various settings" are different in the different
 > packages, some of them are the same. Definitely, 'logged_in' is the
 > same across all the packages, so I want to set it only in one place.

I see.

 > Since Dancer doesn't support the concept of creating a base class, and
 > then inheriting from it, how do I apply same settings to any route in
 > any package or sub-package?

Who told you you can use inheritance with Dancer packages? Dancer does 
nothing for you regarding inheritance, but that doesn't mean you can't 
do it yourself:


package FooApp::Base;

use strict;
use warnings;

use Dancer ':syntax';

sub opts {
     return { base => 42 };
}

get '/' => sub {
     "base opts : ".to_yaml(opts());
};

1;

package FooApp::Forum;

use strict;
use warnings;
use Dancer ':syntax';

use FooApp::Base;
use base 'FooApp::Base';

sub opts {
     my $self = shift;
     my $parent_opts = $self->SUPER::opts();
     return {
         %{ $parent_opts },
         forum => 34,
     };
}

get '/' => sub {
     "forum opts : ".to_yaml(FooApp::Forum->opts());
};

Tested with Dancer 1.1901 here, working.

Hope that helps,

-- 
Alexis Sukrieh


More information about the Dancer-users mailing list