Hi All, Looking for some general advice on separating code/code bases easily with Dancer. I'm using Dancer on a fairly large project (relatively speaking) and it is getting to >2000 lines of code so far. The end product could be in the order of 3000 lines. This is all one product. Another one I'm working on is going to be several different products in one is about 500 lines but is soon going to be at least twice that when I add some stuff in. What is the best way to separate these code bases into separate files? I want to keep the same 'instance' of Dancer, which is currently running under Ubuntu and Windows Plackup respectively and running well. I could just link another pm file into the main .pm file, would that be enough? so I have lib/DancerWebsite.pm and then I just add another .pm file in that directory and load classes from that? Thanks, David
On Apr 22, 2015, at 6:42 PM, David H <untg99@gmail.com> wrote:
What is the best way to separate these code bases into separate files?
Dancer makes this really easy. The ‘dancer’ generator already created a “lib” directory for you and added it to the module path. By default, that lib directory only holds myapp.pm, but there’s nothing preventing you from adding more files to that tree. For my app (which has about 5x the amount of Perl code you’re talking about) I chose to keep lib/myapp.pm as the “Dancer core” module, containing little more than the route handlers. Those route handlers are mostly one-liners that call off into one of the other modules which live in lib/myapp/*.pm. At the top of lib/myapp.pm, you see a bunch of stuff like: use myapp::SomeComponent; use myapp::AnotherComponent; use etc… Those modules then export classes, functions, and globals in the normal way, for use by the Dancer core module. Sometimes I write these modules to require fully-qualified names rather than export function to global level. A route handler thus might look like this: prefix '/api' => sub { prefix '/sc' => { get ‘foo’ => sub { return myapp::SomeComponent::GetFoo(param ‘bar’); }; }; }; If you didn’t know you could nest things with “prefix” this way, aren’t you glad you know now? :) As you can see, I’m mirroring my Perl module component hierarchy with my URL hierarchy: /api/sc/* is handled by myapp::SomeComponent, etc.
Hi David Check this out: http://advent.perldancer.org/2014/9 I suspect it doesn't directly address your question since it's Dancer2 rather than Dancer, but seeing as you're using Plack your already half way there:) cheers Andrew On Thu, Apr 23, 2015 at 5:15 AM, Warren Young <wyml@etr-usa.com> wrote:
On Apr 22, 2015, at 6:42 PM, David H <untg99@gmail.com> wrote:
What is the best way to separate these code bases into separate files?
Dancer makes this really easy. The ‘dancer’ generator already created a “lib” directory for you and added it to the module path. By default, that lib directory only holds myapp.pm, but there’s nothing preventing you from adding more files to that tree.
For my app (which has about 5x the amount of Perl code you’re talking about) I chose to keep lib/myapp.pm as the “Dancer core” module, containing little more than the route handlers.
Those route handlers are mostly one-liners that call off into one of the other modules which live in lib/myapp/*.pm.
At the top of lib/myapp.pm, you see a bunch of stuff like:
use myapp::SomeComponent; use myapp::AnotherComponent; use etc…
Those modules then export classes, functions, and globals in the normal way, for use by the Dancer core module.
Sometimes I write these modules to require fully-qualified names rather than export function to global level. A route handler thus might look like this:
prefix '/api' => sub { prefix '/sc' => { get ‘foo’ => sub { return myapp::SomeComponent::GetFoo(param ‘bar’); }; }; };
If you didn’t know you could nest things with “prefix” this way, aren’t you glad you know now? :)
As you can see, I’m mirroring my Perl module component hierarchy with my URL hierarchy: /api/sc/* is handled by myapp::SomeComponent, etc. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Andrew Solomon Mentor@Geekuni http://geekuni.com/ http://www.linkedin.com/in/asolomon
Thanks guys gives me some great options. I am using both Dancer and Dancer2 (different projects) so that's good. On Fri, Apr 24, 2015 at 6:37 AM, Andrew Solomon <andrew@geekuni.com> wrote:
Hi David
Check this out:
http://advent.perldancer.org/2014/9
I suspect it doesn't directly address your question since it's Dancer2 rather than Dancer, but seeing as you're using Plack your already half way there:)
cheers
Andrew
On Thu, Apr 23, 2015 at 5:15 AM, Warren Young <wyml@etr-usa.com> wrote:
On Apr 22, 2015, at 6:42 PM, David H <untg99@gmail.com> wrote:
What is the best way to separate these code bases into separate files?
Dancer makes this really easy. The ‘dancer’ generator already created a “lib” directory for you and added it to the module path. By default, that lib directory only holds myapp.pm, but there’s nothing preventing you from adding more files to that tree.
For my app (which has about 5x the amount of Perl code you’re talking about) I chose to keep lib/myapp.pm as the “Dancer core” module, containing little more than the route handlers.
Those route handlers are mostly one-liners that call off into one of the other modules which live in lib/myapp/*.pm.
At the top of lib/myapp.pm, you see a bunch of stuff like:
use myapp::SomeComponent; use myapp::AnotherComponent; use etc…
Those modules then export classes, functions, and globals in the normal way, for use by the Dancer core module.
Sometimes I write these modules to require fully-qualified names rather than export function to global level. A route handler thus might look like this:
prefix '/api' => sub { prefix '/sc' => { get ‘foo’ => sub { return myapp::SomeComponent::GetFoo(param ‘bar’); }; }; };
If you didn’t know you could nest things with “prefix” this way, aren’t you glad you know now? :)
As you can see, I’m mirroring my Perl module component hierarchy with my URL hierarchy: /api/sc/* is handled by myapp::SomeComponent, etc. _______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- Andrew Solomon
Mentor@Geekuni http://geekuni.com/ http://www.linkedin.com/in/asolomon
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
participants (3)
-
Andrew Solomon -
David H -
Warren Young