5

Inspired of How can I use CPAN as a non-root user? i'd like to know, how to set up cpanm for non-login users (like www)? There is no login, so i don't know how to include some dir to @INC.

Edit: How i could set permanently enviroment var like PERL5LIB for non-login user? I can't set it from .bashrc (needs login) and not from /etc/environment (sets to everyone).

Community
  • 1
  • 1
w.k
  • 8,218
  • 4
  • 32
  • 55
  • Maybe some suggestions here may help: http://unix.stackexchange.com/questions/88201/whats-the-best-distro-shell-agnostic-way-to-set-environment-variables/88229 – Slaven Rezic Sep 27 '13 at 17:04
  • @SlavenRezic: Thank you, but those solutions involve log-in or set variables for every user. – w.k Sep 27 '13 at 18:15
  • Did you look at the `~/.pam_environment` solution? Note the `~`. – Slaven Rezic Sep 27 '13 at 19:28
  • @SlavenRezic: I did, it says "is read by all login methods that use PAM". Non-system users don't use this file for setting env, as far i understand. For sake I tried it, it did not work for me. – w.k Sep 28 '13 at 17:42

2 Answers2

3

Methods of changing @INC:

On a per-script basis:

On a per-process basis:

  • Set the PERL5LIB env var from the parent process.
    • sh: export PERL5LIB=... ; perl ...
    • sh: PERL5LIB=... perl ...
    • apache: SetEnv PERL5LIB ...

On a per-perl basis:

  • Specifying the directories for @INC when configuring perl when building it.
  • use lib ...; in a script named sitecustomize.pl in the directory returned by perl -V:sitelib.[1]

Of course, you could also install your own local Perl, perhaps using perlbrew.


Notes:

  1. Perl only looks for sitecustomize.pl if The perl has been configured to do with -Dusesitecustomize. Check with perl -V:usesitecustomize to see if it was.
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • some reference to PERL5ENV? – mpapec Sep 26 '13 at 11:40
  • Outside of script, how you obtain it for a non-login (system) user? – w.k Sep 26 '13 at 12:05
  • Links to docs added. It's a `PATH`-like var, i.e. a colon-separated paths in which to search. (They're added to `@INC`.) Like `use lib` (and unlike `-I`), any arch subdirs will be added as well. – ikegami Sep 26 '13 at 13:26
  • `PERL5ENV` should be `PERL5LIB` ? How to set it for a non-login user? – w.k Sep 26 '13 at 14:08
  • Yes, `PERL5LIB`. A process's environment is always set by the parent process. What's the parent process? Apache? `SetEnv`. – ikegami Sep 26 '13 at 14:23
  • Which is parent process for `www-data` running different services? Like Apche, nginx, starman, Dancer, cron-scripts. Don't want set them up separately, but can't find solution. – w.k Sep 26 '13 at 14:45
  • You've previously said you don't want to set it on per-script basis. Now you're saying you don't want to set it on a per-process basis. That leaves setting it on a per-`perl` basis. (See update) – ikegami Sep 26 '13 at 16:57
  • No, i want it set **per-user** basis. I want that everything i run under this user uses separate modules. Setting it by parent process sounded as good idea to me, but what is a parent process for all those things i listed above? – w.k Sep 26 '13 at 17:20
  • Why are you asking me? How should I know what programs you have running on your machine, and which instances were used to create which other instances? – ikegami Sep 26 '13 at 17:34
  • I've listed your options. – ikegami Sep 26 '13 at 17:38
  • 1
    Of course, some of the options can be used to limit the effect to a particular user (e.g. `use if $USER eq ..., 'lib', ...;` instead of `use lib ...`). – ikegami Sep 26 '13 at 17:51
0

Make the first perl in your $PATH a shell script that checks for a file named after the invoking user, sources that file, and then calls the real perl:

    $ cat /usr/local/etc/perl.user.d/www-data
    PERL5LIB=/whatever/you/need
    export PERL5LIB
    $ cat /usr/local/bin/perl
    #! /bin/sh

    PERLBIN=/usr/bin/perl

    USER_CUSTOMIZE=/usr/local/etc/perl.user.d/$(whoami)

    [ -e "$USER_CUSTOMIZE" ] && source "$USER_CUSTOMIZE"

    exec $PERLBIN "$@"

N.B. I don't heartily recommend this. It's similar to sitecustomize.pl suggested elsewhere, but more hackish. Like the others who have answered, I don't think it would be too arduous to identify all of the parent processes you care about (apache, cron, and ... ?) and set PERL5LIB there.

Community
  • 1
  • 1
pilcrow
  • 56,591
  • 13
  • 94
  • 135