Quick and dirty option: add a symlink to your modules into an existing perl include path
The OP mentions using apt-get install
, which strongly implies he's using a Debian or Debian-derived system. A default Debian install will have /usr/local/lib/site_perl
for precisely this purpose. But this does, obviously, make any modules linked there available to all users on the machine rather than user-specific, which may or may not be acceptable to the OP.
Really, though, the only real problem I see in the OP is the bit about the user's $PERL5LIB carrying over to root when using su. My solution to that is, quite simply, to use sudo instead, as it does not carry over the original user's environment:
me@host:~$ export FOO=bar
me@host:~$ echo $FOO
me@host:~$ sudo -i
[sudo] password for me:
root@host:~# echo $FOO
me@host:~$ sudo bash
root@host:/home/me# echo $FOO
If you prefer su
, you should be able to get the same effect by using su -
or su --login
to open a login shell with a fresh environment instead of preserving the previous shell's environment. From the Debian 10 version of man su
For backward compatibility, su defaults to not change the current di‐
rectory and to only set the environment variables HOME and SHELL (plus
USER and LOGNAME if the target user is not root). It is recommended to
always use the --login option (instead of its shortcut -) to avoid side
effects caused by mixing environments.
and, from the Debian 8/9 versions of the man
Note that the default behavior for the environment is the
The $HOME, $SHELL, $USER, $LOGNAME, $PATH, and $IFS environment
variables are reset.
If --login is not used, the environment is copied, except for
the variables above.
If --login is used, the $TERM, $COLORTERM, $DISPLAY, and
$XAUTHORITY environment variables are copied if they were set.
Other environments might be set by PAM modules.
So just add --login
to your su
command and $PERL5LIB
won't be carried over to your root shell.