How can my Perl script find its module in the same directory?

后端 未结 9 1030
执念已碎
执念已碎 2020-12-23 19:26

I recently wrote a new Perl script to kill processes based on either process name / user name and extended it using Classes so that I could reuse the process code in other p

相关标签:
9条回答
  • 2020-12-23 20:04

    Besides the already stated solutions:

    1. use FindBin / lib
    2. Perl Faq 8 How do I add a directory to my include path (@INC) at runtime?

    'The simplest approach' (™) that I use while dev/testing a module prior to deploying it (in /usr/local/lib/site_perl/ or elsewhere in @INC) is to modify @INC before loading the module as follows:

    #!/usr/bin/perl
    use strict;
    use warnings;
    # Modify @INC prior to module loading.
    BEGIN { unshift @INC, '.'; }
    use YourModuleInCWD;
    

    (Add current working directory to @INC? - PerlMonks)

    0 讨论(0)
  • 2020-12-23 20:08

    FindBin::libs will find all your libs placed at reasonable places relative to the path from where your script is running.

    0 讨论(0)
  • 2020-12-23 20:09

    Just keep it simple. There is no need to import any libraries; just find out your current working directory:

    use lib "$ENV{PWD}/relativ_path_own_perllib";
    

    For similar problems, you can read out the environment variable which gives your information about where you are, your home directory, operating system stuff, and so on, with just one row of programming code in the shell-terminal, like:

    perl -e 'map { print; print " : ". $ENV{$_}." \n\r"; } sort keys %ENV '
    

    There is no need to bind some libraries; just use the %ENV-Hash.

    0 讨论(0)
提交回复
热议问题