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

后端 未结 9 1028
执念已碎
执念已碎 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:00

    I am curious why the simplistic solution

    use File::Basename;
    use lib dirname (__FILE__);
    use SomeModuleLocatedInTheSameDirectoryAsThisPerlScriptOrModule;
    

    did not come up. The FindBin module seems to have some issues if the file is not the main executable perl script, but simply a non-executable Perl module. At least that's how I interpret the comment in the documentation. Did not really test it.

    To have any other path relative to the location of this Perl file, do something like

    use File::Basename;
    use lib dirname (__FILE__) . "/MyModules";
    

提交回复
热议问题