[Dancer-users] separating code into packages

Puneet Kishor punk.kish at gmail.com
Fri Dec 24 16:32:40 CET 2010



Gabor Szabo wrote:
> 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.
>

Have you looked at `load_app`?

	package Foo.pm
	use Dancer ":syntax";

	load_app "Foo::User", prefix => "/user";


>
> 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
> _______________________________________________
> Dancer-users mailing list
> Dancer-users at perldancer.org
> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users



-- 
Puneet Kishor


More information about the Dancer-users mailing list