[dancer-users] A way to separate code into smaller parts/files
Andrew Solomon
andrew at geekuni.com
Thu Apr 23 22:07:27 BST 2015
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 at etr-usa.com> wrote:
> On Apr 22, 2015, at 6:42 PM, David H <untg99 at 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 at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
--
Andrew Solomon
Mentor at Geekuni http://geekuni.com/
http://www.linkedin.com/in/asolomon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150423/a3472eb5/attachment.html>
More information about the dancer-users
mailing list