Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Dancer2: Why is bundled 'lib/' required?

by kcott (Archbishop)
on Feb 01, 2022 at 22:03 UTC ( [id://11141027]=perlquestion: print w/replies, xml ) Need Help??

kcott has asked for the wisdom of the Perl Monks concerning the following question:

G'day All,

I have created and deployed a Dancer2 ($work) application. This is successful, I'm not looking for help with this. While doing this I encountered a problem (described below) and found a workaround. My question is why this workaround was required at all.

I initially created the application which had a file structure similar to what's shown in "Dancer2::Tutorial - Creating a new app". There was one module (let's say Private::Work::Module) in lib/Private/Work/Module.pm with tests in the standard t/ directory: make, make test and make install were all OK. I ran this standalone as 'plackup -p 5000 /path/to/script.psgi': everything worked fine.

The next step was deployment. I've basically used what's described in "Dancer2::Manual::Deployment - Running under Apache - With Plack". I copied what I considered were the appropriate files to /srv/www/ directories: I excluded all make-, git- and distribution-related files, as well as the complete lib/ and t/ directories.

When I tried the URL from a variety of browsers (including graphical Firefox and textual lynx) I got "500 Internal Server Error".

I finally tracked down the issue and, after copying the lib/ directory to the apropriate /srv/www/ directory, everything worked fine and all tests on the application were successful.

I will just note that I successfully tried 'use Private::Work::Module; print "$Private::Work::Module::VERSION\n"' as a non-root user from a directory unrelated to my source directory, any Apache directory, or any @INC directory.

So, why does the lib/ directory need to be bundled with the Dancer2 files? Other Perl modules that I used — core, CPAN, and Private::OtherWork::OtherModule — were all found, loaded and used without any problems.

— Ken

Replies are listed 'Best First'.
Re: Dancer2: Why is bundled 'lib/' required?
by bliako (Monsignor) on Feb 01, 2022 at 23:46 UTC

    Isn't it just a matter of finding out where make install installed Private::Work::Module from its output? And then check apache's INC and yours. If all else fails, bin/app.psgi tries to find required modules with this: use lib "$FindBin::Bin/../lib";

      G'day bliako,

      Both Private::Work::Module and Private::OtherWork::OtherModule are in the same "@INC" directory. Dancer2 finds the latter but not the former. Why would it find one but not the other?

      I thought you might be on to something with FindBin but a few tests indicate that's not the case. If /path/to/bin/../lib doesn't exist, it still gets prepended to @INC. Having a non-existent directory in @INC does not cause Perl any problems.

      $ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +' $ ls -al /home/ken/tmp/zxc/bin/../lib ls: cannot access '/home/ken/tmp/zxc/bin/../lib': No such file or dire +ctory ken@titan ~/tmp/zxc/bin $ perle 'use FindBin; use lib "$FindBin::Bin/../lib"; use JSON; say $J +SON::VERSION; say $INC{"JSON.pm"}; say for @INC;' 4.03 /home/ken/perl5/perlbrew/perls/perl-5.34.0/lib/site_perl/5.34.0/JSON.p +m /home/ken/tmp/zxc/bin/../lib /home/ken/perl5/perlbrew/perls/perl-5.34.0/lib/site_perl/5.34.0/cygwin +-thread-multi /home/ken/perl5/perlbrew/perls/perl-5.34.0/lib/site_perl/5.34.0 ...

      Thanks for the suggestions anyway.

      — Ken

Re: Dancer2: Why is bundled 'lib/' required?
by perlfan (Vicar) on Feb 04, 2022 at 05:38 UTC
    The whole point of PSGI is to get as far away from A-patch-y as possible. You might have a better time with starman behind a reverse proxy like haproxy, nginx, or varnishd. Principally, the "dance app" is just what's in bin/app.psgi or whatever you named it. Furthermore, there's nothing dependant about what's in ../lib than what you have yourself coded. E.g., app.psgi doesn't have to have any external dependencies other that the code Dancer module.

    If you look in bin/app.psgi generated by your referenced document, you will note:

    #!/usr/bin/perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; # use this block if you don't need middleware, and only have a single +target Dancer app to run here use Dancr2; Dancr2->to_app;
    So the line, use lib "$FindBin::Bin/../lib"; is the answer to your question. So you would want to replace that to ensure that the right path is in @INC. But if you can help it, get off the Apache plantation pronto. If it's getting invoked by Apache "as is", who the heck knows what the is going to translate into.

    And I imagine Dancer2 does care that views, public, etc are next to whatever lib you use to to_app. The initial use lib ... line just gets to the module that has the use Dancer2; line and defines the handlers. So YMMV.

      The OP read you should
        English, Francis. English.
Re: Dancer2: Why is bundled 'lib/' required?
by Anonymous Monk on Feb 01, 2022 at 22:14 UTC
    What was the error message?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11141027]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-18 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found