applying the same setting to different routes in different packages
A stylistic question, perhaps a strategy question. This is what I do right now - foo.pl --- use Dancer; .. load_app 'foo'; dance; ---- - foo.pm --- load_app 'foo:bar', prefix => '/bar'; load_app 'foo:baz', prefix => '/baz'; get '/' => sub { template 'index', opts(); }; sub opts { return { various settings; 'logged_in' => session->{logged_in} ? 1 : 0; } } ---- - foo/foo.pm --- get '/' => sub { template 'index', opts(); }; sub opts { return { various settings; 'logged_in' => session->{logged_in} ? 1 : 0; } } ---- - foo/bar.pm --- get '/' => sub { template 'index', opts(); }; sub opts { return { various settings; 'logged_in' => session->{logged_in} ? 1 : 0; } } ---- 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. 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? -- Puneet Kishor http://www.punkish.org Carbon Model http://carbonmodel.org Charter Member, Open Source Geospatial Foundation http://www.osgeo.org Science Commons Fellow, http://sciencecommons.org/about/whoweare/kishor Nelson Institute, UW-Madison http://www.nelson.wisc.edu ----------------------------------------------------------------------- Assertions are politics; backing up assertions with evidence is science =======================================================================
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
participants (2)
-
Alexis Sukrieh -
P Kishor