[Dancer-users] separating code into packages

Gabor Szabo szabgab at gmail.com
Fri Dec 24 08:12:23 CET 2010


I have not reached that point yet but as your application grows it
might be better
to move some code and with that some route definition to separate files.
For example if I have a Dancer application called Foo I might
want to move all the user related methods

get '/user/this' => sub { ... };
get '/user/that' => sub { ... };
get '/user/other' => sub { ... };

to a package called Foo::User;

I can then include

use Foo::User;

in the main Foo package and the above routes are added.

Within Foo::User the /user prefix seems to be too repetitive so
it might be better to have just

get '/this' => sub { ... };
get '/that' => sub { ... };
get '/other' => sub { ... };

and mount that package to '/user' within the Foo application.
(which by itself might have been mounted to some path.


If I understood it correctly from http://advent.perldancer.org/2010/2
and from the recent presentation of Sawyer, this would be getting similar to
what MVC frameworks do and what you referred to as "Namespace matching".

As I was playing with this idea I implemented a small example I call
Dancer::Plugin::MVC for now that  would do this automatically.
You just include

use Dancer::Plugin::MVC;

in the main Foo package and during its import() call it will load all
the modules
in the subdirectories of Foo. So far it simply loads the modules it
finds which is
quite simple.

I'll try to check if I can mount the various modules in other places
as well though in
that case I might not want to do it for all the modules and the mount
point should
be the same as the package name.

What do you think?
Is this totally contradicting the philosophy of Dancer?


Gabor


More information about the Dancer-users mailing list