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

Joel Roth joelz at pobox.com
Mon Jan 28 05:09:07 GMT 2013


On Mon, Jan 28, 2013 at 03:48:19AM +0000, gvim wrote:
> 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.

Hi gvim,

Each module needs to "use Dancer ':syntax'". According to 'man
dancer':

    Note that a package loaded using load_app must import Dancer
    with the ":syntax" option, in order not to change the
    application directory (which has been previously set for the
    caller script).

To double-check my answer to this question, I just took at
gander at Assaf Gordon's Cowbell sample code.

http://cowbell.cancan.cshl.edu/

If you want Perl version 5.014 syntax in a module, you need
'use 5.014'.

cheers,

Joel
 
> 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;
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users

-- 
Joel Roth


More information about the dancer-users mailing list