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

I'm trying to use perlbrew with apache and my head hurts!

I have a machine where I don't want to use the system perl with my applications, so I've installed perlbrew. It works fine for me, and it works fine for another user sharing my installation, but I can't figure out how to make it work for apache CGI scripts.

I know this is a question that's been asked lots of times in various places, because google tells me so. But I haven't found any articles that answer all my questions.

The first stumbling block of course is that apache doesn't have a login account, so can't setup perlbrew in the normal way. So the common wisdom appears to be to use the shebang line in the CGI scripts to hardwire a particular perlbrew perl. So OK, it feels a bit clunky because I have to modify every script every time I change perls, but hey. So I do that and discover that now it can't find the modules so I add a bunch of use lib statements to the scripts as well.

That gets the script running, but all the script does is massage its arguments and 'system' an actual application program. So I add another hack that discovers the perl running the script and adds that to the 'system' call that runs the application. And after doing some tweaking to satisfy taint, that works too. But I have a nagging worry about which libraries it's using and it all feels kludgy.

Except that the application program in turn executes various other applications in some circumstances, so before I can investigate my library doubts I'm faced with yet another case of the wrong perl.

Now sure I could edit my application and however many other applications until it all works, but there's really got to be a better way! It feels like I'm falling down the rabbit hole. So I thought before I dive off and explore all kinds of wacky possibilities, I'd supplicate myself at the gates of the monastery and ask pretty please if anybody already knows a good way to do this?

Replies are listed 'Best First'.
Re: using perlbrew with apache
by ikegami (Patriarch) on Feb 12, 2016 at 16:51 UTC

    So OK, it feels a bit clunky because I have to modify every script every time I change perls,

    It is clunky. In theory, you should relaunch the script's installer using the new perl. But since you're doing the installation manually, you have to do this manually too.

    But it can easier than the way you're doing it. Create a symlink to the `perl` you want to use in production, and use that symlink in the shebang. When you've installed a new version of `perl` and you've tested it with your web app, just update the symlink.

    So I add another hack that discovers the perl running the script and adds that to the 'system' call that runs the application.

    Sounds like you forgot to update a shebang.

    But I have a nagging worry about which libraries it's using and it all feels kludgy.

    @INC is hardcoded into the installation. As long as you don't add anything using PERL5LIB or similar, you'll get the right perl installation's modules.

      Thanks. I've now got it working. I pointed the shebang line in the CGI script to the perlbrew perl (I'll probably change that to a symlink as you suggest at the next go-around). Then I assigned to $ENV{PERLBREW_ROOT} and $ENV{PERLBREW_HOME} in the CGI script before running an application. All the applications have !#/usr/bin/env perl shebangs in the usual way and that seems to work. Thanks for the reassurance about the libraries.

Re: using perlbrew with apache
by 1nickt (Canon) on Feb 12, 2016 at 17:07 UTC

    So I do that and discover that now it can't find the modules

    Because you installed "the modules" with the other Perl, so they went into that Perl's library. If you are using a perlbrew Perl then that Perl needs to have the modules installed with it.

    One thing I do a lot is create a base perlbrew Perl installing either nothing but core Perl, or nothing but core Perl and certain common extensions, and then use perlbrew lib based upon that previously created Perl to house application-specific modules.

    Then after that you will either have to use the shebang line, or else do some path wizardry on top of perlbrew, which is a bit silly since that's what it's for.

    The way forward always starts with a minimal test.

      Thanks