Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Best practices for local libraries

by NERDVANA (Deacon)
on Dec 08, 2019 at 02:13 UTC ( [id://11109828]=note: print w/replies, xml ) Need Help??


in reply to Best practices for local libraries

Echoing some other ideas here, I recommend putting all your utility code into modules in your own namespace (in a version-controlled directory) and then:

  1. Quick and dirty option: add a symlink to your modules into an existing perl include path
  2. More robust option: use Dist::Zilla to build actual packages of your modules, which you might even decide to publish on cpan

The first option will get you up and running the quickest, and even help transfer your modules around (along with bug fixes for your modules) from computer to computer with the help of git. You use the existing perl lib path, so no need to clutter your scripts with "use lib ..." boilerplate. If package management looses your symlink you can put it back easily. You can also add your modules to every lib path of every perl you use while only maintaining one copy.

Later, when you find time to learn Dist::Zilla and clean up your APIs, you can make them into official perl packages which you can install into system-perl or into per-user perlbrew using "cpanm MyPackage-version.tar.gz".

Replies are listed 'Best First'.
Re^2: Best practices for local libraries
by dsheroh (Monsignor) on Dec 08, 2019 at 09:27 UTC
    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 in @INC 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 bar me@host:~$ sudo -i [sudo] password for me: root@host:~# echo $FOO root@host:~# exit logout me@host:~$ sudo bash root@host:/home/me# echo $FOO root@host:/home/me#
    If you prefer su over sudo, 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 page:
               Note that the default behavior for the environment is the
               following:
    
                   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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109828]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found