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

P Kishor punk.kish at gmail.com
Tue Oct 5 14:32:52 CEST 2010


On Tue, Oct 5, 2010 at 1:53 AM, Alexis Sukrieh <sukria at 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
=======================================================================


More information about the Dancer-users mailing list