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

后端 未结 9 1061
执念已碎
执念已碎 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 19:54

    The simplest approach I found it to use FindBin module. Like this:

    use FindBin;
    use lib $FindBin::Bin;
    

    Generally I prefer to have my scripts provided in such a way that programs are in whatever/bin, and libraries are in whatever/lib

    In these situations I use a slightly more complicated approach:

    use Cwd qw(abs_path);
    use FindBin;
    use lib abs_path("$FindBin::Bin/../lib");
    

    The abs_path call is to make the @INC contain whatever/lib, and not whatever/bin/../lib - it's just a slight change, but makes reading error messages easier.

提交回复
热议问题