How do I use a Perl module from a relative location?

前端 未结 8 2152
予麋鹿
予麋鹿 2020-12-23 16:41

I have a dir called foo, and in that I have lib and bin. The scripts in bin need stuff in lib. I do someth

8条回答
  •  情话喂你
    2020-12-23 17:17

    I generally use this technique. Its sadly inspired from my PHP days:

    Its handy in situations where you know where a given file will be relative to the current one, and aren't sure of the entry points it may be called in or the surrounding environment at calltime.

    However, I would generally use this technique only for test scripts which need dummy libraries for emulating things.

    use File::Basename ();
    use Cwd            ();
    my $base_dir;
    my $relative_path; 
    BEGIN {
        $realitive_path = '../../' # Path to base of project relative to the current file
        $base_dir = Cwd::realpath( File::Basename::dirname(__FILE__) .'/' . $relative_path );
    }
    
    
    use lib "${base_dir}/lib";
    use Foo;
    

    Ideally there should be some module somewhere that does this, if not, I'm half tempted to write one:

    use Some::Module ();
    use lib Some::Module::relative_self('../../lib', __FILE__ ); 
    

提交回复
热议问题