http://www.perlmonks.org?node_id=11158499

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

I have some common modules that are used in several places so I have created a directory for them at /usr/lib/perl_modules. I want to include this location in @INC for every user, including CRON.

I've added export PERL5LIB=/usr/lib/perl_modules in both /etc/environment and /etc/profile.

When I list the environment variables, PERL5LIB is there as expected. But when I try to use on of the modules I get an error:

Can't locate my_module.pm in @INC (you may need to install the my_modu +le module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/ +perl/5.36.0 /usr/local/share/perl/5.36.0)
There are other locations in @INC but /usr/lib/perl_modules is not one of them...

I suspect the environment variable is set for root that I'm using to list the variables, but not for whatever process is running the script within Apache.

How can I properly set PERL5LIB for all users and processes or is there a better way to get an extra entry in @INC for every script without having to use lib in every script?

Replies are listed 'Best First'.
Re: PERL5LIB not in @INC
by hippo (Archbishop) on Mar 23, 2024 at 15:11 UTC
    but not for whatever process is running the script within Apache.

    Use SetEnv to set environment variables within Apache. You can do this at the global config level to have it apply to all scripts.


    🦛

Re: PERL5LIB not in @INC
by haj (Vicar) on Mar 23, 2024 at 15:29 UTC

    In my (Ubuntu) installation, @INC holds an entry /usr/local/lib/site_perl which, as far as I am aware, is intended to hold such "common modules". In my system, this directory does not exist, and I don't know where it comes from.

    For the special case of Apache: Usually Apache runs CGI processes in a rather bare environment. You can add your directory to the Apache configuration with the SetEnv directive.

      In my (Ubuntu) installation, @INC holds an entry /usr/local/lib/site_perl which, as far as I am aware, is intended to hold such "common modules"

      Likewise...I did notice that in my @INC but didn't investigate if it actually exists. It didn't, but it does now 🙂
      Problem solved!

      I've gone down this route instead of SetEnv because some of the common modules may be used outside of Apache at some point in the future.

Re: PERL5LIB not in @INC
by Anonymous Monk on Mar 23, 2024 at 15:59 UTC
    Smartest solution would be to just treat your code like actual code and put it in a new dist.

    Even smarter, put it in a repository and avoid losing it with the simplistic copy and paste setup you have now. Github is free. Even for private repos.

    Install your code with cpanm and this will be one less thing you'll have to be confused about.

    The smartest solution.