Re: [Dancer-users] applying the same setting to different routes in different packages
On Tue, Oct 5, 2010 at 1:53 AM, Alexis Sukrieh <sukria@sukria.net> wrote:
On 05/10/2010 00:04, P Kishor wrote: ..
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,
This is great news, as it will greatly simplify my application. However, it doesn't work for me (also on 1.1901). My app structure is app.pl lib/ app.pm app/ app1.pm in app1.pm, I have ---- package app::app1; use Dancer ':syntax'; use lib ('/Users/punkish/Sites/app/lib'); use app; # do I really need this and the line just above it? use base 'app'; sub opts { my $self = shift; my $opts = $self->SUPER::opts(); return { %{ $opts }, app1 specific opts } }; ---- and, I get the following runtime error Can't call method "SUPER::opts" on an undefined value at /Users/punkish/Sites/app/lib/app/app1.pm line 16. /Users/punkish/Sites/app/lib/app/app1.pm around line 16 13 14 sub opts { 15 my $self = shift; 16 my $opts = $self->SUPER::opts(); 17 18 return { 19 %{ $opts },
-- Alexis Sukrieh
-- 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 =======================================================================
Le 05/10/2010 14:32, P Kishor a écrit :
and, I get the following runtime error
Can't call method "SUPER::opts" on an undefined value at /Users/punkish/Sites/app/lib/app/app1.pm line 16.
The way you call the opts method matters: get '/' => sub { "forum opts : ".to_yaml(FooApp::Forum->opts()); }; If you don't call it that way (Class->method) $self is undef, hence your error. -- Alexis Sukrieh
On Tue, Oct 5, 2010 at 7:46 AM, Alexis Sukrieh <sukria@sukria.net> wrote:
Le 05/10/2010 14:32, P Kishor a écrit :
and, I get the following runtime error
Can't call method "SUPER::opts" on an undefined value at /Users/punkish/Sites/app/lib/app/app1.pm line 16.
The way you call the opts method matters:
get '/' => sub { "forum opts : ".to_yaml(FooApp::Forum->opts()); };
If you don't call it that way (Class->method) $self is undef, hence your error.
Fantastic! all is well now. This is really great, and might be worth adding to the cookbook. For example, this will also allow me to declare other singletons such as $dbh, for a db handle, in one place and the reuse it in other packages. Many thanks.
-- Alexis Sukrieh
-- Puneet Kishor
On Tue, Oct 5, 2010 at 7:53 AM, P Kishor <punk.kish@gmail.com> wrote:
On Tue, Oct 5, 2010 at 7:46 AM, Alexis Sukrieh <sukria@sukria.net> wrote:
Le 05/10/2010 14:32, P Kishor a écrit :
and, I get the following runtime error
Can't call method "SUPER::opts" on an undefined value at /Users/punkish/Sites/app/lib/app/app1.pm line 16.
The way you call the opts method matters:
get '/' => sub { "forum opts : ".to_yaml(FooApp::Forum->opts()); };
If you don't call it that way (Class->method) $self is undef, hence your error.
even better to call it as __PACKAGE__->opts(); This allows me to reuse that statement in all the sub packages without having to change the name of the class. Man, the fewer keystrokes I have to type to make Dancer work means fewer chances of screwing up. This is getting better and better.
Fantastic! all is well now.
This is really great, and might be worth adding to the cookbook. For example, this will also allow me to declare other singletons such as $dbh, for a db handle, in one place and the reuse it in other packages.
Many thanks.
-- Alexis Sukrieh
-- Puneet Kishor
participants (2)
-
Alexis Sukrieh -
P Kishor