[dancer-users] Scope of modules and 'use' directives

gvim gvimrc at gmail.com
Mon Jan 28 03:48:19 GMT 2013


I'm developing a Dancer app with the setup outlined below (ie. modules 
'use'-ing other modules) but I'm confused about when to 'use' various 
modules and Perl version statements. My config.yml has Template Toolkit 
selected so my first question is do I need to specify 'use Template' in 
each module that uses the 'template()' call or is that all taken care of 
once the config has been specified. If it is taken care of, does that 
apply to all the 'nested' modules? Next, do I need to specify 'use 
Dancer qw(:syntax)' in every module which makes use of Dancer functions? 
Again with 'use 5.014' - in every module or is once enough in the 
App::Routes module, for example? I suppose what I'm unclear about is 
what an imported module can access that is in the importing module/script.

gvim

****************************************
package App::Routes;

use 5.014;
use Dancer qw(:syntax);
use App::Login;
use App::Logout;
use App::Register;
........

post '/login'        => sub { App::Login::run };
post '/logout'       => sub { App::Logout::run };
post '/register'     => sub { App::Register::run };
.........

1;

**************** App/Register.pm ************************
package App::Register;

use 5.014;
use Dancer qw(:syntax);
use App::Validate;

sub run {
   my $valids = validate(params());     # Validate and process any 
errors then return %form
   ............
   template 'register_user', {form => $valids};
};

1;

***************** App/Validate.pm ***************************
package App::Validate;

use 5.014;
use Data::FormValidator;
use Dancer qw(:syntax);

BEGIN {
   use Exporter;
   our @ISA = 'Exporter';
   our @EXPORT = 'validate';
}

sub validate {
   my $params = shift;
   my $results = Data::FormValidator->check($params, \%profile);
   .........
   template 'errors', {invalid => $results->invalid, missing => 
$results->missing};
   return $results->valid;
}

my %profile = (
............
);

1;










More information about the dancer-users mailing list